-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
should use esm/cjs's native way to import externals (no wrappers needed) #1927
Comments
This is actually some sort of @rollup/plugin-commonjs does. But that's not trivial, changing the import method will result in choosing different files according to the While I'd like to see esbuild can do this tiny transform (just get rid of In simple cases, someone has written a script to do that transform: #1921 (comment). |
That only happens when running the output in a Node.js environment, not for browsers. (most bundlers implenment the Node.js resovle algorithm, but not all of them) |
whats the consensus on this ? |
See my comment here: #1921 (comment) |
Code Example
Say the we have an input file
foobar.js
:What I expect when running
esbuild foobar.js --bundle --format=esm --external:foo --external:bar --platform=browser
is:However the actual is:
Reason
Generally we have two ways to consume the output:
Consume the output directly
When we consume the output directly, the externals should ready available as well. In browsers, we should already specified what the module
foo
and the modulebar
are using import-maps. Sincefoo
andbar
must be esm, we don't need a wrapper when improting them. In Node.js, it meansfoo
andbar
are already installed or them are just nodejs built-in modules. Thoughtfoo
andbar
could be esm or cjs, but we dont' need an import wrapper because Node.js will automatically does it for us.However in current implementation, esbuild will produce something like
var bar = __require("bar");
, which will cause a rumtime error.Consume the output via a bundler
When consume the output via a bundler, the bundler will actually add those wrappers. So no wrappers needed for esbuild.
The text was updated successfully, but these errors were encountered: