-
Notifications
You must be signed in to change notification settings - Fork 109
use "exports" field in package.json #1410
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
Conversation
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.
fantastic
Jest is tricky to get working with ESM code. jestjs/jest#13739
Specifically when tests are written in TypeScript. https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/
...and their recommended configuration requires a newer version of ts-jest than 29.1.0, which is from 2023. 29.2.0 added a bunch of ESM config helpers.
...and because Jest writes its own require() logic as part of its mocking feature, it's more restrictive than Node.js v20.19 which allows CJS code (the Jest test) to require() ESM code (the xchainjs module). Jest won't allow that, so the test has to be ESM too.
...except the tests aren't ESM. There are a whole ton of require() calls that are used to read and parse a nearby JSON file. They can't work in ESM:
ReferenceError: require is not defined
ee58bf7 to
8bbb66c
Compare
f8edd47 to
f97511f
Compare
In order to make Jest work, since currently the tests only work when transpiled to CJS, and Jest refuses to let CJS code require ESM code, had to resort to conditional exports:
"exports": {
"require": "./lib/index.cjs",
"import": "./lib/index.esm.js"
},
Never mind. The built ESM code in xchain-cosmos/kujira/thorchain/mayachain (and probably anything that depends on them) just doesn't work.
It only maybe works if a transpiler messes with it. Raw, it fails trying to import from cosmos/cosmjs-types#93
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'xchainjs-lib/node_modules/cosmjs-types/cosmos/tx/v1beta1/tx' imported from xchainjs-lib/packages/xchain-cosmos/lib/index.esm.js
Did you mean to import cosmjs-types/cosmos/tx/v1beta1/tx.js?
CosmJs packages are only very lightly maintained by Confio these days, and they're all effectively CommonJs-only right now. cosmos/cosmjs#1649 (comment)
Since it's not exported from the top level index.ts at all, the only ESM-compatible way to access that type is importing as .../tx.js.
253594c to
7ecd27f
Compare
cc3cf32 to
a1ebd02
Compare
9.1.0 is the version that added ESM support.
can't omit the file extension in ESM imports.
can't omit the file extension in ESM imports.
69ea0a6 to
13f93b4
Compare
node packages have standardized on this field since circa 2020.
Uh oh!
There was an error while loading. Please reload this page.
https://nodejs.org/api/packages.html#exports
Without this field, Node.js import logic believes it's just a CJS package - node doesn't recognize the old non-standard
"module"field (while transpilers like webpack do).