postcss-only-directive

0.1.1 • Public • Published

PostCSS Only Directive Build Status

This plugin is designed to help you write CSS for a component in one file, and then split the rules up into separate files based on your needs.

A simple use case would be for creating separate IE stylesheets. Another good use case would be splitting up rules by media query.

Directive Syntax - Example

We start with a file that has been marked up with @only directives:

/* _component.scss */
 
.button {
  background: blue;
  @only(ie) { content: 'ie only'; }
}
 
@only(medium{
  @media(min-width: 500px) {
    .button { background: green; }
  }
}
 
@only(large{
  @media(min-width: 900px) {
    .button { background: red; }
  }
}

Then we call the @onlyRender directive at the top of each file to specify what should be included.

By name

/* ie.scss */
@onlyRender(ie);
@import 'component'; // inlines the css
/* ie.css */
.button { content: 'ie only'; }

Multiple names

/* medium-and-up.scss */
@onlyRender(medium, large);
@import 'component'; // inlines the css
/* medium-and-up.css */
@media(min-width: 500px{
  .button { background: green; }
}
 
@media(min-width: 900px{
  .button { background: red; }
}

:root

/* small.scss */
@onlyRender(:root);
@import 'component'; // inlines the css
/* small.css */
.button { background: blue; }

:all

/* app.scss */
@onlyRender(:all);
@import 'component'; // inlines the css
/* app.css */
.button {
  background: blue;
  content: 'ie only';
}
 
@media(min-width: 500px{
  .button { background: green; }
}
 
@media(min-width: 900px{
  .button { background: red; }
}

Config

postcss([ require('postcss-only-directive')({ whitelist: [] }) ])

See PostCSS docs for examples for your environment.

Whitelist

The whitelist is a list of strings specifying which @only directives will be supported. Any rules not in a whitelist will be rolled into :root by default.

Example

Suppose I'm splitting a file out for IE:

/* button.css */
.button { background: blue; }
@only(ie11{ .button { background: green; } }
 
/* main.css */
@onlyRender(:root);
@import 'button';
/* ie11.css */
@onlyRender(ie11);
@import 'button';

Now pretend that someone comes along later and adds an @only(ie10) rule - not realizing that no one has created a matching call to @onlyRender(ie10). Their rules will be removed from our stylesheets silently!

The whitelist is here to save us from that. Any rules that aren't in the whitelist will be automatically rolled up into the special :root keyword, avoiding lossy changes.

Motivation and inspiration

There has been a lot of talk about ways to accomplish this over on the sass project on github. There was a lot of discussion on https://github.com/sass/sass/issues/241, and then @meefox proposed the @only directive in https://github.com/sass/sass/issues/1187.

There are some other postcss plugins that do similar things:

These generally didn't fit my needs because they emit files outside of the normal build pipeline. These files have to be manually minified, gzipped, digested / etc.

Complicating my pipeline like that wasn't an option for me, so I chose this approach. The tradeoff is that you must specify the files ahead of time. Other media-query splitters can dynamically generate files based on the CSS itself - this plugin does not give you that option.

Package Sidebar

Install

npm i postcss-only-directive

Weekly Downloads

0

Version

0.1.1

License

MIT

Unpacked Size

14.9 kB

Total Files

9

Last publish

Collaborators

  • wheeyls