10

每次 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');
        }))
}
4

1 回答 1

39

我遇到了同样的问题,并通过将环境变量 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);
});
于 2015-04-27T12:39:23.330 回答