gulp browserify bundle time takes too long

为君一笑 提交于 2019-12-04 11:06:15

This is because React goes and requires a couple of things which means your browserify has to traverse through more nodes.

One way you might be able to improve this is to prebundle React. However, this would only improve the React library, add another library and it will slow down again.

One thing you can use to improve rebundling is by adding watchify to your browserify bundle process.

Give this a try

var watchify = require('watchify');
var babelify = require('babelify');

var bundler;

function buildApp () {
  bundler = bundler || watchify(browserify({
    entries: config.paths.jsx,
    extensions: ['.jsx'],
    debug: true,
    transform: [babelify], //This will allow you to use babel for jsx/es6
    cache: {}, // required for watchify
    packageCache: {}, // required for watchify
    fullPaths: true //You can change this false in production
  }))

  return bundler
  .bundle()
  .on('error', onError)
  .pipe(source('app.js'))
  .pipe(gulp.dest(config.paths.dest));
}   

Essentially what you need to do is wrap your browserify in a watchify and add some extra properties (cache, packageCache, fullPaths)

Watchify will speed up your rebundling process by caching the bundled files.

!Important Note

Remember to remove watchify in production, unless you're build process will hang since it's watching your files.

Please have a look on my repo https://github.com/skrobek/react-gulp-browserify. I'm using exactly the same tech stack: gulp, browserify, react.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!