更新文件时触发来自 gulpfile 的更新

Triggering update from gulpfile when file is updated

本文关键字:更新 gulpfile 文件      更新时间:2023-09-26

在我的gulpfile中,index.js被处理,拉入需求,并吐出bundle.js。问题是即使更新了requiredfile.js,我也需要触发更新。这是我的代码:

var browserify = require('browserify'),
    watchify = require('watchify'),
    gulp = require('gulp'),
    source = require('vinyl-source-stream'),
    sourceFile = './index.js',
    destFolder = './',
    destFile = 'bundle.js';
gulp.task('browserify', function() {
    return browserify(sourceFile, {transform: 'reactify'})
        .bundle()
        .pipe(source(destFile))
        .pipe(gulp.dest(destFolder));
});
gulp.task('watch', function(){
    var bundler = browserify(sourceFile, {
        debug: true,
        cache: {},
        packageCache: {},
        transform: 'reactify'
    });
    var watcher  = watchify(bundler);
    return watcher.on('update', function () { // When any files update
        console.log('Updating!');
        var updateStart = Date.now();
        watcher.bundle()
            .pipe(source(destFile))
            .pipe(gulp.dest(destFolder));
        console.log('Updated!', (Date.now() - updateStart) + 'ms');
    })
        .bundle() // Create the initial bundle when starting the task
        .pipe(source(destFile))
        .pipe(gulp.dest(destFolder));
});
gulp.task('default', ['browserify', 'watch']);

当其他文件发生更改时,如何添加更新(而不会通过同一进程运行requiredfile.js而导致问题)?

想通了,我添加了另一个任务来包装第一个任务,每当列出的文件之一更新时就会触发。

gulp.task('watchall', function(){
    gulp.watch( ['index.js', 'js/*.js', 'js/**/*.js', 'index.html'], ['browserify']);
});
更新

:这是一个较慢的更新。