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

grunt-extract-css-media

v0.1.1

Published

grunt task to split css files in html files into separate medie query css files

Downloads

740

Readme

grunt-extract-css-media

A grunt task for extracting media queries out of CSS files and store them in separate files. It uses extract-css-media and issues with the created css itself should be reported here.

With the right options, it can also create html snippet files, which contain the link tags to that extracted CSS files with the right media attributes.

Background

Like you can read on the Google Developer's article about render blocking css, it's good for the loading performance of a web page to load CSS files separately for different media types. Therefore you can specify media attributes on the link tags to let the CSS files be loaded only when they're required. If the media query doesn't match, the CSS gets loaded though but it doesn't block the rendering anymore.

Since it's sometimes complicated to structure your CSS (especially when using sass, where you can write media queries hierarchically) this way, I wrote this grunt plugin, to let these files be generated automatically.

Getting Started

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-extract-css-media -S

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-extract-css-media');

Options

minRules: Number - Default: 20

Gets passed to the extractCssMedia module and specifies a minimum number of rules for a media query to be required to split that media query definitions into an extra string.

The background for that is, that there are sometimes media queries which contain only a little amount of definitions. In that case it often makes sense to keep them in the overall css.

compress: Boolean - Default: true

The parameter gets passed to the extractCssMedia module which itself passes it to css.parse. When set to true, the output CSS gets minified.

note

The compress option doesn't produce a fully minimized css. Like you can already see in the example output, the splitting semicolon for CSS definitions is also added to the last statement of each definition. If you really want to have the smallest css, you should use a real minifier after the media query separation.

snippetDest: String

If the value is set, a snippet html file gets created, which contains link tags with the appropriate media attributes for all created CSS files.

hrefBase: String,

If snippetDest is set, the hrefBaseOption specifies the prefixing path for the created hrefs in the link tags. This can be used, to adjust the CSS urls to your webserver configuration. If it's not set, then hrefs will be the same as specified by the snippetDest option.

Include the created files via grunt

By using the processhtml plugin, you can easily include the created html snippet into your HTML files.

According to the documentation, you can specify an include comment around the css files, which you processed with grunt-extract-css-media and it automatically includes the snippet at this place.

To let the snippet be created, you have to specify the snippetDest option.

Example config

This is a very basic grunt configuration which uses processhtml and extract-css-media.

gruntfile.js

module.exports = function (grunt) {
    grunt.initConfig({
        processhtml: {
            dist: {
                files: {
                    'dest/index.html': ['src/index.html']
                }
            }
        },

        extractCssMedia: {
            dist: {
                options: {
                    minRules: 3,
                    snippetDest: 'dest/snippet/medias.html',
                    hrefBase: './css/'
                },
                files: [{
                    src: ['src/shop.css'],
                    dest: 'dest/css/'
                }]
            }
        }
    })

    grunt.loadNpmTasks('grunt-processhtml')
    grunt.loadNpmTasks('grunt-extract-css-media')

    grunt.registerTask('default', ['extractCssMedia', 'processhtml'])
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>grunt-extract-css-media Example</title>

    <!-- build:include ../dest/snippet/medias.html -->
    <link rel="stylesheet" type="text/css" href="src/style.css" />
    <!-- /build -->
</head>
<body>

</body>
</html>

style.css

body {
    color: green;
}

@media screen and (min-width: 100px) {
    html {
        background: red;    
    }
    
    body {
        background: green;
    }
    
    p {
        background: black;
    }
}

/* will be in style_all.css since the setting says, there are at least 3 rules required for the extraction */
@media screen and (min-width: 200px) {
    html {
        background: yellow;
    }
}

output

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Extract</title>

    <link rel="stylesheet" type="text/css" href="./css/style_screen_and_min_width_100px.css" media="screen and (min-width: 100px)" />
<link rel="stylesheet" type="text/css" href="./css/style_all.css" media="all" />
</head>
<body>

</body>
</html>

Yeah, I know, the indentation breaks, but probably you minimize the html anyways ;)