Skip to content

Grunt task to extract & inline critical-path CSS from HTML

License

Notifications You must be signed in to change notification settings

poltavchenko/grunt-critical

 
 

Repository files navigation

grunt-critical

Grunt plugin to extract & inline critical-path CSS from HTML

NPM version Build Status Build Status: Windows Dependency Status

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

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

grunt.loadNpmTasks('grunt-critical');

Critical task

Run this task with the grunt critical command.

Task targets, files and options may be specified according to the grunt Configuring tasks guide.

Generate critical path css and inline it with critical.

Usage

Use the grunt-critical task by specifying a target destination (file) for your critical CSS. Below this is test/generated/critical.css.

Along-side, specify the input HTML file you would like scanned as well as the width and height of the critical viewport. In this case test/fixture/index.html.

critical: {
    test: {
        options: {
            base: './',
            css: [
                'test/fixture/styles/main.css',
                'test/fixture/styles/bootstrap.css'
            ],
            width: 320,
            height: 70
        },
        src: 'test/fixture/index.html',
        dest: 'test/generated/critical.css'
    }
}

If the dest file is a stylesheet, the resulting critical-path css is saved to this file for later use. If dest points to an Markup file (HTML, PHP, etc.) the resulting CSS gets inlined and the exiting stylesheets are wrapped in a javascript function to load them asynchronously as well as a noscript block for users with JavaScript disabled

critical: {
    test: {
        options: {
            base: './',
            css: [
                'test/fixture/styles/main.css',
                'test/fixture/styles/bootstrap.css'
            ],
            width: 320,
            height: 70
        },
        src: 'test/fixture/index.html',
        dest: 'test/generated/index-critical.html'
    }
}

Options

For a full list of options see critical

License

(C) Ben Zörb 2014, released under an MIT license

About

Grunt task to extract & inline critical-path CSS from HTML

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HTML 78.8%
  • JavaScript 20.4%
  • CSS 0.8%