Autoprefixer在Gulpfile.js中不起作用

时间:2016-10-06 05:49:21

标签: javascript html gulp

伙计我在我的gulp文件中添加了Autoprefixer插件一切正常但CSS不会吐出autoprefixed css。我很乐意得到帮助。我是gulpfile.js:

// require gulp
var gulp = require('gulp');

// require other packages
var concat = require('gulp-concat'),
    cssmin = require('gulp-minify-css'),
    rename = require("gulp-rename"),
    sass = require('gulp-sass'),
    browserSync = require('browser-sync'),
    uglify = require('gulp-uglify'),
    changed = require('gulp-changed'),
    imagemin = require('gulp-imagemin'),
    autoprefix = require('gulp-autoprefixer');

// scripts task
gulp.task('scripts', function() {
  return gulp.src('./src/js/*.js')
    .pipe(concat('app.js'))
    .pipe(gulp.dest('./dist/js/'))
    .pipe(uglify())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('./dist/js/'));
});

// styles task
gulp.task('styles', function() {
  return gulp.src('./src/sass/*.scss')
    .pipe(autoprefix('last 2 versions'))
    .pipe(sass())
    .pipe(gulp.dest('./dist/css/'))
    .pipe(cssmin())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('./dist/css/'));
});

// browser sync
gulp.task('browser-sync', ['styles'], function() {
    browserSync({
        server: {
            baseDir: ''
        },
        notify: false
    });
    gulp.watch("./src/sass/*.scss", ['scss']);
    gulp.watch("*.html").on('change', browserSync.reload);
});

// watch task
gulp.task('watch', function() {
  gulp.watch('./src/js/*.js', ['scripts']);
  gulp.watch('./src/sass/*.scss', ['styles']);
});

// image minification
gulp.task('imagemin', function() {
  var imgSrc = './src/img/**/*',
      imgDst = './dist/img';

  gulp.src(imgSrc)
    .pipe(changed(imgDst))
    .pipe(imagemin())
    .pipe(gulp.dest(imgDst));
});

gulp.task('default', ['scripts', 'styles', 'browser-sync', 'imagemin', 'watch']);

我希望你们能帮我解决这个问题:)

2 个答案:

答案 0 :(得分:0)

传递给config的{​​{1}}是错误的。它应该是

autoprefixer

参考:gulp-autoprefixer

答案 1 :(得分:0)

在创建css后尝试init autoprefixer。

Autoprefixer在css中进行更改。它不适用于sass文件。

更新

在复制新gulpfile.js之前,在项目路径中添加您的控制台cmd

// require gulp
var gulp = require('gulp');

// require other packages
var concat = require('gulp-concat'),
    cssmin = require('gulp-minify-css'),
    rename = require("gulp-rename"),
    sass = require('gulp-sass'),
    browserSync = require('browser-sync'),
    uglify = require('gulp-uglify'),
    changed = require('gulp-changed'),
    imagemin = require('gulp-imagemin');
   // autoprefix = require('gulp-autoprefixer'); -- comment this line

// scripts task
gulp.task('scripts', function() {
  return gulp.src('./src/js/*.js')
    .pipe(concat('app.js'))
    .pipe(gulp.dest('./dist/js/'))
    .pipe(uglify())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('./dist/js/'));
});

// styles task
gulp.task('styles', function() {
  return gulp.src('./src/sass/*.scss')
  //  .pipe(autoprefix('last 2 versions')) -- comment this line
    .pipe(sass())
    .pipe(gulp.dest('./dist/css/'))
    .pipe(cssmin())
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('./dist/css/'));
});

// =========================================
// Start - Add packages / new configurations

require('es6-promise').polyfill();
var postcss      = require('gulp-postcss');
var autoprefixer = require('autoprefixer');

gulp.task('autoprefixer', function () {

    return gulp.src(
          ['.dist/css/*.css', '!.dist/css/*.min.css']
      )
        .pipe(postcss([ autoprefixer({ browsers: ['last 2 versions'] }) ]))
        .pipe(gulp.dest('.dist/css/'));
});

// =========================================
// End new lines

// browser sync
gulp.task('browser-sync', ['styles'], function() {
    browserSync({
        server: {
            baseDir: ''
        },
        notify: false
    });
    gulp.watch("./src/sass/*.scss", ['scss']);
    gulp.watch("*.html").on('change', browserSync.reload);
});

// watch task
gulp.task('watch', function() {
  gulp.watch('./src/js/*.js', ['scripts']);
  gulp.watch('./src/sass/*.scss', ['styles']);
});

// image minification
gulp.task('imagemin', function() {
  var imgSrc = './src/img/**/*',
      imgDst = './dist/img';

  gulp.src(imgSrc)
    .pipe(changed(imgDst))
    .pipe(imagemin())
    .pipe(gulp.dest(imgDst));
});

gulp.task('default', ['scripts', 'styles', 'browser-sync', 'imagemin', 'watch']);

然后使用autoprefixer复制新的gulpfile.js配置

gulp autoprefixer

之后尝试在您的cmd控制台中写入

 def playbook_on_task_start(self, name, is_conditional):
        pass

问候:)