gulpfile autoprefixer必须更改为postcss

时间:2018-01-29 17:07:47

标签: gulp postcss autoprefixer gulp-autoprefixer

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

但是因为我只使用Quench生成器获得了这个gulpfile,所以我不知道如何更改它,因此autoprefixer将再次运行。谁能详细告诉我如何改变这个?

我的gulpfile是:

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"
  }
}

1 个答案:

答案 0 :(得分:1)

安装这些软件包

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}))
});

这应该可以解决问题,如果不是,我会再次看一遍