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-filter-mq

v1.0.1

Published

Filter all matching or non-matching media queries

Downloads

1,690

Readme

PostCSS Filter Media Queries

Return a filtered sub-set of css media queries, useful for creating stylesheets for specific media queries (print, desktop, mobile).

For use with PostCSS or gulp-postcss.


 will it build!?  dependencies

Why?

Your mobile users shouldn't have to download extraneous css. It's a waste of their bandwidth. Use this PostCSS plugin to make your page load faster for them, and decrease their frustration. Also ease your bandwidth.

Assuming a mobile-first coding style, turn code like this:

/* main.css */
.container { background: turquoise; }

@media screen and (min-width: 40em) {
    .container { background: grey; }
}

@media screen and (min-width: 64em) {
    .container { background: white; }
}

in to code like this:

/* mobile-and-up.css
    - serve to all users */

.container { background: turquoise; }
/* desktop.css
    - serve to desktop users only */

@media screen and (min-width: 40em) {
    .container { background: grey; }
}
@media screen and (min-width: 64em) {
    .container { background: white; }
}

or ideally if your server can detect mobile devices, this:

/* mobile-only.css
    - serve to mobile users only */

.container { background: turquoise; }
/* all.css
    - serve to desktop users only */

.container { background: turquoise; }

@media screen and (min-width: 40em) {
    .container { background: grey; }
}

@media screen and (min-width: 64em) {
    .container { background: white; }
}

How?

PostCSS

Install postcss and postcss-filter-mq to your project;

$ npm install --save-dev postcss postcss-filter-mq

Given that you have followed the steps to get PostCSS running in your javascript environment, you should have a file that looks somewhat similar to this:

var postcss = require("postcss"),
    filtermq = require("postcss-filter-mq");

postcss([ filtermq ])
    .process(css, { from: "src/input.css", to: "dist/output.css" })
    .then(function (result) {
        fs.writeFileSync("dist/output.css", result.css);
        if ( result.map ) fs.writeFileSync("dist/output.css.map", result.map);
    });

depending on your needs and file-structure, there will be differences ofcourse. Please refer to https://github.com/postcss/postcss/#js-api for any help getting PostCSS running in your Node env.

Gulp

Install gulp-postcss and postcss-filter-mq to your project:

$ npm install --save-dev gulp-postcss postcss-filter-mq

Then create a task to filter your media queries:

var gulp = require("gulp"),
    rename = require("gulp-rename"),
    postcss = require("gulp-postcss"),
    filtermq = require("postcss-filter-mq");

gulp.task( "css:mq", function () {

    gulp.src("src/input.css")
        .pipe( postcss([ filtermq ]) )
        .pipe( rename( "output.css" ) )
        .pipe( gulp.dest("dist/") );

});

Grunt

It's also possible to use with Grunt, but you'll have to figure that out using the guide on their Github repo.

Options

By default postcss-filter-mq will filter all media queries, this is not usually very useful, and so you'll want to pass options for controlling which media queries are filtered and how.

The default, configurable options are:

var options = {
    regex: /.*/i,           // decides the queries to filter
    invert: false,          // inverts the regex filter result
    keepBaseRules: false    // keep the non-media css rules
};

/*
 * then use in your environment like:
 *     postcss([ filtermq( options ) ])
 */

Examples

Check out the OPTIONS.md file for a more in-depth look at how the options work, or refer to the EXAMPLES.md file for advanced examples on how to use this plugin.

Changelog

Please refer to the release page for the full release history / changelog.

License

Please refer to the LICENSE file for distribution info.