-
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
minify: provide an option to enable treeshaking in single-file transform #1610
Comments
Actually this is why I've found a somewhat unpredictable 10%~20% difference compared to Terser when I tested using esbuild as the minifier for Vite in real world cases - in some cases it's Rollup's treeshaking failing, in some cases it's plugins leaving unused code expecting the minifier to remove them. I think having this option would allow Vite to default to esbuild as the minifier. |
also add dedicated --minify-unused flag. closes evanw#1610
It is also possible to do what you said in However, as I know vite uses rollup to build the dist, which does tree-shaking a lot (at least more than esbuild). Therefore I don't think applying esbuild on rollup's result could make dist smaller. |
Our use case is a bit niche, since we rely on disabling Rollup's treeshaking for certain modules for module graph analysis in bundle generation phase, and instead rely on the minifier to remove those forced-included modules. There are also some cases where Rollup bails out by default (e.g. try catch blocks) despite a conditional branch that will never execute post transform, causing all code used by that branch to be included. |
In 4d32f7c it was explained that treeshaking is disabled in single file transform due to esbuild not seeing the entire bundle. However, there are use cases where:
build --bundle
because we want to minify the file in isolation.--format iife
because we don't want to change the format of the file.A real world example is where Vite using
esbuild
as a minifier to minify ESM chunks produced by Rollup.Currently there seems to be no way to force esbuild to enable top-level treeshaking when using the
transform
API - this creates some issues when some tools' codegen leaves unused declarations expecting them to be removed by minifiers.I think it'd be nice to have an option to force enable this. Maybe just respect
treeShaking: true
when calling thetransform
API.The text was updated successfully, but these errors were encountered: