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-critical-css

v0.1.3

Published

Grunt task to extract Above the Fold CSS for a URL

Downloads

30

Readme

grunt-critical-css

Build Status

Grunt task using critical-css to extract Above the Fold styles from URLs.

Getting Started

This plugin requires Grunt ~0.4.5

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-critical-css --save-dev

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

grunt.loadNpmTasks('grunt-critical-css');

The "critical_css" task

Overview

In your project's Gruntfile, add a section named critical_css to the data object passed into grunt.initConfig().

grunt.initConfig({
  critical_css: {
    options: {
      width: 1200,
      height: 900,
    },
    your_target: {
      url: 'http://localhost:3000/', // Parse this page.
      dest: 'dist/critical.css',     // Path to save critical css to.
    },
  },
});

Options

options.width

Type: Integer Default value: 1200

The width of the viewport used in the browser. Used to determine what is "above the fold", i.e what is visible during rendering the page initially.

options.height

Type: Integer Default value: 900

The height of the viewport used in the browser. Used to determine what is "above the fold", i.e what is visible during rendering the page initially.

options.excludeSelectors

Type: Array Default value: []

An array of CSS selectors or basically any pattern. These are matched against every individual style declaration and if the patterns provided here match the style rule is discarded from the output.

options.enabledOrigins

Type: Array Default value: []

An array of host names to serve as a whitelist where CSS can originate from. Any CSSRuleList objects with parentStyleSheet.href not having this host name are excluded from the critical CSS.

This can be useful to exclude certain styles supplied by 3rd party widgets that are loaded asynchronously anyways.

options.keepInlineStyles

Type: Boolean Default value: false

Controls whether non-external styles should be included. These are usually rules which are already inlined or are set by JavaScript. These are excluded by default.

options.ignoreConsole

Type: Boolean Default value: true

Controls console output from the headless browser should be added to the output. Useful for debugging purposes.

options.maxBuffer

Type: Integer Default value: 819200

Sets the output buffer for the child process.

Usage Examples

Default Options

In this example, the default options are used to generate the critical CSS from a development server. The generated stylesheet is then saved to a file under the specified path.

grunt.initConfig({
  critical_css: {
    dist: {
      url: 'http://localhost:3000/'
      file: 'dist/critical.css'
    },
  },
});

Custom Options, multiple targets

In this example, custom options are used to fetch from two different URLs whilst sharing some custom options.

grunt.initConfig({
  options: {
    excludeSelectors: [
      'html, body, div' // Prevent the CSS reset from appearing inline.
    ],
    enabledOrigins: [
      'localhost'       // Only include locally hosted styles.
    ]
  }
  desktop: {
    options: {
      width: 1200,
      height: 760,
    },
    url: 'http://www.example.com/'
    file: 'dist/critical-desktop.css'
  },
  mobile: {
    options: {
      width: 400,
      height: 800,
    },
    url: 'http://www.example.com/'
    file: 'dist/critical-mobile.css'
  },
});

Pro tip: You can add the grunt-inline plugin to your build pipeline for embedding the resulting critical stylesheets in your html file or template.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Changelog

  • 0.1.1 - Initial release

License

Copyright (c) 2015 Attila Beregszaszi Licensed under the MIT license.

Plug

Development was sponsored by Front Seed Labs and Dennis Publishing