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

minify: provide an option to enable treeshaking in single-file transform #1610

Closed
yyx990803 opened this issue Sep 15, 2021 · 3 comments · Fixed by #1625
Closed

minify: provide an option to enable treeshaking in single-file transform #1610

yyx990803 opened this issue Sep 15, 2021 · 3 comments · Fixed by #1625

Comments

@yyx990803
Copy link
Contributor

yyx990803 commented Sep 15, 2021

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:

  1. We know the file is a single ES module that won't be concatenated with other files;
  2. We do NOT want to use build --bundle because we want to minify the file in isolation.
  3. We also do NOT want to use --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 the transform API.

@yyx990803
Copy link
Contributor Author

yyx990803 commented Sep 15, 2021

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.

@yyx990803 yyx990803 changed the title minify: provide an option to force enable top-level treeshaking in single-file transform minify: provide an option to enable treeshaking in single-file transform Sep 16, 2021
yyx990803 added a commit to yyx990803/esbuild that referenced this issue Sep 17, 2021
yyx990803 added a commit to yyx990803/esbuild that referenced this issue Sep 18, 2021
@hyrious
Copy link

hyrious commented Sep 21, 2021

It is also possible to do what you said in bundle api with a plugin that externalizes everything except the entry point: #1576 (comment). But I do think it is valuable to do tree-shaking in transform too.

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.

@yyx990803
Copy link
Contributor Author

yyx990803 commented Sep 22, 2021

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.

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