[webpack v3.0.0] generates CSS in wrong order
Do you want to request a feature or report a bug? bug
What is the current behavior?
When using extract-text-webpack-plugin with webpack3 to generate stylesheet file, the content's order doesn't obey source code's order. Use create-react-app boilerplate to explain this :
// App.js
import React, { Component } from "react";
import Block from "./Block";
import "./App.css";
class App extends Component {
render() {
return (
<div className="App">
<Block />
</div>
);
}
}
export default App;
//Block.js
import React from "react";
import "./block.css";
export default () => <div className="block">Content</div>;
// App.css
.App {
text-align: center;
}
// block.css
.block {
font-size: 16px;
}
Got :
.App {
text-align: center;
}.block {
font-size: 16px;
}
/*# sourceMappingURL=main.f2db6320.css.map*/
I'm not sure if this problem is webpack3 or extract-text-webpack-plugin's problem, since I can get correct result in webpack2.6.1, I think I should post in here first.
If the current behavior is a bug, please provide the steps to reproduce.
The complete project : https://github.com/cyl19910101/wp3_test
What is the expected behavior? Expected stylesheet order :
.block {
font-size: 16px;
}.App {
text-align: center;
}
If this is a feature request, what is motivation or use case for changing the behavior?
Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
OS :
ProductName: Mac OS X
ProductVersion: 10.12.5
BuildVersion: 16F73
Node :
v8.1.2
Webpack :
3.0.0
This issue was moved from webpack/webpack#5106 by @sokra. Orginal issue was by @cyl19910101.
extractedChunk.modules.sort is no longer valid, as extractedChunk.modules is now a getter to extractedChunk._modules which is a Set.
Haaaaaaaa I'm not alone.
I'm playing with ignoreOrder but it doesn't change anything.
Imagine i've got:
import 'antd/dist/antd.css'
import '~/styles/index.css'
Once it's compiled (it's random), I've got the result like:
import '~/styles/index.css'
import 'antd/dist/antd.css'
which is wrong for me as both define font-size for body, for example.
Why is a bug set to semver: Major?
Please stop assigning the entire org team to issues. This is exactly what @evilebottnawi was talking about in slack when he said it makes things confusing.
Everyone has a personal projects board, when someone picks up an issue for work they will add it to their personal projects board.
@cyl19910101 - Can you put that test project back up so I can get this issue sorted? Thanks :)
@filipesilva FYI this should probably be added to your blocking scope hoisting list in angular/angular-cli#6365
@d3viant0ne that test project is still up: https://github.com/cyl19910101/wp3_test
any import statements bringing in css files are not output in the order of the imports
Thanks, the link in the migrated issue routes to a 404.
Thanks for the heads up @d3viant0ne, much appreciated 👍
Update for those having this issue. We are adding a module sort function into webpack, the update on this end will be simple. I'll update the thread again when I have more information.
@d3viant0ne keep up the good work!
@d3viant0ne I tested on Angular CLI and it doesn't affect our projects since they only use extract-text-webpack-plugin for global styles. Neither importing css from within css, nor adding an array of css files to an entry point seem to be affected.
@filipesilva - It isn't happening in Angular projects from what I can tell. It's not an issue in my CLI projects or my boilerplate based versions unless I start doing silly things with global styles that you shouldn't do in Angular anyway.
In my context, I mostly use CSS Modules so I don't mind.
…except for global styles.
ant.design has a style for html, body, a. I wanted to override it so I do the same in a specific file. But if order isn't kept, ant.design takes the advantage on my own style. That's the problem.
My configuration:
// styles
{
test: /\.css$/,
exclude: /\.module\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
modules: false,
importLoaders: 1,
import: true,
alias: {
'~': `${SRC_DIR}`
}
}
},
{
loader: 'postcss-loader'
}
]
})
},
{
test: /\.module\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
modules: true,
importLoaders: 1,
localIdentName: '[name]--[local]___[hash:base64:5]',
import: true,
alias: {
'~': `${SRC_DIR}`
}
}
},
{
loader: 'postcss-loader'
}
]
})
},
@kud Actually I used CSS Modules too, but in one situation the order does matter, as you mentioned, override.
Let's say we have one stylesheet file common.css which has some shared style like :
.full-page {
height : 100vh;
width : 100vw;
padding : 20px;
}
And a component with this style and also accept other className from props :
// use React CSS Modules to handle styleName
function FullPage ({className,children}) {
return (
<div styleName="full-page" className={className}>
{children}
</div>
)
}
We have another stylesheet file ad.css for specific page :
.ad-page {
padding-left : 320px;
}
Then we can reuse the FullPage like :
function ADPage({children}) {
return (
<FullPage styleName="ad-page">
{children}
</FullPage>
)
}
However, if the bundled stylesheet file put .ad-page in front of .full-page, it will get incorrect padding-left value.
We're seeing this too. We have exactly that situation that @cyl19910101 mentioned.
What is the status of this issue? This is beginning critical, and I would really like to avoid to downgrade webpack
@phun-ky - It's going to require landing a feature in Webpack.
Realistically, even if it landed right now, you would still be waiting on the next patch release. At the absolute earliest, it's going to be this weekend.
This issue is on the top of the list for the org-maintainers, the solution has already been discussed with Tobias on the Webpack side. We will get it done & in as soon as humanly possible.
Associated issue in Webpack is webpack/webpack#5204
So, just poking around on this.. so i tried modifying this line https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/master/index.js#L272 to:
chunk.modules.slice().reverse()
It outputs the css in the correct order this way, but I suspect there's more to it than just that. @d3viant0ne thoughts?
Btw all tests pass with this change - If sounds ok ill make pr.
Chunk.modules : Array<Module> was changed to Chunk._modules : Set<Module> in webpack/webpack#4764. Chunk.modules is a deprecated getter/setter.
ETWP calls sort() on Chunk.modules which doesn't work because it's a copy of the original Set.
What you are suggesting may work around the issue for those that are having this issue with a side effect of breaking the sort for everyone else.
The proposed solution is to add Chunk.sortModules(fn) so ETWP can sort without needing to understand all the module internals.