-
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
ESM entry module's exports signature is not preserved when required()
by other modules
#706
Comments
I created a reproduction repo: https://github.com/yyx990803/esbuild-exports-signature-repro |
required()
required()
by other modules
Thanks for the report. I'll keep this in mind during the upcoming code splitting rewrite. |
Just to add to Evan's decription: when bundling ESM (even when An even simpler example is esbuild: when importing it as an ESM module in NodeJS Example: // node test.mjs
import { transform } from 'esbuild';
console.log(transform); Error: import { transform } from 'esbuild';
^^^^^^^^^
SyntaxError: Named export 'transform' not found. The requested module 'esbuild' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using: I think esbuild is being bundle with esbuild, so it exhibits the same export mangling problem. |
Actually, why do you say this? It looks like CommonJS to me: https://unpkg.com/preact@10.5.12/dist/preact.min.js. It assigns to |
@evanw I think that would be awesome as it will remove the ambiguity (the same applies for the |
Context: I am using esbuild to do dependency-pre-bundling in Vite. This is a multiple-entry build with
splitting: true
. The entries are resolved package entry files - e.g.I'm also using a plugin with
onResolve
that redirects any imports topreact
orreact
to these already resolved file locations to avoid duplicated copies of them in the resulting bundle.Preact is a pure ESM module, so its corresponding output entry chunk is expected to preserve the same export signature as the original entry module. However, this is broken if any transitive module in the bundle contains a cjs
require()
call to it. E.g. if somewhere down the tree a dependency does this:This causes esbuild to wrap the ESM preact module with a CommonJS wrapper and expose only a
default
export. This makes sense for the internal interop, but currently this also affects the final output entry chunk for preact: now the output chunk for preact also only has adefault
export! This creates a mismatch between the output of the preact chunk and the original entry chunk.What I imagine esbuild needs to do is to
require()
Currently I have to work around this by detecting exports mismatch between input/output entry chunks and perform additional runtime interop. Would be great to have this fixed in esbuild itself.
The text was updated successfully, but these errors were encountered: