Skip to content
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

Remove unused exports from dynamic imported module #3987

Open
alan-agius4 opened this issue Nov 26, 2024 · 0 comments
Open

Remove unused exports from dynamic imported module #3987

alan-agius4 opened this issue Nov 26, 2024 · 0 comments

Comments

@alan-agius4
Copy link

When using a dynamic import, unused symbols are not being tree-shaken (dropped) from the output bundle.

Input Code Example

// main.ts
const { foo } = await import('./dep.js');
console.log(foo);
// dep.js
export const foo = "foo";
export const bar = "bar";

Observed Output

// dep-CH3SPVYO.js (69 bytes)
var foo = "foo";
var bar = "bar";
export {
  bar,
  foo
};

Actual
The bar variable, which is unused in the code, is not being tree-shaken (dropped) as expected. This increases the bundle size unnecessarily.

Expected Output
Only the foo export should be retained in the final bundle because bar is not used in the importing module. The desired output should look like:

// dep-CH3SPVYO.js (40 bytes)
var foo = "foo";
export {
  foo
};

Context
The issue arises even though esbuild is set up with splitting and bundle optimizations enabled. The expectation is that unused exports like bar would be eliminated.

Reproduction Link
The behavior can be reproduced using the REPL: https://esbuild.github.io/try/#YgAwLjI0LjAALS1idW5kbGUgLS1mb3JtYXQ9ZXNtIC0tc3BsaXR0aW5nIC0tb3V0ZGlyPSIvIiAAZQBlbnRyeS5qcwBjb25zdCB7IGZvbyB9ID0gYXdhaXQgaW1wb3J0KCcuL2RlcC5qcycpOwpjb25zb2xlLmxvZyhmb28pAABkZXAuanMAZXhwb3J0IGNvbnN0IGZvbyA9ICJmb28iOwpleHBvcnQgY29uc3QgYmFyID0gImJhciI7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant