CSS files being treeshaken even when sideEffects=true
See original GitHub issueBug report
What is the current behavior? Css files that are imported without named imports is currently removed even though css files have sideEffects=true defined in the webpack config.
If the current behavior is a bug, please provide the steps to reproduce. I’ve created a repo to help reproduce it: https://github.com/k2snowman69/bug-css-tree-shaking
Basic textual intepretation:
- In a js file, add
import "somecss.css"
so that javascript file is importing the css file without named modules - Make sure your webpack config specifies that css files have sideEffects
{
sideEffects: true,
test: /\.css$/,
use: [
// MiniCssExtractPlugin is not necessary to replicate this issue.
// I added it as it is easier to view the order of the generated CSS.
// However, you can still see the issue by searching for the generated
// class names in the .js bundle. They're still out of order.
MiniCssExtractPlugin.loader,
"css-loader",
],
},
What is the expected behavior? That the css file is included in the final build output. In my sample repository there should be two classes available:
.component--import-modules {
color: black;
}
.component--import-no-modules {
color: lightblue;
}
However currently only one is compiled:
.component--import-modules {
color: black;
}
Other relevant information: webpack version: 5.28.0 Node.js version: 14.15.4 Operating System: OSX Additional tools: babel@7.x.x
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Tree Shaking - webpack
The sideEffects and usedExports (more known as tree shaking) optimizations are two different things. sideEffects is much more effective since it allows to...
Read more >webpack 4 tree-shaking style sideEffects: Fix #1527 - GitHub
General: The problem is if you are writing libraries, and exposing SFCs the CSS is not included in the resulting tree-shaken result. Despite ......
Read more >sideEffects in antd package.json not working as expected
We have tried to set sideEffects: true for all CSS files being picked up from node_modules for CSSLoader in Webpack config's module.rules.
Read more >1 Webpack rule that may reduce your CSS file size dramatically
By flipping the switch and turning the value into true, it will enable the tree shaking technique, and reduce your CSS file size...
Read more >Tree Shaking CSS problem - YouTube
Today is more about a problem than a solution: https://github.com/kentcdodds/ama/issues/549.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No
facing the same issue: all the css imported in the index.js files are removed, while not the others. I’ve found a workaround by adding this in my package.json
I hope index.js in the sideEffects will not keep too much code…