aromameng / aromameng.github.io

个人博客
https://aromameng.github.io/
0 stars 1 forks source link

create-react-app 打包优化 #13

Open aromameng opened 5 years ago

aromameng commented 5 years ago
安装 webpack-bundle-analyzer 分析打包后的文件
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; 

new BundleAnalyzerPlugin()
分析发现antd的包太大,可以采用按需加载

官方指导:https://ant.design/docs/react/introduce-cn 安装 babel-plugin-import

// .babelrc or babel-loader option
{
  "plugins": [
    ["import", {
      "libraryName": "antd",
      "libraryDirectory": "es",
      "style": "css" // `style: true` 会加载 less 文件
    }]
  ]
}
模块懒加载,重复打包antd的情况

使用webpack 的 plugin CommonsChunkPlugin 抽取公共代码

new webpack.optimize.CommonsChunkPlugin({
      minChunks: 2,
      minSize: 0,
      children: true,
      deepChildren: true,
      async: true
    }),
aromameng commented 4 years ago