带有CLI的Angular 2-为生产而构建 [英] Angular 2 with CLI - build for production
问题描述
我新安装了angular-cli 1.0.0.beta.17(最新版本),开始新项目,能够在4200端口上为项目提供服务,没有问题-仅标准的应用程序就可以使用!"消息.
I have freshly installed angular-cli 1.0.0.beta.17 (latest one), start new project, able to serve project on port 4200 with no problems - just standard "app works!" message.
但是,当我尝试使用命令ng build --prod
为这个空的通用应用程序进行生产时,我根本没有创建main.*.js文件,并且显示了一些警告屏幕,例如:
However when I try to build for production this empty and generic application by using command ng build --prod
I do not have main.*.js file created at all and have a few screens of warnings like:
- 删除未使用的功能...
- 初始化中的站点效果...
- 等
这是一个全新的空项目-我还没有机会破坏任何东西...
This is a brand new empty project - I did not have a chance to break anything yet...
如何构建生产版本?
推荐答案
已针对Angular v6 +更新
Updated for Angular v6+
# Prod - these are equivalent
ng build --configuration=production
ng build --c=production
ng build --prod=true
# Dev - and so are these
ng build --configuration=development
ng build --c=development
ng build --prod=false
ng build
更多标志设置在这里
根据Angular-cli的github wiki v2 +,这是启动开发和生产版本的最常见方式
Per Angular-cli's github wiki v2+, these are the most common ways to initiate a dev and production build
# Prod these are equivalent
ng build --target=production --environment=prod
ng build --prod --env=prod
ng build --prod
# Dev and so are these
ng build --target=development --environment=dev
ng build --dev --env=dev
ng build --dev
ng build
有不同的默认标志会影响--dev和--prod的构建.
There are different default flags that will affect --dev vs --prod builds.
Flag --dev --prod
--aot false true
--environment dev prod
--output-hashing media all
--sourcemaps true false
--extract-css false true
--prod
还会设置以下不可举报的设置:
--prod
also sets the following non-flaggable settings:
- 如果在
.angular-cli.json
中配置,则添加服务工作程序. - 用
production
替换模块中的process.env.NODE_ENV
值(某些库需要此值,例如react). - 在代码上运行UglifyJS.
- Adds service worker if configured in
.angular-cli.json
. - Replaces
process.env.NODE_ENV
in modules with theproduction
value (this is needed for some libraries, like react). - Runs UglifyJS on the code.
我需要进行一些故障排除才能使AOT正常工作.当我跑步时:
I need to do some troubleshooting in order to get AOT working. When I ran:
ng build --prod --aot = false
ng build --prod --aot=false
我会得到类似的错误
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory'
最初,我必须进行一些项目重构才能使AOT正常工作.但是,如果遇到此错误,它们可能是一种解决方法.试试
Originally, I had to do some project refactoring to get AOT to work. However, they may be a fix if you are encountering this error. Try
npm i Enhanced-resolve@3.3.0
npm i enhanced-resolve@3.3.0
https://github.com/angular/angular-cli/issues/7113
这篇关于带有CLI的Angular 2-为生产而构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!