-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
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
Additional context
No response