Gulp Browserify Reactify任务速度较慢。

9

我正在使用Gulp作为任务运行器,使用browserify捆绑我的CommonJs模块。

我注意到运行browserify任务相当慢,需要大约2-3秒的时间,目前我只使用了React和一些我为开发构建的非常小的组件。

有没有什么方法可以加快这个任务的速度,或者我的任务有没有明显的问题?

gulp.task('browserify', function() {
  var bundler = browserify({
    entries: ['./main.js'], // Only need initial file
    transform: [reactify], // Convert JSX to javascript
    debug: true, cache: {}, packageCache: {}, fullPaths: true
  });

  var watcher  = watchify(bundler);

  return watcher
  .on('update', function () { // On update When any files updates
    var updateStart = Date.now();
        watcher.bundle()
        .pipe(source('bundle.js'))
        .pipe(gulp.dest('./'));
        console.log('Updated ', (Date.now() - updateStart) + 'ms');
  })
  .bundle() // Create initial bundle when starting the task 
  .pipe(source('bundle.js'))
  .pipe(gulp.dest('./'));
});

我正在使用Browserify、Watchify、Reactify和Vinyl Source Stream,以及其他一些不相关的模块。

var browserify = require('browserify'),
watchify = require('watchify'),
reactify = require('reactify'),
source = require('vinyl-source-stream');

谢谢

3个回答

17

请查看使用watchify实现快速browserify构建,注意传递给browserify的仅为主入口点和watchify的配置。

转换被添加到watchify包装器中。

文章中的代码完全保留

var gulp = require('gulp');
var gutil = require('gulp-util');
var sourcemaps = require('gulp-sourcemaps');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var watchify = require('watchify');
var browserify = require('browserify');

var bundler = watchify(browserify('./src/index.js', watchify.args));
// add any other browserify options or transforms here
bundler.transform('brfs');

gulp.task('js', bundle); // so you can run `gulp js` to build the file
bundler.on('update', bundle); // on any dep update, runs the bundler

function bundle() {
  return bundler.bundle()
    // log errors if they happen
    .on('error', gutil.log.bind(gutil, 'Browserify Error'))
    .pipe(source('bundle.js'))
    // optional, remove if you dont want sourcemaps
      .pipe(buffer())
      .pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file
      .pipe(sourcemaps.write('./')) // writes .map file
    //
    .pipe(gulp.dest('./dist'));
}

谢谢你的帮助,不过在我的例子中我正在使用watchify,你的构建时间也需要2-3秒吗? - svnm
我目前正在开发的项目中没有使用watchify,所以我没有参考点,抱歉。 - Brigand
3
我在项目中使用watchify。第一次构建需要的时间最长,大约需要2秒钟。接下来的构建需要大约300毫秒左右的时间。 - niba
2
@steveniseki 我完全面临同样的问题。我使用的是同样的堆栈(browserify、watchify),在我的情况下任务甚至需要更长的时间——大约7秒钟。你成功解决了这个问题吗? - wawka

2
你需要使用watchify并启用其缓存。请查看以下链接: https://www.codementor.io/reactjs/tutorial/react-js-browserify-workflow-part-2 若要在构建源映射时进行更多优化,可以执行以下命令:

cd node_modules/browserify && npm i substack/browser-pack#sm-fast 这将节省您一半的时间

这是我的gulpfile.js的一部分。
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var htmlreplace = require('gulp-html-replace');
var source = require('vinyl-source-stream');
var browserify = require('browserify');
var watchify = require('watchify');
var reactify = require('reactify');
var streamify = require('gulp-streamify');

var path = {
    OUT : 'build.js',
    DEST2 : '/home/apache/www-modules/admimail/se/se',
    DEST_BUILD : 'build',
    DEST_DEV : 'dev',
    ENTRY_POINT : './src/js/main.jsx'
};

gulp.task('watch', [], function() {
    var bundler = browserify({
        entries : [ path.ENTRY_POINT ],
        extensions : [ ".js", ".jsx" ],
        transform : [ 'reactify' ],
        debug : true,
        fullPaths : true,
        cache : {}, // <---- here is important things for optimization 
        packageCache : {} // <----  and here
    });
    bundler.plugin(watchify, {
//      delay: 100,
//      ignoreWatch: ['**/node_modules/**'],
//      poll: false
    });

    var rebundle = function() {
        var startDate = new Date();
        console.log('Update start at ' + startDate.toLocaleString());
        return bundler.bundle(function(err, buf){
                if (err){
                    console.log(err.toString());
                } else {
                    console.log(' updated in '+(new Date().getTime() - startDate.getTime())+' ms');
                }
            })
            .pipe(source(path.OUT))
            .pipe(gulp.dest(path.DEST2 + '/' + path.DEST_DEV))
            ;
    };

    bundler.on('update', rebundle);
    return rebundle();
});

gulp.task('default', [ 'watch' ]);

1
感谢@PHaroZ的回答。不过,我需要对代码进行一些修改以满足我的需求。我正在使用ReactJS和Symfony2框架,所有的构建都需要7秒到21秒的时间!太疯狂了...现在我有了以下代码:
var path = {
    OUT : 'app.js',
    DEST_BUILD : './src/MyBundle/Resources/js/dist',
    ENTRY_POINT : './src/MyBundle/Resources/js/src/app.js'
};

gulp.task('watch', [], function() {
    var bundler = browserify({
        entries : [ path.ENTRY_POINT ],
        extensions : [ ".js", ".jsx" ],
//        transform : [ 'reactify' ],
        debug : true,
        fullPaths : true,
        cache : {}, // <---- here is important things for optimization
        packageCache : {} // <----  and here
    }).transform("babelify", {presets: ["es2015", "react"]});
    bundler.plugin(watchify, {
//      delay: 100,
//      ignoreWatch: ['**/node_modules/**'],
//      poll: false
    });

    var rebundle = function() {
        var startDate = new Date();
        console.log('Update start at ' + startDate.toLocaleString());
        return bundler.bundle(function(err, buf){
                if (err){
                    console.log(err.toString());
                } else {
                    console.log(' updated in '+(new Date().getTime() - startDate.getTime())+' ms');
                }
            })
            .pipe(source(path.OUT))
            .pipe(gulp.dest(path.DEST_BUILD))
            ;
    };

    bundler.on('update', rebundle);
    return rebundle();
});

现在第一次编译需要约20秒,每次更新文件需要约800毫秒。这正好足够我从IDE切换到浏览器。

"cache: {}和packageCache: {}" 对我来说很有帮助! - Abdo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接