Remove unused exports from dynamic imported module
- Dominant language
- Go
- Stars
- 40.1k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
When using a dynamic import, unused symbols are not being tree-shaken (dropped) from the output bundle.
**Input Code Example**
```ts
// main.ts
const { foo } = await import('./dep.js');
console.log(foo);
```
```ts
// dep.js
export const foo = "foo";
export const bar = "bar";
```
**Observed Output**
```js
// 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:
```js
// 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
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.