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

Treeshaking and dead code #1481

Closed
igorskyflyer opened this issue Jul 30, 2021 · 5 comments
Closed

Treeshaking and dead code #1481

igorskyflyer opened this issue Jul 30, 2021 · 5 comments

Comments

@igorskyflyer
Copy link

igorskyflyer commented Jul 30, 2021

Hello, a very fast and very efficient bundler indeed but I am having issues with treeshaking, sorry in advance if I misunderstood the concept of treeshaking. Let me explain the issue; as all of us who use bundlers we install our 3rd-party modules and bundle them into a single file but what I'm seeing in that bundle file is a lot of dead code.

For example, let's say we have a library X that exports 2 functions and has some top-level variables (for whatever reason) which are only referenced in one of the 2 functions but I don't use that function, I use the other one - the one that doesn't reference any top-level variables.

In my project Y I set up everything and bundle the code and I see all variables + all functions from library X in the bundle file.

Note that setting sideEffects: false in the library X doesn't do anything.

Is this the expected behaviour or am I missing something? Also, I've set up a repository for you to test - if you want to - it's located at igorskyflyer/esbuild-treeshaking. I've included the dist folder too so you don't have to build anything to begin with. 😄

P.S. don't judge me by my code in the lib folder, I never use quotes - only single quotes and I don't name my variables like that!!! 😂

Thanks.

@igorskyflyer
Copy link
Author

May be somewhat related to #1435.

@hyrious
Copy link

hyrious commented Jul 30, 2021

there's no tree shaking support for cjs code (lib/main.js) now. you should use esm (import/export).

@igorskyflyer
Copy link
Author

there's no tree shaking support for cjs code (lib/main.js) now. you should use esm (import/export).

Ah, that makes sense, don't I feel stupid now? 😁 I'll work it out somehow with ES modules, thanks. 👍

@igorskyflyer
Copy link
Author

there's no tree shaking support for cjs code (lib/main.js) now. you should use esm (import/export).

Ah, that makes sense, don't I feel stupid now? 😁 I'll work it out somehow with ES modules, thanks. 👍

Or not - I mean - it does work but it requires all modules to be ES modules which is almost impossible to obtain at this moment in the npm ecosystem. 😥

@Aworldc
Copy link

Aworldc commented May 5, 2023

there's no tree shaking support for cjs code (lib/main.js) now. you should use esm (import/export).

Ah, that makes sense, don't I feel stupid now? 😁 I'll work it out somehow with ES modules, thanks. 👍

Or not - I mean - it does work but it requires all modules to be ES modules which is almost impossible to obtain at this moment in the npm ecosystem. 😥

Yeah. that's my issue. Anyone know of something that can treeshake cjs?

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

No branches or pull requests

3 participants