我有一个简单的应用,由初始化angular-cli
。
它显示一些与3条路线有关的页面。我有3个组成部分。在这个页面上我使用一个lodash
与角2 HTTP模块来获取一些数据(使用RxJS Observable
S,map
和subscribe
)。我使用简单的显示这些元素*ngFor
。
但是,尽管事实上我的应用程序非常简单,但我还是得到了巨大的捆绑包和地图。我不是在谈论gzip版本,而是gzip压缩之前的大小。这个问题只是一般性建议查询。
一些测试结果:
ng构建
哈希:8efac7d6208adb8641c1时间:10129毫秒块{0} main.bundle.js,main.bundle.map(主要)18.7 kB {3} [初始] [呈现]
块{1} styles.bundle.css,styles.bundle.map,styles.bundle.map(样式)155 kB {4} [initial] [rendered]
块{2} scripts.bundle.js,scripts.bundle.map(脚本)128 kB {4} [初始] [呈现]
块{3} vendor.bundle.js,vendor.bundle.map(供应商)3.96 MB [初始] [呈现]
块{4} inline.bundle.js,inline.bundle.map(内联)0字节[输入] [呈现]
等待:10Mb的供应商捆绑包是否提供了如此简单的应用程序?
ng build --prod
哈希:09a5f095e33b2980e7cc时间:23455ms块{0} main.6273b0f04a07a1c2ad6c.bundle.js,main.6273b0f04a07a1c2ad6c.bundle.map(主要)18.3 kB {3} [初始] [呈现]
块{1} styles.bfdaa4d8a4eb2d0cb019.bundle.css,styles.bfdaa4d8a4eb2d0cb019.bundle.map,styles.bfdaa4d8a4eb2d0cb019.bundle.map(样式)154 kB {4} [initial]
块{2} scripts.c5b720a078e5464ec211.bundle.js,scripts.c5b720a078e5464ec211.bundle.map(脚本)128 kB {4} [initial] [rendered]
chunk {3} vendor.07af2467307e17d85438.bundle.js, vendor.07af2467307e17d85438.bundle.map (vendor) 3.96 MB [initial] [rendered]
chunk {4} inline.a345391d459797f81820.bundle.js, inline.a345391d459797f81820.bundle.map (inline) 0 bytes [entry] [rendered]
Wait again: such a similar vendor bundle size for prod?
ng build --prod --aot
Hash: 517e4425ff872bbe3e5b Time: 22856ms chunk {0} main.95eadabace554e3c2b43.bundle.js, main.95eadabace554e3c2b43.bundle.map (main) 130 kB {3} [initial] [rendered]
chunk {1} styles.e53a388ae1dd2b7f5434.bundle.css, styles.e53a388ae1dd2b7f5434.bundle.map, styles.e53a388ae1dd2b7f5434.bundle.map (styles) 154 kB {4} [initial] [rendered]
chunk {2} scripts.e5c2c90547f3168a7564.bundle.js, scripts.e5c2c90547f3168a7564.bundle.map (scripts) 128 kB {4} [initial] [rendered]
chunk {3} vendor.41a6c1f57136df286f14.bundle.js, vendor.41a6c1f57136df286f14.bundle.map (vendor) 2.75 MB [initial] [rendered]
chunk {4} inline.97c0403c57a46c6a7920.bundle.js, inline.97c0403c57a46c6a7920.bundle.map (inline) 0 bytes [entry] [rendered]
ng build --aot
Hash: 040cc91df4df5ffc3c3f Time: 11011ms chunk {0} main.bundle.js, main.bundle.map (main) 130 kB {3} [initial] [rendered]
chunk {1} styles.bundle.css, styles.bundle.map, styles.bundle.map (styles) 155 kB {4} [initial] [rendered]
chunk {2} scripts.bundle.js, scripts.bundle.map (scripts) 128 kB {4} [initial] [rendered]
chunk {3} vendor.bundle.js, vendor.bundle.map (vendor) 2.75 MB [initial] [rendered]
chunk {4} inline.bundle.js, inline.bundle.map (inline) 0 bytes [entry] [rendered]
So a few questions for deploying my app on prod:
- Why are the vendor bundles so huge?
- Is tree shaking properly used by
angular-cli
? - How to improve this bundle size?
- Are the .map files required?
- Are the testing features included in bundles? I don't need them in prod.
- Generic question: what are the recommanded tools to pack for prod? Maybe
angular-cli
(using Webpack in the background) is not the best option? Can we do better?
I searched many discussions on Stack Overflow, but I haven't found any generic question.
如果您使用的是Angular 8+,并且想要减小捆绑包的大小,则可以使用Ivy。Ivy是Angular 9中的默认视图引擎。只需转到src / tsconfig.app.json并添加angularCompilerOptions参数即可,例如: