npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postcss-only-directive

v0.1.1

Published

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

Downloads

4

Readme

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:

  • https://www.npmjs.com/package/postcss-extract-media-query
  • https://www.npmjs.com/package/postcss-critical-split

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.