question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Treeshaking failing to remove an export that's only dynamically imported

See original GitHub issue

Bug report

What is the current behavior?

I found a bug with treeshaking, and will link to this repo to explain it.

The RxFunctionList file defines 2 named exported variables - one small (a graphQL fragment) and one heavy (the entire list of rxjs exports). Another file, App, statically imports the light export from RxFunctionList, and dynamically imports the heavy export.

Goal: for webpack’s treeshaking to remove the heavy export from the main bundle, as we only statically import the light variable from RxFunctionList. We want the main bundle to be free of any of the rxjs exports.

Test case Static import only Dynamic import only BUG: Static AND dynamic import
Result treeshaking removes rxjs exports as expected the bundle does not contain rxjs exports (they are code split) BUG: treeshaking fails, bundle includes rxjs exports
Main bundle size (before minification) 589kb 588kb 802kb
Webpack bundle analzyer image image image
Branch code static import dynamic import static import and dynamic import

Steps to reproduce

  1. make sure your node version is 17+
  2. git clone this repo
  3. cd frontend
  4. yarn install
  5. yarn build
  6. webpack-bundle-analyzer should open, and the main bundle should be 800+ kb (before minification).
  7. for further proof the bundle contains rxjs, open dist/main.bundle.js and see that it includes rx functions like bindnodecallback, dematerialize

Additional information

I found this discussion of a similar topic and made sure to enable Terser in my webpack config. As explained in the grid above, I am getting treeshaking to work properly when only statically importing from a file - but it fails to treeshake exports that are dynamically imported, despite the fact they are never statically imported.

Other relevant information: webpack version: 5.65 Node.js version: 17 Operating System: Mac OS Additional tools: yarn, react, typescript

Are you willing to work on this yourself

Potentially

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
alexander-akaitcommented, Jan 10, 2022

You can setup splitChunks to do it, by default we decide to keep this in one chunk

0reactions
alexander-akaitcommented, Feb 2, 2022

Yep

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree shaking and code splitting in webpack - LogRocket Blog
To dynamically import a node module and tree shake it, we can first create a module that only exports what we want, then...
Read more >
webpack@4 Tree Shaking doesn't eliminate code between ...
js : ES exports two methods: red and blue via two scenarios: one-file (all code within one file) and re-export (methods re-exported from ......
Read more >
The unexpected impact of dynamic imports on tree shaking
When you dynamically import a module, it automatically makes that module ineligible for tree shaking. The result of the dynamic import is an ......
Read more >
Webpack 4 tree shaking - Marcin Wanago Blog
We often use named imports on files that have a lot of other exports. It might create a case in which we don't...
Read more >
Rollup - keep individual exports from tree-shaking
How do I keep the exports from being tree-shaken out of the output (only for *.constants.js in my real scenario)? ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found