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

Fixing the standalone CLI by making ESM and CJS builds for the SDK #2541

Merged
merged 23 commits into from
Dec 23, 2021

Conversation

habdelra
Copy link
Contributor

@habdelra habdelra commented Dec 23, 2021

apologies for all the npm pushes--i needed to do a full circle tests of the build process which was initially not working for the standalone CLI consuming the SDK from npm.

The main takeaway from this PR is that we are using tsup to create ESM and CJS bundles (as well as the index.d.ts file) coupled with the exports property in the package.json to be able to describe to the different types of bundles. Confirmed that this works with the standalone CLI. This, in theory, should work with the metro bundler in the wallet app too (the SDK package.json main is pointing to the ESM entry point still to make things really simple for metro in case it doesn't understand the package.json exports).

I pushed a pkg for the CLI into AWS, so that should be happy now. we can hold off, though, on merging this until after the holidays so that we can test together with metro.

EDIT: it looks like according to this issue metro does not currently understand package.json "exports" facebook/metro#670, so it will fallback to using the package.json "main" instead, which should be fine.

"typescript": "4.2.3"
},
"files": [
"dist"
],
"scripts": {
"postinstall": "node bin/codegen.js",
"prepack": "tsc"
"postinstall": "if [ -f ./bin/codegen.js ]; then node bin/codegen.js; fi",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lukemelia this is an interesting gotcha--the package in npm doesn't actually include bin/. this script only works for maintainers of this source code, not for pkgs downloaded from npm. This actually blows up if you yarn install this package because of this file not being present

Comment on lines 8 to 14
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/esm/index.js",
"types": "./dist/index.d.ts"
}
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the new shiny thing that node recognizes that will allow the CLI to use the CJS entrypoint. Metro hopefully can understand this, otherwise, it should be able to fallback to the main property which defaults to the ESM entrypoint.

Copy link
Contributor Author

@habdelra habdelra Dec 23, 2021

Choose a reason for hiding this comment

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

Arg--it looks like TS is mad now, because it's trying to use ./dist/esm/index.js. i mean we can allow that to happen, but that's kind of annoying because it's yet another build you have to have running. i wonder if there is a rule that will work for TS but not for metro.

I'm going to try using main for metro, and exports.import for ts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to this, metro does not understand package.json exports, so this approach probably works in our favor facebook/metro#670

Copy link
Contributor

@lukemelia lukemelia left a comment

Choose a reason for hiding this comment

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

tsup tsup and AWAY!!!! 🦸

@habdelra habdelra merged commit e7b2bd9 into main Dec 23, 2021
@delete-merged-branch delete-merged-branch bot deleted the standalone-cli-fails-to-run-packagejson-cs-2888 branch December 23, 2021 23:10
@tintinthong tintinthong mentioned this pull request May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants