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
CSSO not restructuring unless media queries are sequencially close to each other in the file. #350
Comments
It's a valid behaviour, not a bug. In the first example moving media down is unsafe, since it may change the meaning of your CSS. Let's imaging |
Correct me if I'm wrong (and it very well may be the case since I'm relatively new to web development) but what you are saying is that if the first media were to move down with the other queries, it would change the meaning of the code, since, if they applied to the same element, the media would take precedence over the other rule due to CSS cascading rules, correct? But isn't that intended behavior with media queries anyway? Do we not always want them taking precedence over whatever isn't inside a media query? If we author something in a media query to change some element but the general rule of that element always take precedence over the more specific media query, then there was no point having that query that in the first place, correct? Doesn't in the specific case of media queries, always moving them down(and thus having priority in the cascading ruling) with other queries, make sense? |
If this behavior, by default, might be too opinionated and take too much control out of developer hands (I mean, maybe they want to have a media query that does nothing?). Maybe a "optimizeMediaQueries" option parameter could work? (That is, of course, if what I said made sense). |
That's may be correct in your case, but wrong in general. Possible problems are described in node-css-mqpacker: https://github.com/hail2u/node-css-mqpacker#notes
Yep, I thought about such option. Not sure it's a good idea. At the same time, this can eliminate the need to use additional transformers like We can turn this issue into a feature request to implement a new option for unsafe media moving. |
`mergeAtrule` goes first now - lift `@keyframes` to the beginning of stylesheet (chunk), but after charset and import rules - remove duplicate (with the same name) keyframes (keep last one) - new option `forceMediaMerge` to force media rules merging (#350)
With the new |
Or perhaps we should wait till this feature is in a release. |
Yep, I prefer to wait for the release. |
@carlospnav v3.2.0 addresses the issue. Use |
I have a project in which I have a couple of different modules, each with their own css file. In my build process I use ExtractTextPlugin to extract the css from the bundle and place it in styles.css. I then tried to use CSSO as a webpack plugin to restructure the resulting styles.css in order to merge media queries sentences to avoid duplicating statements. The result is that the CSSO pass seemed to be processing only the individual chunks and not the complete styles.css. However, upon using the CLI tool on that styles.css separately I got the following results:
With this stylesheet:
The resulting minification and restructuring would be this:
Now, if I repeated the process with the following stylesheet:
The resulting minified version would be:
Notice that when the two @media all and (max-width: 100px) statements are close to each other, csso will attempt to restructure it to avoid duplications. The same doesn't happen when they are in different locations within the file, but similarly duplicated. Any work-arounds for this problem? If I start having many modules, this can start adding up to the resulting filesize.
The text was updated successfully, but these errors were encountered: