gulpfile autoprefixer 必须更改为 postcss

gulpfile autoprefixer must change to postcss

我最近才遇到这些错误,我的 Gulp 说我需要将我的 gulp-autoprefixer 更改为 postcss。

但是因为我只得到这个 gulp 带有 Quench 生成器的文件,所以我不知道如何更改它以便自动前缀程序可以再次工作。谁能详细告诉我怎么改?

我的gulp文件是:

var gulp = require('gulp'),
plumber = require('gulp-plumber'),
rename = require('gulp-rename');
var autoprefixer = require('gulp-autoprefixer');
var minifycss = require('gulp-minify-css');
var sass = require('gulp-sass');
var browserSync = require('browser-sync');

gulp.task('browser-sync', function() {
  browserSync({
    server: {
       baseDir: "./"
    }
  });
});

gulp.task('bs-reload', function () {
  browserSync.reload();
});


gulp.task('styles', function(){
  gulp.src(['src/css/*.scss'])
    .pipe(plumber({
      errorHandler: function (error) {
        console.log(error.message);
        this.emit('end');
    }}))
    .pipe(sass())
    .pipe(autoprefixer('last 2 versions'))
    .pipe(gulp.dest('assets/css/'))
    .pipe(rename({suffix: '.min'}))
    .pipe(minifycss())
    .pipe(gulp.dest('assets/css/'))
    .pipe(browserSync.reload({stream:true}))
});


gulp.task('default', ['browser-sync'], function(){
  gulp.watch("src/css/*.sass", ['styles']);
  gulp.watch("src/css/*.scss", ['styles']);
  gulp.watch("*.html", ['bs-reload']);
});

我的package.json:

"name": "Quench",
  "version": "1.0.0",
  "description": "A Gulp file and project generator.",
  "main": "gulpfile.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Quench",
  "license": "ISC",
  "devDependencies": {
    "browser-sync": "2.6.5",
    "gulp": "3.8.11",
    "gulp-autoprefixer": "2.1.0",
    "gulp-babel": "5.1.0",
    "gulp-concat": "2.5.2",
    "gulp-minify-css": "1.0.0",
    "gulp-plumber": "1.0.0",
    "gulp-rename": "1.2.2",
    "gulp-sass": "^3.1.0",
    "gulp-uglify": "^3.0.0"
  }
}

安装这些包

npm i --save-dev gulp-postcss autoprefixer cssnano

cssnano 是 minify 的 postcss 版本 更改您的 gulp 代码

var gulp = require('gulp'),
plumber = require('gulp-plumber'),
rename = require('gulp-rename');
//var autoprefixer = require('gulp-autoprefixer'); /*remove*/
//var minifycss = require('gulp-minify-css'); /*remove*/
var postcss = require('gulp-postcss'); /*add*/
var autoprefixer = require('autoprefixer'); /*add*/
var cssnano = require('cssnano'); /*add*/

var sass = require('gulp-sass');
var browserSync = require('browser-sync');

gulp.task('browser-sync', function() {
  browserSync({
    server: {
       baseDir: "./"
    }
  });
});

gulp.task('bs-reload', function () {
  browserSync.reload();
});


gulp.task('styles', function(){
  /*add this*/
  var processors = [
    autoprefixer,
    cssnano
  ];

  gulp.src(['src/css/*.scss'])
    .pipe(plumber({
      errorHandler: function (error) {
        console.log(error.message);
        this.emit('end');
    }}))
    .pipe(sass())
    //.pipe(autoprefixer('last 2 versions')) /*remove*/
    .pipe(postcss(processors)) /*add*/
    .pipe(gulp.dest('assets/css/'))
    .pipe(rename({suffix: '.min'}))
    // .pipe(minifycss()) /*remove*/
    .pipe(gulp.dest('assets/css/'))
    .pipe(browserSync.reload({stream:true}))
});

这应该可以解决问题,否则我会再看一遍