-
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
Unused top-level code is not removed with format:esm but is removed with format:iife #1551
Comments
This is intended behavior. People sometimes use the DCE is enabled for IIFE output but not for ESM output because adding extra code at the end can't access inside the IIFE closure but can access inside the top-level ESM scope. However, DCE is always enabled when bundling because the assumption is that all of the code to be bundled is included in the input files. I agree that what the Svelte people want to do is weird. For example, they need to hack into the TypeScript compiler to get it to have this behavior. But esbuild's current behavior enables their use case while still making both things possible. So that's why it currently works that way. |
Thank you for this explanation. Would it be possible to add a configuration setting to enable DCE in an ESM using the transform API? So the default behavior for Svelte does not change, and the docs could tell about this behavior (or maybe I'm just blind and missed this info about requiring |
You should now be able to use |
Hello,
If I use an IIFE as output format, the dead top-level code is removed. I would expect the same behavior for ESM (as the global definitions also cannot leak outside the module).
This seems to be an unintended bug, as @evanw wrote on #1518 (comment)
Or is there maybe a technical reason why I need to use the
build
API to get DCE when using ESM as an output format? But I think it would still be inconsistent behavior.I have this code to check for the behavior
Which yields with esbuild 0.12.22:
❯ node esbuild-bug.js
Unused code is removed with format:iife
Unused code is kept with format:esm
Unused code is removed with format:esm and bundle:true
The text was updated successfully, but these errors were encountered: