Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@lodder/grunt-postcss

C-Lodder57.8kMIT3.1.1

Apply several post-processors to your CSS using PostCSS

gruntplugin, postcss-runner, css, postprocessor, postcss

readme

grunt-postcss

npm Version Build Status

Apply several post-processors to your CSS using PostCSS.

Getting Started

This plugin requires Grunt 1.0.3 or above.

Note: As of v3.0.0, Node.js 10.x or above is required.

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 i --save-dev postcss @lodder/grunt-postcss

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

grunt.loadNpmTasks('@lodder/grunt-postcss');

Usage

npm i @lodder/grunt-postcss autoprefixer cssnano
grunt.initConfig({
  postcss: {
    options: {
      map: true, // inline sourcemaps

      // or
      map: {
          inline: false, // save all sourcemaps as separate files...
          annotation: 'dist/css/maps/' // ...to the specified directory
      },

      processors: [
        require('autoprefixer')(),
        require('cssnano')() // minify the result
      ]
    },
    dist: {
      src: 'css/*.css'
    }
  }
});

Options

Post-processors options

require('postcss-plugin')({option: value})

Plugin options

options.processors

  • Type: Array|Function
  • Default value: []

An array of PostCSS compatible post-processors. You can also use a function that returns an array of PostCSS post-processors.

options.map

  • Type: Boolean|Object
  • Default value: false

If the map option isn't defined or is set to false, PostCSS won't create or update sourcemaps.

If true is specified, PostCSS will try to locate a sourcemap from a previous compilation step using an annotation comment (e.g. from Sass) and create a new sourcemap based on the found one (or just create a new inlined sourcemap). The created sourcemap can be either a separate file or an inlined map depending on what the previous sourcemap was.

You can gain more control over sourcemap generation by assigning an object to the map option:

  • prev (string or false): a path to a directory where a previous sourcemap is (e.g. path/). By default, PostCSS will try to find a previous sourcemap using a path from the annotation comment (or using the annotation comment itself if the map is inlined). You can also set this option to false to delete the previous sourcemap.
  • inline (boolean): whether a sourcemap will be inlined or not. By default, it will be the same as a previous sourcemap or inlined.
  • annotation (boolean or string): by default, PostCSS will always add annotation comments with a path to a sourcemap file unless it is inlined or the input CSS does not have an annotation comment. PostCSS presumes that you want to save sourcemaps next to your output CSS files, but you can override this behavior and set a path to a directory as a string value for the option.
  • sourcesContent (boolean): whether original file contents (e.g. Sass sources) will be included to a sourcemap. By default, it's true unless a sourcemap from a previous compilation step has the original contents missing.

options.diff

  • Type: Boolean|String
  • Default value: false

Set it to true if you want to get a patch file:

options: {
  diff: true // or 'custom/path/to/file.css.patch'
}

You can also specify a path where you want the file to be saved.

options.sequential

  • Type: Boolean
  • Default value: false

By default grunt-postcss will load all passed CSS files and immediately process them. Set this to true if you want files to be processed one by one. This can help in case when you have a lot of CSS files and processing them causes an out of memory error.

options.failOnError

  • Type: Boolean
  • Default value: false

Set it to true if you want grunt to exit with an error on detecting a warning or error.

options.writeDest

  • Type: Boolean
  • Default value: true

Set it to false if you do not want the destination files to be written. This does not affect the processing of the map and diff options.

options.onError

  • Type: Function
  • Default value: null

This function is called when an error occurs and passes the error data.

options.syntax, options.parser, options.stringifier

Options to control PostCSS custom syntaxes.

options: {
  parser: require('postcss-safe-parser') // instead of a removed `safe` option
}
options: {
  syntax: require('postcss-scss') // work with SCSS directly
}

Why would I use this?

Unlike the traditional approach with separate plugins, grunt-postcss allows you to parse and save CSS only once applying all post-processors in memory and thus reducing your build time. PostCSS is also a simple tool for writing your own CSS post-processors.

How to migrate from grunt-autoprefixer?

Autoprefixer is a PostCSS plugin, so first replace grunt-autoprefixer with grunt-postcss and autoprefixer plugin.

npm remove --save-dev grunt-autoprefixer
npm install --save-dev @lodder/grunt-postcss autoprefixer

Assuming you have a config like this:

autoprefixer: {
  options: {
    map: true,
    browsers: ['last 1 version']
  },
  dist: {
    src: '...'
  }
}

Replace it with:

postcss: {
  options: {
    map: true,
    processors: [
      require('autoprefixer')()
    ]
  },
  dist: {
    src: '...'
  }
}

And add the browsers to either your package.json or .browserslistrc file.

browsers, cascade and remove options are plugin-specific, so we pass them as an argument while require the plugin.

changelog

v3.1.1 date: 03-11-2021 changes:

  - Switch to picocolors
  - Code improvements

v3.1.0 date: 11-10-2021 changes:

  - Move to the grunt logging API
  - General code improvements

v3.0.1 date: 13-04-2021 changes:

  - Fix ability to catch grunt errors

v3.0.0 date: 19-09-2020 changes:

  - Updated to PostCSS 8 (Note PostCSS is now a peerDependency you must install yourself)
  - Drop support for NodeJS 8, 11 and 13

v2.0.4 date: 12-05-2020 changes:

  - Updated PostCSS to 7.0.31
  - Fix logging of source map output (thanks @jorrit)

v2.0.3 date: 12-05-2020 changes:

  - Updated PostCSS to 7.0.30
  - Remove kleur dependency
  - Fix some logs not showing

v2.0.2 date: 15-04-2020 changes:

  - Updated PostCSS to 7.0.27
  - Updated diff to 4.0.2

v2.0.1 date: 17-12-2019 changes:

  - Updated PostCSS to 7.0.24

v2.0.0 date: 08-11-2019 changes:

  - Updated PostCSS to 7.0.21
  - Updated kleur to 3.0.3
  - Requires Node 8.x or above

v1.0.9 date: 05-07-2019 changes:

  - Updated Grunt to 1.0.4
  - Updated time-grunt to 4.0.0
  - Updated load-grunt-tasks to 5.0.0
  - Updated autoprefixer example to prevent deprecated warning

v1.0.8 date: 13-02-2019 changes:

  - Updated PostCSS to 7.0.14
  - Updated Kleur to 3.0.2
  - Updated cssnano to 4.1.9

v1.0.7 date: 18-01-2019 changes:

  - Updated PostCSS to 7.0.13

v1.0.6 date: 14-01-2019 changes:

  - Updated PostCSS to 7.0.11
  - Replaced Chalk dependency with Kleur

v1.0.5 date: 08-01-2019 changes:

  - Updated dependencies. Mainly postcss to v7.0.8

v1.0.4 date: 02-01-2019 changes:

  - Fix source file size display (thanks @axten)

v1.0.3 date: 05-12-2018 changes:

  - Add support to synchronously process CSS files (thanks @VitaliyR)
  - Fix documentation (thanks @jorrit)

v1.0.0 - v1.0.2 date: 22-11-2018 changes:

  - PostCSS 7.0.6
  - Drop NodeJS 0.x and 4.x

v0.9.0 date: 10-09-2017 changes:

  - PostCSS 6.0
  - Drop nodejs 0.12 support
  - `processors` option as a function (PR #99)

v0.8.0 date: 03-03-2016 changes:

  - Drop nodejs 0.10 support

v0.7.2 date: 16-02-2016 changes:

  - Update Grunt dependency (#pull/86)

v0.7.1 date: 06-11-2015 changes:

  - Fix #70

v0.7.0 date: 21-10-2015 changes:

  - New `failOnError` option
  - New `writeDest` option
  - Better npm@3 flat dependencies compatibility

v0.6.0 date: 23-08-2015 changes:

  - PostCSS 5.0

v0.5.5 date: 12-07-2015 changes:

  - Handle async PostCSS API properly

v0.5.4 date: 28-06-2015 changes:

  - Fix annotation URL for Windows

v0.5.3 date: 25-06-2015 changes:

  - Fix annotation paths

v0.5.2 date: 20-06-2015 changes:

  - Fix `annotation` option

v0.5.1 date: 10-06-2015 changes:

  - Fix process hanging when no source files found

v0.5.0 date: 07-06-2015 changes:

  - PostCSS `safe` option support
  - Less verbose output in standard mode, `silent` option is removed
  - Log no source files found, not fail

v0.4.0 date: 04-05-2015 changes:

  - Pass PostCSS Runner Guidelines

v0.3.0: date: 18-01-2015 changes:

  - PostCSS 4.0
  - Use a new PostCSS instance for each Grunt target (#12)

v0.2.0: date: 14-11-2014 changes:

  - PostCSS 3.0
  - Maps now inline and containing sourcesContent by default

v0.1.0: date: 25-09-2014 changes:

  - First release