简体   繁体   English

如何提高webpack的性能?

[英]How to improve webpack performance?

I recently switched from browserify to webpack and the build time jumped from 4s to 16s on (2014 MBP). 我最近从browserify切换到webpack,构建时间从4s跃升到16s(2014 MBP)。 I understand webpack does alot more than browserify but i shouldn't take that long. 我理解webpack确实比浏览器更多,但我不应该花那么长时间。 My build process is fairly simple. 我的构建过程非常简单。 Are there any tips or options to improve my build time? 有没有提示或选项来改善我的构建时间?

var webpackOptions = {
  cache : true,
  output: {
    filename: '[name].js',
  },
  module: {
    loaders: [
      { test: /\.js$/, loader: 'jsx-loader' },
      { test: /\.css$/, loader: "style!css" }
    ]
  },
};


gulp.task('buildJs', function(){ 
    multipipe(
      gulp.src(jsSrcPath),
      named(),
      webpack(webpackOptions),
      uglify(),
      gulp.dest(jsDestPath)
    ).on('error', function(error){
      console.log(error)
    });
});

You should set include paths for your loaders. 您应该为加载器设置include路径。 Example: 例:

{ test: /\.js$/, loader: 'jsx-loader', include: jsSrcPath }

Consider doing the same for that css case. 考虑对css案例做同样的事情。

In my experience this can give massive gains as it doesn't have to traverse through node_modules anymore. 根据我的经验,这可以带来巨大的收益,因为它不再需要遍历node_modules Alternatively you could exclude node_modules but I find it neater just to set up that include . 或者你可以exclude node_modules但我发现它只是设置include It gets more complicated if you are requiring content out of the include path, though. 但是,如果您要求包含路径之外的内容,则会变得更加复杂。

Docs for include/exclude 包含/排除的文档

You can use the noParse option for big files, like jquery and angular. 您可以对大文件使用noParse选项,例如jquery和angular。

Examples here: https://github.com/jeffling/angular-webpack-example/blob/b2b59dff60c35ee6d70170948ab15bba8af5e613/template/webpack.config.coffee#L60 示例: https//github.com/jeffling/angular-webpack-example/blob/b2b59dff60c35ee6d70170948ab15bba8af5e613/template/webpack.config.coffee#L60

Also, if you set cache to true, when watching it rebuilds a lot faster . 此外,如果您将cache设置为true,则在观看它时会更快地重建。

Another technique to increasing speed is to put big dependencies that you aren't going to edit into a separate bundle. 另一种提高速度的技术是将您不打算编辑的大依赖项放入单独的包中。

Recently a new module loader, HappyPack (not written by me), makes heavy use of parallelization and disk caching to improve build times on large code bases pretty significantly. 最近,一个新的模块加载器HappyPack (不是我编写的)大量使用并行化和磁盘缓存来显着改善大型代码库的构建时间。 Compile times on my code base went from 40 seconds to 10. It's still a pretty new library though, so it's not extremely well documented or user friendly. 我的代码库上的编译时间从40秒到10秒。但它仍然是一个非常新的库,所以它没有很好的文档记录或用户友好。 Worth taking a look at though. 值得一看但是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM