Skip to content
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

Options to split by rules instead of size? #16

Open
lsycxyj opened this issue Jul 16, 2017 · 2 comments
Open

Options to split by rules instead of size? #16

lsycxyj opened this issue Jul 16, 2017 · 2 comments

Comments

@lsycxyj
Copy link

lsycxyj commented Jul 16, 2017

There may be quite a few rules only used in certain circumstances. (eg. IE X specific rules, media queries etc.). They shouldn't be split by size but rules. Is it possible or any other ways to achieve this?

@mendedwraith
Copy link

I recently forked this project and added the ability split CSS files by comment. It works similarly to webpack magic comments. When I have time, I'll see about adding support for media queries. However, you could simulate the behavior using the current version.

For example:
styles.css

/*! split:ie8 */
.box-shadow {
  ...
}

/*! split:ie9 */
.flexbox-row {
  ...
}

/*! split:mobile  */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2) {
  .mobile {
   ...
  }
}

Will be split into three separate chunks with the names denoted in the comment:
styles-ie8.css, styles-ie9.css and styles-mobile.css

I tested out every other CSS splitting plugin I could find and none of them seemed to offer this somewhat obvious feature. The main motivation being to split vendor styles (compiled from SASS) from app-specific styles.

https://github.com/jeremiahmmartinez/css-magic-split-webpack-plugin
https://www.npmjs.com/package/css-magic-split-webpack-plugin

@SassNinja
Copy link

SassNinja commented May 24, 2018

Almost half a year later and there still doesn't seem to be a webpack plugin for this :(
I had a meeting with Google and they recommended to split the CSS media queries into separate files. Thus I'm quite surprised there are hardly any solutions for it out there.

@jeremiahmmartinez you haven't found another solution than your fork, have you?

I did try it but the problem with your feature is

  1. it requires to add magic comments what is not that easy because my media query wrappers are generated by a framework
  2. it extracts all the code below – I however only want to extract the media query and not everything else below it
  3. I need more control over the emitted filename (e.g. small.css or small-landscape.css)

I've found some promising postcss plugins such as css-mqpacker which lets me concat my media queries.
But I wasn't able yet to emit as files with specific naming afterwards.
Wish there would be an all-in-one solution for extracting media queries from a css chunk...

UPDATE

In case someone is reading this: I've created an own webpack plugin for extracting media queries from a css chunk
https://github.com/SassNinja/media-query-plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants