【问题标题】:gulp browserify reactify task is quite slowgulp browserify reactify 任务很慢
【发布时间】:2015-03-25 14:35:06
【问题描述】:

我使用 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');

谢谢

【问题讨论】:

    标签: javascript gulp reactjs browserify watchify


    【解决方案1】:

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

    【讨论】:

    • 感谢您的帮助,虽然在我的示例中我使用的是 watchify,但您的构建是否也需要大约 2 到 3 秒?
    • 我没有在我正在积极开发的任何项目上使用 watchify,所以我没有参考点,抱歉。
    • 我在我的项目中使用 watchify。第一个构建是最长的。大约需要2s。下一个构建大约需要 300 毫秒
    • @steveniseki 我也有同样的问题。我正在使用相同的堆栈(浏览器、监视),在我的情况下,任务需要更长的时间约 7 秒。滴滴你设法解决了这个问题?
    【解决方案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' ]);
    

    【讨论】:

      【解决方案3】:

      非常感谢@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 切换到我的浏览器的时间已经足够了。

      【讨论】:

      • cache:{} and packageCache: {} 为我做了什么!
      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多