Git Product home page Git Product logo

grunt-critical-css's Introduction

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

Development

Project led and maintained by Attila Beregszaszi

Development sponsored by Dennis Publishing and Front Seed Labs

grunt-critical-css's People

Contributors

attila avatar

Stargazers

 avatar

Watchers

 avatar  avatar

grunt-critical-css's Issues

JSHint Warning

First of all, thanks for this great project! Really appreciate the effort. It helps to optimise for Google Pagespeed Insights.

One little remark. I get a JSHint Warning for the actual task name: critical_css

Hence I suggest to rename it to criticalCss

My current workaround is this: critical_css: { // jshint ignore: line

just does not work

I had second attempt today to run this plugin

Does not work.

Either return bad argument for destination, or dest not specified.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.