-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
CSS Module Order Changes When Using sideEffects: false
with Barrel Files
#18961
Comments
As we also see this issue in combination with Next.js, we initially thought it was a Next.js bug. The following issues may be related and eventually also resolved by this fix. |
This is not a webpack issue and unfortunately we can't fix it here. This applies to how we work with CSS (css -> js -> extract CSS) and to Bare JS imports are considered side free by default and can therefore be moved to any location in the file. It should be fixed in #14893 (I will add test). We can also fix this here - https://github.com/webpack-contrib/mini-css-extract-plugin. |
Ah super interesting! |
/cc @ahabhgk is rspack solve this problem? or just don't support it |
@alexander-akait I did a quick test (on the rspack branch of the reproduction) I added a simple side effect:
@@ -0,0 +1 @@
+console.log("hello world")
@@ -1 +1,2 @@
export * from './teaser';
+export * from "./side-effect" then I compiled it twice - onece with package.json for the javascript part both bundlers rspack and webpack worked the same way: |
@jantimon I looked deeper into your example and I want to say that you have the wrong I looked at other bundles and it seems they just don't support sideEffects or support it selectively. |
If you want to achieve the absence of unnecessary initial requests, I recommend using |
Also - #15610 (comment) |
Let me remind you that there is no such thing in js like |
I get your point about CSS imports being side effects and side effects in javascript should not affect the order, but I want to share why the current behavior is causing real headaches in production In the reproduction repo, you can see that just changing or removing a barrel file (like Other bundlers might handle sideEffects "less correct" for CSS, but at least they give us a consistent CSS order we can work with and understand. When discussing about those problems you very often hear things like just use tailwind or just use styled-components or just increase the specificity or just use For me the ideal tradeoff would be something in between - keep the good parts of JS tree-shaking but make CSS ordering predictable (like in rspack, vite and parcel) |
I added a new unused export to import styles from './unused.module.css';
export const CarouselUnused = ({
className = '',
}) => {
return `<div class="${styles.unused + (
className ? ` ${className}` : ''
)}">Unused</div>`;
}; This created the following results:
|
After update Rspack to v1.1.1 (>=1.0) it should have the same result as webpack ahabhgk/reproduction-webpack-css-order@835fc43 |
thanks @ahabhgk I removed rspack it from the table now that it has the exact same behaviour |
Connected Issues
Description
When using CSS Modules in a monorepo setup with barrel files (index.ts) and
sideEffects: false
, webpack produces an incorrect CSS ordering in the output bundle. This might be connected to the long-standing CSS ordering issues with tree-shakingKey Findings
sideEffects: false
in package.json"sideEffects": ["*.css"]
does NOT fix the issue - it behaves the same as"sideEffects": false
After some debugging we traced it to the build chunk graph logic:
webpack/lib/buildChunkGraph.js
Lines 683 to 708 in 5e21745
Working Variations
There are two workarounds:
"sideEffects": true
in package.json (unfortunately this will break chunk splitting)OR
But I believe we should fix it in webpack because it works in all other bundlers I tried (rspack, vite, parcel)
Reproduction
Full reproduction repository: https://github.com/jantimon/reproduction-webpack-css-order
The issue can be reproduced by:
sideEffects: false
produces incorrect CSS order ❌The repository has dist files checked in so you can see the full main.css
"sideEffects": ["*.css"]
produces incorrect CSS order ❌See also main.css
sideEffects: true
produces correct order ✅See also main.css
sideEffects: false
also produces correct order ✅See also main.css
Comparison with Other Bundlers
Each bundler repository branch contains the same code structure and dependencies, only changing the build configuration for the respective bundler:
Environment
The text was updated successfully, but these errors were encountered: