简体   繁体   English

Angular CLI 1.2.4命令ng build的说明

[英]Explanation on Angular CLI 1.2.4 command ng build

I am learning Angular4 and wondering about what is ng build and ng build --prod really do for us. 我正在学习Angular4,想知道ng build和ng build --prod真正为我们做什么。

When running with ng build, there are, for instance main.bundle.js, main.bundle.js.map , generated inside dist folder 使用ng build运行时,例如dist文件夹中生成了main.bundle.js,main.bundle.js.map

but with ng build --prod, there are only, for instance main.90e798078cb11a3159ce.bundle.js , generated inside dist folder 但是使用ng build --prod时,例如只有main.90e798078cb11a3159ce.bundle.js在dist文件夹中生成

Can anyone please explain how ng build work with and without --prod 任何人都可以解释ng build如何使用--prod和不使用--prod

Thank you 谢谢

According to the angular-cli documentation: 根据angular-cli文档:

Both --dev/--target=development and --prod/--target=production are 'meta' flags, that set other flags. --dev/--target=development--prod/--target=production都是“ meta”标志,用于设置其他标志。 If you do not specify either you will get the --dev defaults. 如果您不指定任何一个,则将获得--dev默认值。

and the difference between them are explained in this link: https://github.com/angular/angular-cli/wiki/build#--dev-vs---prod-builds 并在此链接中说明了它们之间的区别: https : //github.com/angular/angular-cli/wiki/build#--dev-vs---prod-builds

  Flag                 --dev    --prod
--aot                   false     true
--environment            dev      prod
--output-hashing        media      all
--sourcemaps             true     false
--extract-css           false     true

ng build –prod OR ng build ng build –prod OR ng build

Using the --prod flag allows Angular to do Ahead of Time Compilation (AOT). 使用--prod标志可以使Angular提前进行时间编译(AOT)。

AOT Ahead of Time Compilation 提前进行AOT编译

The Angular Ahead-of-Time (AOT) compiler converts your Angular HTML and TypeScript code into efficient JavaScript code during the build phase before the browser downloads and runs that code. 在浏览器下载并运行该代码之前,Angular Ahead-of-Time(AOT)编译器会在构建阶段将Angular HTML和TypeScript代码转换为高效的JavaScript代码。

Advantages of AOT: AOT的优势:

  1. Highlights the compilation error, runtime error and exception before running on the browser hence the name Ahead Of Time (AOT). 在浏览器上运行之前突出显示编译错误,运行时错误和异常,因此命名为Ahead Of Time(AOT)。

  2. If you use ng build in your projects to build your application, if you notice the file size of vendor.bundle.js and vendor.bundle.js.map files in your build directory it will be in MBS which get downloaded to the Browsers and make our application too loaded. 如果您在项目中使用ng build来构建应用程序,则如果注意到build目录中vendor.bundle.js和vendor.bundle.js.map文件的文件大小,它将以MBS格式下载到浏览器,使我们的应用程序加载过多。

But on the other hand if you use the flag ng build –prod you will notice an excessive decrease of this files to 200 KBS means 100 or more times lesser in size. 但是另一方面,如果您使用ng build –prod标志,则会注意到此文件过多减少到200 KBS,意味着大小减小了100倍或更多倍。


Therefore I recommend you to use the AOT in the building of Angular Application by using --prod flag. 因此,我建议您通过使用--prod标志在Angular Application的构建中使用AOT。

And if you want further reading and information on this topic please refer to the following site: https://angular.io/guide/aot-compiler 并且,如果您想进一步阅读和了解有关此主题的信息,请访问以下站点: https : //angular.io/guide/aot-compiler

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

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