-
Notifications
You must be signed in to change notification settings - Fork 64
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
Fixing the standalone CLI by making ESM and CJS builds for the SDK #2541
Conversation
…y for metro consumption as possible)
…s file exists or not
"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", |
There was a problem hiding this comment.
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
packages/cardpay-sdk/package.json
Outdated
"exports": { | ||
".": { | ||
"require": "./dist/index.js", | ||
"import": "./dist/esm/index.js", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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!!!! 🦸
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 theexports
property in thepackage.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.jsonmain
is pointing to the ESM entry point still to make things really simple for metro in case it doesn't understand the package.jsonexports
).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.