Заставьте часы Gulp выполнять функцию только в измененном файле

Я новичок в 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. Как лучше всего это сделать?


person bmdeveloper    schedule 16.02.2015    source источник


Ответы (2)


Вот как:

// 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, кстати.

person Leon Gaban    schedule 16.02.2015
comment
Есть ли способ уважать земной шар? file.path возвращает абсолютный путь, поэтому ** не учитывается, когда дело доходит до gulp.dest(). - person Markus; 26.05.2016
comment
@Маркус. посмотри на gulp-changed - person dgo; 20.12.2016

См. инкрементные сборки в документации по Gulp.

Вы можете отфильтровать неизмененные файлы между запусками задачи, используя функцию gulp.src, начиная с опции и gulp.lastRun.

person James    schedule 19.12.2019