让Gulp手表仅对更改的文件执行功能

问题描述 投票:15回答:2

我是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文件夹。做这个的最好方式是什么?

javascript gulp gulp-watch
2个回答
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使其正常工作。


0
投票

请参见incremental builds on the Gulp docs

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

© www.soinside.com 2019 - 2024. All rights reserved.