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

add --force-tree-shaking flag #1615

Closed
wants to merge 1 commit into from

Conversation

yyx990803
Copy link
Contributor

closes #1610

Please see committed changelog entry for details.

@yyx990803
Copy link
Contributor Author

yyx990803 commented Sep 17, 2021

Some additional details - I considered some other options without adding a new flag:

  1. Add another enumerated value to --tree-shake, like --tree-shake=force. However, it would be ambiguous how it interacts with --tree-shake=ignore-annotations. Ideally, we want to keep the ability to force treeshaking but still ignore annotations.

  2. Making it an implicit behavior when --minify is enabled, as in most cases minification implies removing unused code. However, this would technically be a breaking change so I tried to avoid it.

I'm open to any form of API adjustments as long as it allows us to minify a single file using transform with tree-shaking enabled.

@yyx990803
Copy link
Contributor Author

yyx990803 commented Sep 18, 2021

I just realized the current --minify behavior is already non-concatenation safe when processing an ESM input file when a target format is specified. This also applies when it implicitly detects ESM syntax:

Given this input:

function foo() {}
export { foo }

Result of running esbuild --minify:

function n(){}export{n as bar};

Notice foo has been renamed, which will break if another concatenated file expects to call foo().

That is to say, esbuild already assumes isolated scope when it is in ModeConvertFormat. With this in mind, I believe the correct behavior would be auto enabling tree-shaking when (1) --minify is enabled and (2) running in ModeConvertFormat. Since --minify was concatenation-unsafe to begin with, this should not be considered a breaking change.

We can also add a separate --minify-unused flag if necessary.

@yyx990803
Copy link
Contributor Author

Closing in favor of #1617

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

Successfully merging this pull request may close these issues.

minify: provide an option to enable treeshaking in single-file transform
1 participant