美文网首页前端魔法世界React.js
React App 中如何分析Bundle Size?

React App 中如何分析Bundle Size?

作者: _TheSpecialOne | 来源:发表于2019-01-11 10:19 被阅读157次

source-map-explorer 利用 source maps 分析 bundles 的size,帮助我们理解是什么让我们的代码膨胀。

  • 首先安装 Source map explorer 到你的 Create React App project 中,请按一下步骤
npm install --save source-map-explorer

或者你更中意用 yarn

yarn add source-map-explorer
  • 然后在你的package.json中
"scripts": {
+    "analyze": "source-map-explorer build/static/js/main.*",
     "start": "react-scripts start",
     "build": "react-scripts build",
     "test": "react-scripts test",

如果你的React App是在Node express 项目中,可以以下尝试

"scripts": {
+   "analyze": "cd ./client && source-map-explorer build/static/js/main.*"
     "start": "react-scripts start",
     "build": "react-scripts build",
     "test": "react-scripts test",

然后在production build 之后开始分析你的脚本

npm run build
npm run analyze

运行结果如下:

> cd ./client && source-map-explorer build/static/js/main.*

Unable to map 42 / 3531258 bytes (0.00%)

结果将会在你本地生成一个HTML文件,Chrome打开如下显示,

image.png

分析出来的结果太可怕,node_modules的size为3.38M, 占了整个 bundle
95.7%, 那么如何给Bundle瘦身呢?

请听下回分解。

相关文章

网友评论

    本文标题:React App 中如何分析Bundle Size?

    本文链接:https://www.haomeiwen.com/subject/cjvtdqtx.html