Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

postcss-only-directive

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-only-directive

PostCSS plugin Allows you to easily create entrypoint files which contain only certain rules.

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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.

Keywords

FAQs

Package last updated on 04 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc