每次watchify检测到更改时,捆绑时间变慢。我的gulp任务肯定有问题。任何想法吗?

gulp.task('bundle', function() {
    var bundle = browserify({
            debug: true,
            extensions: ['.js', '.jsx'],
            entries: path.resolve(paths.root, files.entry)
        });

    executeBundle(bundle);
});

gulp.task('bundle-watch', function() {
    var bundle = browserify({
        debug: true,
        extensions: ['.js', '.jsx'],
        entries: path.resolve(paths.root, files.entry)
    });

    bundle = watchify(bundle);
    bundle.on('update', function(){
        executeBundle(bundle);
    });
    executeBundle(bundle);

});

function executeBundle(bundle) {
    var start = Date.now();
    bundle
        .transform(babelify.configure({
            ignore: /(bower_components)|(node_modules)/
        }))
        .bundle()
        .on("error", function (err) { console.log("Error : " + err.message); })
        .pipe(source(files.bundle))
        .pipe(gulp.dest(paths.root))
        .pipe($.notify(function() {
            console.log('bundle finished in ' + (Date.now() - start) + 'ms');
        }))
}

最佳答案

我遇到了同样的问题,并通过将环境变量DEBUG设置为babel进行了调查。例如。:

$ DEBUG=babel gulp


检查调试输出后,我注意到babelify运行了多次转换。

罪魁祸首是我实际上在每次执行包时都添加了转换。您似乎也遇到了同样的问题。

移动

.transform(babelify.configure({
    ignore: /(bower_components)|(node_modules)/
}))


executeBundle内部进入任务。新的bundle-watch可以这样写:

gulp.task('bundle-watch', function() {
    var bundle = browserify({
        debug: true,
        extensions: ['.js', '.jsx'],
        entries: path.resolve(paths.root, files.entry)
    });

    bundle = watchify(bundle);
    bundle.transform(babelify.configure({
        ignore: /(bower_components)|(node_modules)/
    }))
    bundle.on('update', function(){
        executeBundle(bundle);
    });
    executeBundle(bundle);
});

08-07 18:06