-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 build for browsers #342
Conversation
@@ -87,4 +87,4 @@ let api: typeof types = { | |||
startService, | |||
}; | |||
|
|||
module.exports = api; |
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 change was needed, because without it, ESM shim code was being included in the ESM build. I switched it so that now it uses a default export as well as named exports. I took a look at the outputs of the various bundles and it seems correct. The bundle that I'm least sure about is the browser.js
bundle, but it looks right as far as I can tell.
"exports": { | ||
".": { | ||
"browser": { | ||
"module": "./lib/browser.mjs", |
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.
The ./lib/browser.mjs
does not import any CJS files, so it qualifies for a "module-only optimization" as described here. I think it is not necessary to additionally have the import
field, since bundlers will prefer module
over import
, and only node needs import
, but node does not look at browser
.
const browserMjs = childProcess.execFileSync(esbuildPath, [ | ||
path.join(repoDir, 'lib', 'browser.ts'), | ||
'--bundle', | ||
'--target=es2020', |
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.
I set it to a modern target since the assumption for the .mjs
build is that if people need it to work in older browsers, they can transpile it themselves.
Hey @evanw Could you take a look at this? |
Thanks for the ping. I can definitely ship something like this. The export logic isn't quite right though since you don't want bundlers that support modules picking up |
Changes I made:
So basically the only change in the next release will be a new |
Hi @evanw!
I added a ESM build for use in browsers, so that the CJS shim code is not needed for browsers which support ESM natively. I added it to
pkg.module
which is enough for my use case.As a bonus, I added
pkg.exports
, which is not necessary for my use case, but I thought I might as well. I saw the discussion in this issue and since then, the discussions between webpack and rollup authors (and others) seem to have stabilized. Here's a good summary of those discussions (keep in mind they went withmodule
instead ofmoduleOnly
). If you decide that you aren't quite ready to havepkg.exports
here, I am totally fine with removing that from this PR 🙂Thank you for your work with esbuild! I am very excited to see where this project goes in the future, and I am looking forward to new competition in the tooling landscape!