猫码让前端飞Web前端之路

11、webpack从0到1-CodeSplitting代码分割

2020-03-19  本文已影响0人  ComfyUI

简单说下代码分割。
git仓库:webpack-demo

1、什么是codeSplitting?

2、配置

module.exports = {
  mode: "development",
  entry: {
    main: "./src/index.js"
  },
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "dist")
  },

+ // 代码分割codeSpliting
+ optimization: {
+   splitChunks: {
+     chunks: "all"
+   }
+ },
  // ...
};

3、SplitChunksPlugin

module.exports = {
  // ...
  optimization: {
    splitChunks: {}
  }
  // 等同于:
  optimization: {
    splitChunks: {
      chunks: 'async',
      minSize: 30000,
      minRemainingSize: 0,
      maxSize: 0,
      minChunks: 1,
      maxAsyncRequests: 6,
      maxInitialRequests: 4,
      automaticNameDelimiter: '~',
      automaticNameMaxLength: 30,
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10
        },
        default: {
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true
        }
      }
    }
  },
  // ...
};

4、小结

参考链接
webpack-代码分割
SplitChunksPlugin

上一篇下一篇

猜你喜欢

热点阅读