简体   繁体   English

针对生产环境优化Angular应用

[英]Optimize Angular application for Production Environment

I'm trying to optimize my Angular application for Production. 我正在尝试优化生产的Angular应用程序。 Currently I'm using Uglify, Compression, ... and I'm compiling in AOT. 目前,我正在使用Uglify,Compression,...,并且正在AOT中进行编译。 When I compile the application I'm getting this message: 当我编译应用程序时,我收到以下消息:

Asset     Size  Chunks                    Chunk Names
main.bundle.js  2.66 MB       0  [emitted]  [big]  main
main.bundle.js.gz   530 kB          [emitted]  [big]
../index.html   2.3 kB          [emitted]

Looking in the Webpack Bundle Analyzer I have seen the next one: 在Webpack Bundle Analyzer中查看,我看到了下一个: 在此处输入图片说明

Here I have seen that: 在这里,我看到了:

1.- @angular/material are adding all the components to the bundle but I only use some of this. 1.- @ angular / material将所有组件添加到捆绑包中,但我仅使用其中的一部分。

2.- In my SRC are being added my *.component.ts files but also my *.component.ngfactory.ts (the same for my CSS) 2.-在我的SRC中,添加了我的* .component.ts文件以及我的* .component.ngfactory.ts文件(与CSS相同)

So, my question is: Is there a way to ask Webpack not to include the components from material that I'm not using? 因此,我的问题是:有没有办法让Webpack不要包括我不使用的材料中的组件? And, what are the ngfactory files and why they are being added? 而且,什么是ngfactory文件,为什么要添加它们?

UPDATE 1: 11/14/2017 更新1:11/14/2017

Hi, I have been investigating and I have seen that, when I'm compiling for AOT my browser.module.ngfactory.ts contain "import * from ..." for all the dependencies 嗨,我一直在调查中,已经看到,当我为AOT编译时,我的browser.module.ngfactory.ts包含“ import * from ...”,用于所有依赖项

import * as i81 from '../+app/+product/product.component';
import * as i82 from '../+app/+term/term.component';
import * as i83 from '../+app/+home/home.component';
import * as i84 from '../+app/+oops/oops.component';
import * as i85 from '@angular/material/toolbar';
import * as i86 from '@angular/material/button';
import * as i87 from '@angular/material/button-toggle';

For example, I'm not using angular/material/toolbar, so I don't understand why it is being added. 例如,我没有使用angular / material / toolbar,所以我不明白为什么要添加它。

There's a lot of things that you could do to optimize your production builds (like removing unnecessary material modules). 您可以做很多事情来优化生产构建(例如删除不必要的材料模块)。 It looks to be like you're not using angular-cli, but their --build-optimizer flag for production builds is very helpful. 看起来您好像没有在使用angular-cli,但是用于生产构建的--build-optimizer标志非常有用。

Based on your bundle analyzer report, your src modules are quite large, for this I would recommend lazy loading. 根据包分析器报告,您的src模块很大,为此,我建议您延迟加载。 It will allow you load the modules over time as the user navigates to them instead of all upon app initialization. 当用户导航到模块时,它将允许您随时间加载模块,而不是在应用初始化时加载所有模块。 You can see an example of lazy loading here 您可以在此处看到延迟加载的示例

Here are some more detailed answers about optimizing angular bundle sizes 有关优化角度束尺寸的一些更详细的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM