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

How to disable tree shaking? #1518

Closed
sc372 opened this issue Aug 12, 2021 · 6 comments · Fixed by #1625
Closed

How to disable tree shaking? #1518

sc372 opened this issue Aug 12, 2021 · 6 comments · Fixed by #1625

Comments

@sc372
Copy link

sc372 commented Aug 12, 2021

Hello.
Build with unused functions or variables included. Please tell me how to disable tree shake.

Thanks.

@hyrious
Copy link

hyrious commented Aug 12, 2021

Just don't --minify-syntax or --minify.

@sc372
Copy link
Author

sc372 commented Aug 12, 2021

@hyrious
I tried it without --minify or --minify-syntax. It doesn't work.

Thank you for answer.

@evanw
Copy link
Owner

evanw commented Aug 12, 2021

Do you have bundling enabled? There shouldn’t be any observable difference between bundling with tree shaking enabled and bundling with it disabled, so there is no way to disable tree shaking while bundling. Can you say why you need to do this?

The one thing you can control is to disable user-specified tree shaking annotations, which is sometimes useful as these annotations can sometimes be incorrect: https://esbuild.github.io/api/#manual-tree-shaking-annotations.

@sc372
Copy link
Author

sc372 commented Aug 13, 2021

@evanw
Hello.
Sometimes the code is hosting on CDN.

@evanw
Copy link
Owner

evanw commented Aug 15, 2021

I’m guessing your desire for disabling tree shaking is coming from a misunderstanding of how export formats work. There are currently three supported output formats when bundling is enabled: CommonJS, ES modules, and global code that is wrapped in a closure (a.k.a. IIFE). There is no output format for global code that’s not wrapped in a closure, so unexported module-level variables can never be accessed outside of the output file and tree shaking is always valid. See https://esbuild.github.io/api/#format.

If you are running code in the browser, you need to pick either ESM or IIFE. Neither of these formats allow module-level variables to leak into the global scope, so it’s ok for esbuild to remove unused code as it cannot possibly be referenced.

If you want to expose exports from your code on the global object when your script is run, you should use the IIFE format and either use the global name setting or manually assign your exports to properties on the global scope with window.prop = ….

You should only be using the ESM format with <script type="module">. And in that case top-level variables in the output file do not become global variables, so esbuild removing unused and unexported top-level variables is fine.

@sc372
Copy link
Author

sc372 commented Aug 17, 2021

@evanw
I lacked understanding of how the export format works.
It worked well using the 'IIFE' format method.

Thank you.

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 a pull request may close this issue.

3 participants