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.

CSS files being treeshaken even when sideEffects=true

See original GitHub issue

Bug 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:

  1. In a js file, add import "somecss.css" so that javascript file is importing the css file without named modules
  2. 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:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
alexander-akaitcommented, Apr 1, 2021

IMO if file A imports B where B has sideEffects, then A should be calculated to have sideEffects as well to be on the safer side.

No

0reactions
paztiscommented, Sep 29, 2022

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

"sideEffects": [
    "index.js",
    "*.css",
    "*.scss"
],

I hope index.js in the sideEffects will not keep too much code…

Read more comments on GitHub >

github_iconTop 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 >

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