cache-loader

在一些性能开销较大的 loader 之前添加 cache-loader,将结果缓存中磁盘中。默认保存在 node_modueles/.cache/cache-loader 目录下

  1. {
  2. test: /\.jsx?$/,
  3. use: ['cache-loader','babel-loader']
  4. }

happypack thread-loader

HappyPack 就能让 Webpack 做到这点,它把任务分解给多个子进程去并发的执行,子进程处理完后再把结果发送给主进程。

  1. new Happypack({
  2. id: 'js', //和rule中的id=js对应
  3. use: ['babel-loader'] //必须是数组
  4. })
  5. //happypack
  6. {
  7. test: /\.js[x]?$/,
  8. use: 'Happypack/loader?id=js',
  9. include: [path.resolve(__dirname, 'src')]
  10. }
  11. //thread-loader
  12. rules: [{
  13. test: /\.jsx?$/,
  14. use: ['thread-loader', 'cache-loader', 'babel-loader']
  15. }]

HardSourceWebpackPlugin

  1. //webpack.config.js
  2. var HardSourceWebpackPlugin = require('hard-source-webpack-plugin');
  3. module.exports = {
  4. //...
  5. plugins: [
  6. new HardSourceWebpackPlugin()
  7. ]
  8. }

noParse

如果一些第三方模块没有AMD/CommonJS规范版本,可以使用 noParse 来标识这个模块,这样 Webpack 会引入这些模块,但是不进行转化和解析,从而提升 Webpack 的构建性能 ,例如:jquerylodash

  1. //webpack.config.js
  2. module.exports = {
  3. //...
  4. module: {
  5. noParse: /jquery|lodash/
  6. }
  7. }

optimization.splitChunks

  1. //webpack.config.js
  2. module.exports = {
  3. optimization: {
  4. splitChunks: {//分割代码块
  5. cacheGroups: {
  6. vendor: {
  7. //第三方依赖
  8. priority: 1, //设置优先级,首先抽离第三方模块
  9. name: 'vendor',
  10. test: /node_modules/,
  11. chunks: 'initial',
  12. minSize: 0,
  13. minChunks: 1 //最少引入了1次
  14. },
  15. //缓存组
  16. common: {
  17. //公共模块
  18. chunks: 'initial',
  19. name: 'common',
  20. minSize: 100, //大小超过100个字节
  21. minChunks: 3 //最少引入了3次
  22. }
  23. }
  24. }
  25. }
  26. }