-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
CSS Order Differs Between Development & Production Modes when Treeshaking. #7094
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
Comments
Yeah we could improve this. (But technically using sideEffects you say order doesn't matter). Note to myself: for "index" we use dependency order, but for harmony execution harmony initi oder should be respected. |
Thanks for the reply.
I'm curious why this is the case. Is it a technical limitation? For me, the final order doesn't matter, but what does matter is a consistent order between development and production environments |
@brycehill are you saying that unused CSS which causes the development mode to look different from production mode is getting treeshaken? |
@TheLarkInn That's what I thought was happening at first. However, if you take a look at my example repo, none of the CSS is eliminated, but setting
|
Should be fixed by #7507 in 4.12.1 |
Not sure if this is related, but after upgrading babel to v7 (already on webpack 4.17.2), my staging/production webpack builds are missing any css including through Since I had Can someone explain me what this sideEffects is doing, why it only affects production builds, and why it was working with Babel v6 and not with babel v7 ? |
@sokra I just hit this issue in 4.27.1. It was also reproducable in https://github.com/brycehill/webpack-css-order with the upgraded webpack version. Because of this I can't set |
I'm also seeing this in 4.27.1 and 4.28.3. |
I'm seeing this in 4.29. When I remove the 'sideEffects' property from one my vendor libraries the css order between production and dev are the same but when the property is included they are not. |
@sokra Did this get deliberately reverted, or can the same fix just be reapplied to master? |
v4.30.0 - broken css order when you import your css files in js |
Attention, as before, when switching from one mode to another, for some strange reason, order gets lost! |
When import file from node_modules which has declared I have created a test repo: https://github.com/Javey/webpack-tree-shaking-demo |
This issue had no activity for at least three months. It's subject to automatic issue closing if there is no activity in the next 15 days. |
@webpack-bot please don't. This is an important issue and I'm disappointed we still haven't been able to solve without copious amounts of "!important" rules. |
@darylteo don't worry we check all PRs what bot closed |
It's pretty obvious, but might help someone. Hi guys. I'm not sure, that what i'm going to say is fully applicable to covered situation, but i had the same issue. The reason was obvious and i am a bit shy about not figuring out this in first minutes of investigation:
All i had to do is switch order for last two guys. |
The "sideEffects" property in package.json improves webpack treeshaking. However that causes the out-of-order CSS we were seeing in production builds. We may enable again once the bug is fixed. For more context see webpack/webpack#7094 and webpack-contrib/mini-css-extract-plugin#202.
The "sideEffects" property in package.json improves webpack treeshaking. However that causes the out-of-order CSS we were seeing in production builds. We may enable again once the bug is fixed. For more context see webpack/webpack#7094 and webpack-contrib/mini-css-extract-plugin#202.
Does it mean that I have to mark all JS files that import CSS directly or indirectly as having side effects? |
Note, that completely broke my build. Console error:
I was trying this as a workaround suggested here: vuetifyjs/vuetify#13732 |
Fwiw, I had been using |
I got around this issue by adding Edit: Without modifying sideEffects, it actually seems to be producing class names in the exact opposite order that I would expect. |
I created an issue in CSS files are concatenated in the order they're imported. Webpack seems to reorder imports of side effect free modules. We're supposed to counteract it with the However, it seems like
|
@alexander-akait Greetings! Is there currently any way to solve the problem with the order of styles when using tree shaking? My case:
When you enable tree shaking in webpack (specify sideEffects: ["/.css", "/.scss"]), problems with css formation begin, namely, the order of modules entering the final css file ceases to be predictable and logical.
With tree shaking turned off, we will have the following final css file:
That is, everything will work as expected, first come the styles of the Title component, then the styles of the Content component. The passed class from Content to Title will be below, so styles can be easily redefined. However, when tree shaking is active, the style order will be the opposite of what we expect, i.e.:
In this case, the styles from the Content will not be able to override the styles of the Title, respectively. |
Yeah, it is a bug inside webpack, I don't look deeply at this right now, so no solution right now, you can try to solve it on the CSS side, i.e. use css specificity, I know it is not a perfect solution, also you can try to use CSS modules |
@alexander-akait I'd like to fix it myself if possible, would you accept a PR? I haven't done any contributions to webpack yet though, what do you think is the scope of this issue? Any suggestions as to where I should start investigating? webpack/lib/buildChunkGraph.js Line 792 in 6ccd531
|
@kosciolek I'm afraid this is not an easy task, you can start with adding failed test cases and then we will look how to fix it |
One of the commits in 5.90.2 seems to have fixed it, at least for the reproduction I made above. However, our larger project still has this problem, but it's likely it's something on our side. |
@kosciolek Yeah, we can also have some edge cases, so, if you can provide an example I will investigate it
Release will be today/tomorrow, need to finish one small thing |
This is the commit in 5.90.2 that fixed the issue in the reproduction:
Adding these 3 removed lines back breaks the repro again: a2ce375#diff-926fcfc6c89e458cc222fa6b30ac4150fca4140e70522c08fa661b005da45b48L102 |
@alexander-akait I made a PR with a failed test case As stated above, after 5.90.2, the reproduction I linked above is no longer applicable (apparently it's fixed), but now there's another problem when optimizing reexports of modules that import side-effectful modules. Commenting this line out webpack/lib/optimize/SideEffectsFlagPlugin.js Line 341 in 6ab9bda
|
Any update on this? I have this problem as well. The uikit I'm using has {"sideEffects": [".css", ".scss"],} in its package.json. Code:
Css build result:
|
One solution to avoid this problem as a component library author is to add all styles in a component library package to a CSS layer. In this way, the styles in the consuming application always take precedence over the styles in the library package no matter what the order of appearance is. But keep in mind that style rules with less specificity will have precedence as well. So if you have some sort of CSS reset in addition to the library package, you might want to add this to a CSS layer as well and define the order early in the application. |
I just stumbled a similar behaviour. Setting graph TD
subgraph "No Barrel ✅"
A3["@applications/base/src/index.ts preOrder: 0, postOrder: 6"]
B3["@libraries/teaser/src/teaser.ts preOrder: 1, postOrder: 5"]
E3["@segments/carousel/src/buttons.ts preOrder: 2, postOrder: 2"]
F3["@segments/carousel/src/button.module.css preOrder: 3, postOrder: 1"]
G3["button.module.css|0|||}} preOrder: 4, postOrder: 0"]
H3["@libraries/teaser/src/teaser.module.css preOrder: 5, postOrder: 4"]
I3["teaser.module.css|0|||}} preOrder: 6, postOrder: 3"]
A3 --> B3
B3 --> E3
E3 --> F3
F3 --> G3
B3 --> H3
H3 --> I3
style A3 fill:#0a0a4a,stroke:#333
style F3 fill:#294b51,stroke:#333
style G3 fill:#294b51,stroke:#333
style H3 fill:#294b51,stroke:#333
style I3 fill:#294b51,stroke:#333
end
subgraph "sideEffects: true ✅"
A2["@applications/base/src/index.ts preOrder: 0, postOrder: 8"]
B2["@libraries/teaser/src/index.ts preOrder: 1, postOrder: 7"]
C2["@libraries/teaser/src/teaser.ts preOrder: 2, postOrder: 6"]
D2["@segments/carousel/src/index.ts preOrder: 3, postOrder: 3"]
E2["@segments/carousel/src/buttons.ts preOrder: 4, postOrder: 2"]
F2["@segments/carousel/src/button.module.css preOrder: 5, postOrder: 1"]
G2["button.module.css|0|||}} preOrder: 6, postOrder: 0"]
H2["@libraries/teaser/src/teaser.module.css preOrder: 7, postOrder: 5"]
I2["teaser.module.css|0|||}} preOrder: 8, postOrder: 4"]
A2 --> B2
B2 --> C2
C2 --> D2
D2 --> E2
E2 --> F2
F2 --> G2
C2 --> H2
H2 --> I2
style A2 fill:#0a0a4a,stroke:#333
style F2 fill:#294b51,stroke:#333
style G2 fill:#294b51,stroke:#333
style H2 fill:#294b51,stroke:#333
style I2 fill:#294b51,stroke:#333
end
subgraph "sideEffects: false ❌"
A1["@applications/base/src/index.ts preOrder: 0, postOrder: 6"]
B1["@libraries/teaser/src/teaser.ts preOrder: 1, postOrder: 5"]
C1["@libraries/teaser/src/teaser.module.css preOrder: 2, postOrder: 1"]
D1["teaser.module.css|0|||}} preOrder: 3, postOrder: 0"]
E1["@segments/carousel/src/buttons.ts preOrder: 4, postOrder: 4"]
F1["@segments/carousel/src/button.module.css preOrder: 5, postOrder: 3"]
G1["button.module.css|0|||}} preOrder: 6, postOrder: 2"]
A1 --> B1
B1 --> C1
C1 --> D1
B1 --> E1
E1 --> F1
F1 --> G1
style A1 fill:#0a0a4a,stroke:#333
style C1 fill:#294b51,stroke:#333
style D1 fill:#294b51,stroke:#333
style F1 fill:#294b51,stroke:#333
style G1 fill:#294b51,stroke:#333
end
it causes the css to be in the wrong order but I am not 100% sure if it's exactly the same issue therefore I opened a new issue: #18961 |
Close in favor - #18961 |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
CSS seems to be out of order in "development" vs "production" mode. It appears somehow related to
sideEffects
and tree shaking.If the current behavior is a bug, please provide the steps to reproduce.
Here's a repo demonstrating the issue: https://github.com/brycehill/webpack-css-order.
There are instructions in the readme. Basically, if you build in
development
mode, the outputted CSS is different fromproduction
mode.What is the expected behavior?
CSS order is consistent from one environment to another.
I don't want to depend on a specific CSS order, but I do expect it to be consistent from when I develop to when I build for production.
If this is a feature request, what is motivation or use case for changing the behavior?
N/A
Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.
Node: v8.9.1
Webpack: v4.6.0
OS: MacOS High Sierra
The text was updated successfully, but these errors were encountered: