How can we run Autoprefixer after “compass watch” using Gulp?

微笑、不失礼 提交于 2020-01-05 12:33:41

问题


I want to take advantage of running autoprefixer after compass watches for changes to my scss files and update the css file with the autoprefixed code, but I'm stuck. I created a gulp.task in gulpfile.js for compass and autoprefixer. When I run "gulp server" using the gulpfile.js below everything works but no autoprefixing; the scss files are run through compass and output as a css file and browserSync live-reloads the page in the browser. Any help would be greatly appreciated.

'use strict';

var gulp = require('gulp');
var jshint = require('gulp-jshint');
var stylish = require('jshint-stylish');
var paths = require('compass-options').paths();
var rename = require('gulp-rename');
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync');
var shell = require('gulp-shell');

//////////////////////////////
// Begin Gulp Tasks
//////////////////////////////
gulp.task('lint', function () {
  return gulp.src([
      paths.js + '/**/*.js',
      '!' + paths.js + '/**/*.js'
    ])
    .pipe(jshint())
    .pipe(jshint.reporter(stylish))
});

//////////////////////////////
// Compass Task
//////////////////////////////
gulp.task('compass', function () {
  return gulp.src(paths.sass + '/**/*')
    .pipe(shell([
      'bundle exec compass watch --time'
    ]));
});

//////////////////////////////
// Autoprefixer
//////////////////////////////
gulp.task('prefix', function() {
  return gulp.src('css/style.css')
    .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
    .pipe(gulp.dest('css'));
});

//////////////////////////////
// Watch
//////////////////////////////
gulp.task('watch', function () {
  gulp.watch(paths.js + '/**/*.js', ['lint']);
});

//////////////////////////////
// BrowserSync Task
//////////////////////////////
gulp.task('browserSync', function () {
  browserSync.init([
    paths.css +  '/**/*.css',
    paths.js + '/**/*.js',
    paths.img + '/**/*',
    paths.fonts + '/**/*',
    paths.html + '/**/*.html',
  ]);
});

//////////////////////////////
// Server Tasks
//////////////////////////////
gulp.task('server', ['watch', 'compass', 'browserSync']);

回答1:


Like @tmack mentioned you must use autoprefixer in the same task. Here is an example from one of my projects (I use gulp-compass for compiling):

var compass = require('gulp-compass'),
    autoprefixer = require('gulp-autoprefixer');

// Styles
gulp.task('styles', function() {
  return gulp.src(['src/styles/main.scss'])
    .pipe(compass({
        sass     : 'src/styles',
        css      : 'dist/styles',
        logging  : false,
        comments : false,
        style    : 'expanded'
    }))
    .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'ff 17', 'opera 12.1', 'ios 6', 'android 4'))
    .pipe(gulp.dest('dist/styles'));
});

Ciao Ralf



来源:https://stackoverflow.com/questions/24462804/how-can-we-run-autoprefixer-after-compass-watch-using-gulp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!