Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): proper esm support and codegen overhauls #754

Merged
merged 23 commits into from
Oct 16, 2023
Merged

Conversation

erunion
Copy link
Member

@erunion erunion commented Oct 11, 2023

🚥 Resolves RM-7990

🧰 Changes

  • Changes api install to no longer prompt for a language or target to install. JS is now the only export target.
  • Overhauls codegen internals to now use tsup to compile our constructed TS code into a JS SDK that can be used in CJS and ESM environments.
    • We were previously handling this with a lot of very touchy ts-morph hacks that didn't really support ESM environments.
  • Refactored codegen to now store generated SDKs within a src/ directory.
    • Doing this mostly for aesthetic reasons now that we're going to have a dist/ directory post-tsup compilation.
  • Upgrades ts-morph to the latest release. chore(deps): bump ts-morph from 17.0.1 to 20.0.0 #752

🧬 QA & Testing

  • Can you create an SDK?
  • Can you load the SDK from a .cjs, .mjs, .js, and .ts example file?

@erunion erunion changed the title feat(api): wip of proper esm support and codegen overhauls feat(api): proper esm support and codegen overhauls Oct 11, 2023
@erunion erunion added enhancement New feature or request refactor Issues about tackling technical debt area:core Issues related to `core`, which is the package that powers the SDKs at runtime area:api Issues related to the `api` CLI, which builds the SDKs labels Oct 11, 2023
* `ts-morph` unfortunately doesn't give us any options for programatically doing this
* so we need to resort to modifying the emitted JS code.
*/
code = code
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goodbye forever to this mess

packages/api/package.json Outdated Show resolved Hide resolved
const sourceFile = this.project.createSourceFile('schemas.ts', '');
const schemasDir = this.project.createDirectory('schemas');
private createSchemasFile(sourceDirectory: Directory) {
const sourceFile = sourceDirectory.createSourceFile('schemas.ts', '');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These sourceDirectory changes mean that these files we're creating here (schemas.ts and schemas/) are now being created in a src/ directory.

@erunion erunion marked this pull request as ready for review October 16, 2023 18:31
@erunion erunion requested a review from kanadgupta October 16, 2023 18:31
@kanadgupta kanadgupta mentioned this pull request Oct 16, 2023
7 tasks
kanadgupta and others added 2 commits October 16, 2023 13:32
* chore: minor tsconfig cleanup

* fix: point to correct api-core pkg version

* fix: install TS as dev-dep

* fix: add this to silence the JSON import warning

* refactor: slight cleanup
@erunion erunion added this to the v7 milestone Oct 16, 2023
Copy link
Member

@kanadgupta kanadgupta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mostly cleanup suggestions but overall very happy with this! love how much logic we get to clean up thanks to tsup 🥳

@@ -54,12 +55,12 @@
"prompts": "^2.4.2",
"semver": "^7.3.8",
"ssri": "^10.0.1",
"ts-morph": "^17.0.1",
"ts-morph": "^20.0.0",
"tsup": "^7.2.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we remove this as a dep since it's being installed in the codegen'd directory now?

requiredPackages!: Record<
string,
{
reason: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a follow-up to the work I added in #759, i was thinking it might make sense to have a devDep flag that splits out our required deps (e.g., @readme/api-core) from our required dev-deps (e.g., typescript)

Suggested change
reason: string;
devDependency: boolean;
reason: string;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do this in another PR since it's going to require a little bit of work

packages/api/src/codegen/factory.ts Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might wanna regenerate these now that this file has devDependencies

// });
// });

it('should be able to make an API request (JS + ESM)', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we added a test in this file that imports the generated cjs file and ensures that that works?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats going to require building a dist for these fixtures and keeping that dist in git. i want to move these fixtures into @api/test-utils at some point, will rework this then

packages/api/src/codegen/languages/typescript/index.ts Outdated Show resolved Hide resolved
packages/api/src/codegen/languages/typescript/index.ts Outdated Show resolved Hide resolved
}
: {}),
},
dependencies,
Copy link
Member

@kanadgupta kanadgupta Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per this comment, might be good to add the dev-deps here so we're not running npm i several times

async compile(storage: Storage, opts: InstallerOptions = {}): Promise<void> {
const installDir = storage.getIdentifierStorageDir();

await execa('npm', ['install', 'tsup', 'typescript', '-D'], {
Copy link
Member

@kanadgupta kanadgupta Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per this comment, might be good to remove this and take care of the dev-dep installation in the earlier install command so we're not running npm i several times

@erunion erunion requested a review from kanadgupta October 16, 2023 21:16
@erunion erunion merged commit e739d30 into main Oct 16, 2023
5 checks passed
@erunion erunion deleted the refactor/codegen branch October 16, 2023 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:api Issues related to the `api` CLI, which builds the SDKs area:core Issues related to `core`, which is the package that powers the SDKs at runtime enhancement New feature or request refactor Issues about tackling technical debt
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants