create-react-app 打包后靜態文件過大 webpack優化


在最近的項目里,頁面和靜態文件並不是很多的情況下,打包后發現產出的靜態資源卻很大。

1.關掉sourcemap

 

在config/webpack.config.js文件里,大概30幾行的位置添加這樣一句代碼,這樣做的作用是防止線上生成環境將源碼一起打包部署。

2.將一些公共的庫(比如antd)做一個緩存


splitChunks: {
        chunks: 'all',
        name: "vender",
        cacheGroups: {
          vender: {
            name: "vendor",
            test: /[\\/]node_modules[\\/]/,
            chunks: "all",
            priority: 10,
            enforce: true
          },
          react: {
            name: "react",
            test: (module) => /react|redux/.test(module.context),
            chunks: "initial",
            priority: 11,
            enforce: true
          },
          antd: {
            name: "antd",
            test: (module) => {
              return /ant/.test(module.context);
            },
            chunks: "initial",
            priority: 11,
            enforce: true
          },
          moment: {
            name: "moment",
            test: (module) => {
              return /moment/.test(module.context);
            },
            chunks: "initial",
            priority: 13,
            enforce: true
          }
        }
      },

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM