必须多次运行gulp以获得样式更改

时间:2017-01-09 18:39:11

标签: javascript css sass gulp

我在编译我的CSS时遇到问题,当我在我的SASS文件中进行更改时,它不会在尝试多次之后更改最终文件。


    var gulp = require('gulp');
    var sass = require('gulp-sass');
    var rename = require('gulp-rename');
    var postcss = require('gulp-postcss');
    var autoprefixer = require('autoprefixer');
    var cssnano = require('cssnano');
    var concat = require('gulp-concat');
    var uglify = require('gulp-uglify');

    //CSS Tasks
    gulp.task('styles', function() {
        console.log("Compilling SASS");
        gulp.src('app/sass/style.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(postcss([ autoprefixer() ]))
        .pipe(rename('999_style.css'))
        .pipe(gulp.dest('app/css/'));
    });
    gulp.task('stylescompress', ['styles'], function() {
        console.log("Concating and moving all the css files in styles folder");
        gulp.src("app/css/**.css")
        .pipe(concat('style.css'))
        .pipe(postcss([ cssnano() ]))
        .pipe(gulp.dest('css/'));
    });

    //Javascript Tasks
    gulp.task('jscompress', function() {
        //console.log("Concating and moving all the js files in javascript folder");
        gulp.src("app/js/**.js")
        .pipe(concat('scripts.js'))
        .pipe(gulp.dest('js/'))
        .pipe(rename('scripts.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('js/'))
    });

    //Watch task
    gulp.task('default',function() {
        gulp.watch(['app/sass/**/*.scss'],['stylescompress'])
        .on('change', function(event) {
            console.log('SASS - File ' + event.path + ' was ' + event.type + ', running tasks...');
        });
        gulp.watch(['app/js/**/*.js'],['jscompress'])
        .on('change', function(event) {
            console.log('SCRIPTS - File ' + event.path + ' was ' + event.type + ', running tasks...');
        });
    });

正如你所看到的,我根据Gulp Docs使用了所有内容,但我没有发现发生了什么。

2 个答案:

答案 0 :(得分:6)

我错过了它完成时的回归,让Gulp理解它何时应该调用下一个任务,它记录在Gulp Docs

当每个任务结束时,我使用流来让gulp理解,如下所示:

gulp.task('somename', function() {
  var stream = gulp.src('client/**/*.js')
    .pipe(minify())
    .pipe(gulp.dest('build'));
  return stream;
});

答案 1 :(得分:1)

安装浏览器同步 并添加

var browserSync = require('browser-sync').create();

在您的sass任务中,添加browserSync

gulp.task('sass', function() {
    console.log("Compilling SASS");
    gulp.src('app/sass/style.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(postcss([ autoprefixer() ]))
    .pipe(rename('999_style.css'))
    .pipe(gulp.dest('app/css/'))
    .pipe(browserSync.reload({
      stream: true
    }))
});

然后添加一个“监视”任务

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

然后使用“默认”

gulp.task('default', function(callback) {
  runSequence(['sass', 'browserSync', 'watch'],
    callback
  )
})

现在运行gulp watch:)