简体   繁体   中英

grunt-autoprefixer to grunt-postcss

The grunt-autoprefixer said "This project has been deprecated in favour of grunt-postcss." So, I want to change it to grunt-postcss. My current setting in Gruntfile.js for grunt-autoprefixer

autoprefixer: {
  options: {
    browsers: ['last 1 version']
  },
  dist: {
    files: [{
      expand: true,
      cwd: '.tmp/styles/',
      src: '{,*/}*.css',
      dest: '.tmp/styles/'
    }]
  }
},

If upgrade to grunt-postcss . How can I write my settings in Gruntfile.js?

I saw the README in grunt-postcss, but I didn't get it. Seems some values cannnot mapping to the new settings for grunt-postcss.

It is done like any other postcss processors. See this for example:

var autoprefixer = require('autoprefixer-core');     
grunt.initConfig({
      postcss: {
        options: {
          processors: [
            autoprefixer({
              browsers: ['> 0.5%', 'last 2 versions']
            }).postcss
          ]
        },
        dist: {
                files: {
                    'dist/': 'css/*.css'
                }
        }
      }
  });

Warning from 2020:

The usage of browsers option in autoprefixer processor has changed.

Now you need to move the browsers option into your package.json file:

Gruntfile.js

...
processors: [
    ...
    // No autoprefixer 'browsers' option
    require('autoprefixer')(),
    ...
]
...

package.json

{
    ...
    // 'browserslist' option at the end of the file just above the last curly brace
    'browserslist': [
        'last 2 versions',
        '> 0.5%'
    ]
}

See: https://github.com/browserslist/browserslist#browserslist-

(This answer I took from the part of my similar answer here: https://stackoverflow.com/a/64079551/12320578 )

An example of working postcss-autoprefixer usage:

Gruntfile.js

module.exports = function (grunt) {
    grunt.initConfig({
        postcss: {
            options: {
                processors: [
                    require('autoprefixer')({grid: 'autoplace'}),
                ],
                map: {
                    inline: false, // save all sourcemaps as separate files...
                    annotation: 'assets/css/' // ...to the specified directory
                }
            },
            dist: {
                files: {
                    // Generated file target location and source location
                    'assets/css/style.min.css': 'assets/css/style.css',
                    'assets/css/info.min.css': 'assets/css/info.css'
                }
                // or:
                /*files: [
                    {
                        expand: true,
                        src: ['assets/css/*.css', '!**/*.min.css'],
                        ext: '.min.css',
                        extDot: 'first'
                    }
                ]*/
            }
        },
        watch: {
            styles: {
                files: ['assets/css/style.css', 'assets/css/info.css'],
                // or:
                /*files: ['assets/css/*.css', '!assets/css/*.min.css'],*/
                tasks: ['postcss']
            }
        }
    });
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-postcss');
};

package.json

{
    ... // Other already existing options
    "browserslist": [
        "last 4 versions",
        "> 0.5%"
    ]
}

See https://gruntjs.com/configuring-tasks#building-the-files-object-dynamically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM