浏览器重新设置任务很慢

时间:2015-01-27 03:20:49

标签: javascript gulp reactjs browserify watchify

我正在使用Gulp作为我的任务运行器并浏览器捆绑我的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 个答案:

答案 0 :(得分:16)

fast browserify builds with watchify。请注意,传递给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'));
}

答案 1 :(得分: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' ]);

答案 2 :(得分:1)

非常感谢@PHaroZ的回答。我不得不为我的需要修改一些代码。我在Symfony2框架上使用ReactJS,我所有的构建都需要7s-21s!疯了..这就是我现在所拥有的:

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切换到我的浏览器就足够了。

相关问题