获取Gulp watch仅在更改的文件上执行功能

时间:2015-02-16 16:21:07

标签: javascript gulp gulp-watch

我是Gulp的新手并拥有以下Gulpfile

var gulp = require('gulp');
var jshint = require('gulp-jshint');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');

gulp.task('compress', function () {
    return gulp.src('js/*.js') // read all of the files that are in js with a .js extension
      .pipe(uglify()) // run uglify (for minification)
      .pipe(gulp.dest('dist/js')); // write to the dist/js file
});

// default gulp task
gulp.task('default', function () {

    // watch for JS changes
    gulp.watch('js/*.js', function () {
        gulp.run('compress');
    });

});

我想将其配置为重命名,缩小并仅将我更改的文件保存到dist文件夹。做这个的最好方式是什么?

2 个答案:

答案 0 :(得分:12)

以下是:

// Watch for file updates
gulp.task('watch', function () {
    livereload.listen();

    // Javascript change + prints log in console
    gulp.watch('js/*.js').on('change', function(file) {
        livereload.changed(file.path);
        gutil.log(gutil.colors.yellow('JS changed' + ' (' + file.path + ')'));
    });

    // SASS/CSS change + prints log in console
    // On SASS change, call and run task 'sass'
    gulp.watch('sass/*.scss', ['sass']).on('change', function(file) {
        livereload.changed(file.path);
        gutil.log(gutil.colors.yellow('CSS changed' + ' (' + file.path + ')'));
    });

});

使用gulp-livereload也很棒,你需要安装Chrome plugin才能正常工作。

答案 1 :(得分:0)

请参见incremental builds on the Gulp docs

  

您可以使用gulp.src函数的since选项和gulp.lastRun过滤掉任务运行之间未更改的文件