-
Notifications
You must be signed in to change notification settings - Fork 159
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
Add ESM #400
Add ESM #400
Conversation
b2b8029
to
a310499
Compare
@loynoir Hi, thanks for this PR, I'll need some time to test and review it. Just be mindful that it's very important not to break existing installations across each platform (so Node, Deno (both NPM and here), Bun, and esm.sh) Do you have any reference links on best practice to support both ESM and CJS? I assume the |
#!/usr/bin/node --experimental-import-meta-resolve
// @ts-check
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url)
console.log({
requireResolve: require.resolve('@sinclair/typebox'),
importMetaResolve: await import.meta.resolve('@sinclair/typebox')
}) Runtime resolve is correct, tested under node v18.15.0
I don't know if there is official guide. |
Wait a minute. await import('@sinclair/typebox') Editor dts resolve to |
microsoft/TypeScript#53045 (comment) await import('@sinclair/typebox') Editor dts resolve to Yet, seems a typescript bug to me. Nothing can be done here. But, the good news is they are same. So, even dts resolve wrong, it is correct. 😉
|
@loynoir Thanks for doing this research into this. I think at this stage, I may need to hold off merging this PR till the next minor semver (and probably publish on a Would you be willing to defer this PR till the next minor semver? (possibly 2-4 weeks) ? |
I'm fine with that. In case of at that time point, I am busy. All noticeable informations are
|
@loynoir Hi, just following up on this PR. I've been through the changes, and I don't think I can merge this through at this time. These changes are too far reaching (338 files changed) and there's still a lot of uncertainty around dual publishing CJS and ESM in general (with the uncertainly around dual publishing making such a large change on the codebase untenable in the instance better approaches eventuate down the road). There are some recommendation guidelines in the works (as linked microsoft/TypeScript#52593) which I'm going to let settle for a while, but I think is the best call for now is to wait and see. I think rather than explicit appending each import with the As for module splitting (file size optimization), this is very much under consideration, but won't be carried out for another few minor semver iterations. There has been some desire to offer better modularity / splitting of the codebase in places (outside of this PR and associated issues), so will be taking a look at this eventually, but for now there's a necessity to stabilize the previous 3 minor semver revisions before carrying out this work, and it's going to be difficult to carry this PR along in it's current form until such time. Apologies for taking so long to get back to you on this PR. I appreciate this work and motivation behind it. But will need to revisit at a later time. I hope you understand. Will close off and revisit down the road. |
Main Goal
decrease bundle size #399
Sub Goal
100% backward compact with CJS
better tree shake support, for bundler targetting ESM
When using CJS, refer to CJS dts. When using ESM, refer to ESM dts.
support runtime ESM
[feat] treat typescript namespace like
import * as foo from 'foo'
which refer toImportNamespaceSpecifier
in AST evanw/esbuild#3077 (comment)Impossible Goal
tsconfig
to enforce coding style must explicit import extension.Changes
To achive 100% backward compact with CJS, and using ESM, now it is dual package using conditional exports.
Add explicit import extension, as ESM runtime needs explicit import extension.
Before
$ npm exec -- esbuild --format=esm --bundle --outfile=reproduce.output.mjs reproduce.mjs reproduce.output.mjs 148.7kb
After
$ cp /path/to/fork/typebox/target/build/sinclair-typebox-0.28.1.tgz build/ $ npm remove @sinclair/typebox $ npm i ./build/sinclair-typebox-0.28.1.tgz $ npm exec -- esbuild --format=esm --bundle --outfile=reproduce.output.mjs reproduce.mjs reproduce.output.mjs 129.8kb