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: Named export of objects includes whole file into the bundle instead of only used exports. (with sideEffects: false in package.json) #10346

Closed
Huuums opened this issue Feb 5, 2020 · 3 comments

Comments

@Huuums
Copy link

Huuums commented Feb 5, 2020

Bug report

What is the current behavior?

Creating a bunch of objects in a single file while directly exporting them from there with named exports will cause the whole file to be included in the bundle instead of treeshaking the unused exports.

Using a barreling approach where every object gets created and exported from its own file will treeshake correctly.

If the current behavior is a bug, please provide the steps to reproduce.
This repo let's you reproduce this behavior.
https://github.com/Huuums/repro-webpack-treeshaking-object-issue

What is the expected behavior?
Only include used exports into the bundle when defining multiple exports in a single file.

Other relevant information:
webpack version: 4.41.5
Node.js version: 13.6
Operating System: Mac OS (Catalina) and Windows behaved the same.
Additional tools: Create React App

mode: 'production' is set (Create react apps build script).

@sokra
Copy link
Member

sokra commented Feb 11, 2020

JSX is considered as side-effecty code, so you need to put it into a function to allow it to be removed.

@sokra
Copy link
Member

sokra commented Feb 11, 2020

babel/babel#11126

@sokra
Copy link
Member

sokra commented Apr 22, 2020

babel fix was merged

@sokra sokra closed this as completed Apr 22, 2020
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

2 participants