-
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
Producing esm bundles: Externalized dependencies should always be imported #1148
Comments
Another thread about this: #566.
Can't you just access the
This currently isn't done automatically because doing this unavoidably changes the semantics of the code (dynamic |
Definitely understand the concern in changing the order of operations. Side effects and initialization end up running in a different order when translating commonjs, given the differences with esm. FWIW I think I would still expect the imports to be hoisted as the default behavior though; especially across package boundaries, the initialization order should probably not be a part of the contract. Perhaps with require statements inside the same package, you could have ordering issues. But I think if there are real scenarios where order of import execution matters, the author should be adjusting their file structures. But this seems like an edge case. But given that changing order may cause unintended effects, lets say we do this in a plugin. How would this be done? All I can come up with is a pre-proccessor of sorts in an |
@evanw what would be the implications of changing the |
@evanw ignore my comment above I got it working with the plugin proposed in #566 by changing the conversion module to be @dzearing seemingly if you adapt the solution with the plugin in #566 to return |
@evanw This was opened a bit ago, and after thinking about this for some time and using Rollup for cases where I need to externalize libraries being This is what Rollup does, fwiw. In the project we work on, when we detect this scenario, we have to fall back to Rollup for the job because of this issue. It would super great to add this feature, despite the inconsistency it might introduce. |
I'm bundling
react
directly (index.js
) withformat=esm
, trying to produce a browser consumable bundle. The library is commonjs and depends on the packageobject-assign
, which I want to externalize to another bundle (external: ['object-assign']
in the config.) When externalizing, the require statement is not removed from the bundle output:This breaks using the bundle in the browser, as require is not defined.
Is there any clever way using esbuild, or the plugin system, to translate this at build time to an esm import? At the top of the bundle, I would like to see this in the bundle output:
And no leftover
require
statements, making it loadable by the browser in conjunction with an import-map resolving theobject-assign
dependency.I got very close using a plugin, by making onResolve of the dependency resolve to a generated stub file which imports and re-exports the library. However, in doing so the commonjs
assign
reference being imported would resolve to the export map, rather than the default export. I don't know how to control that.Note: I tried this using Rollup + the commonjs plugin. By default it externalizes the dependency and I see this at the top of the output:
The text was updated successfully, but these errors were encountered: