Skip to content

[FR] Merge duplicate named imports from the same module during minification #11133

@rentalhost

Description

@rentalhost

Describe the feature

Hey there! I'm using esbuild to bundle my JavaScript project into a single .js file. It works great for the bundling part, but I prefer handling the final minification with SWC, which is more efficient in some cases. The issue is that this happens in both esbuild and SWC, and I was hoping SWC would fix it automatically.

Basically, when multiple source files import the same dependency, the final bundle ends up with duplicate imports. This makes sense in esbuild without minification, as it preserves the original structure, but I expected SWC to deduplicate and optimize it during minification. Instead, it keeps the duplicates, sometimes even creating unnecessary aliases.

For example, imagine two simple files in my project:

  • In math-utils.js: import { add } from 'math-library'; (used to sum numbers in a utility function).

  • In calculator.js: import { add } from 'math-library'; (used to sum values in a calculator).

In the final esbuild bundle (without minification), this turns into something like:

// From math-utils.js
import { add } from 'math-library';

// From calculator.js
import { add } from 'math-library';

And when I run it through SWC for minification, instead of merging into a single import { add } from 'math-library';, it transforms into:

import { add as a } from 'math-library';
import { add as b } from 'math-library';

This bloats the bundle size for no reason, since the module is only loaded once in JS. It'd be awesome if SWC had an option to automatically merge these identical imports during minification, like a flag such as "mergeDuplicateImports": true in .swcrc.

What do you think?

Babel plugin or link to the feature description

https://play.swc.rs/?version=1.13.20&code=H4sIAAAAAAAAA9PXV3Arys9VyE0sydAtLcnMKdbLKublyswtyC8qUahWSExJUUgsVkhUqFVIA6lTByvMyUwqSiyqVLfm5eLlSs7PK87PSdXLyU%2FXSNQw1DHSMdbUBEnoQ81OTsxJLs1JLMkvwmp2EpFmJ8HMBgBXdxhctAAAAA%3D%3D&config=H4sIAAAAAAAAA32VyY7bMAyG7%2FMUgc89FINiDj0XBXroMwiKRTlMtRgilYkxyLuXXrLMhPYt5seflEiK%2BXjZ7Zojtc3P3Yf8lI%2FeFoJy%2BxYLDYntWSwNtNFSW7Dn5tuVHmlE3gaCyXSZScO2dMCTil6%2Fv%2F5YFE3ImeCqWGwRE%2FrhMWebY1%2BA6MEmVglZIySmz%2FqFlfw%2BAi710b7POYBNG8RYMpgYOiha4DaHYHsCc7JFiTKe1BakrKUYYWVwpi%2B5V3lyyJiT5HymDqwzbXagICzQMp5Ak0kukSWS6yn3mbCDfe26qc9f1HCyoVpWcsJ5aomcVol6yEhsfE1aCWe4UoMZLsX9qkRvCnAt6Vl3zJhWevIPQCoQLFGyEbS4k4eXeVpT%2B00lJi8jy4PCZb61WybopKgG0SuVHSsDhVHrZgFXWxgr22rHWfBK%2BQgdGPBeZkUJTe%2FI7UFLykMP2StA%2Bmu9NlUzMLdXuMLHB7GBf8stWR%2BwxSNaPqxTGuI%2Bh40EEfiQ3YaDtILzOi6yJc79Oq%2FJgYwGONWl0gSel4A8AM4mTPvyaTbkeUhE04W8v6%2BJxeFy28PRpu7%2B3j%2BtYjkxFBwXpw2PSxYi8h%2FZEjKY7td9qc7al0XfxOzqFHj5gxhnY17rb83d6brBb5dukP5eldOBL%2F8BX%2FWab2wGAAA%3D

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions