Angular CLI构建目标与环境 [英] Angular CLI build target vs environment
问题描述
在 Angular CLI 中,--target
和--environment
有什么区别?运行 build 命令时的选项?
In the Angular CLI, what is the difference between the --target
and --environment
options when running the build command?
从文档中:
ng构建可以指定构建目标(--target = production或--target = development)和要与该构建一起使用的环境文件(--environment = dev或--environment = prod).默认情况下,使用开发构建目标和环境.
ng build can specify both a build target (--target=production or --target=development) and an environment file to be used with that build (--environment=dev or --environment=prod). By default, the development build target and environment are used.
但是,他们从未真正阐明两者之间的区别.
However, they never really clarify the distinction between the two.
据我所知,--environment
标志控制着构建时使用哪个环境文件(environment.ts
与environment.prod.ts
).但是--target
控制什么?
From what I can gather, the --environment
flag controls which environment file (environment.ts
vs environment.prod.ts
) gets used when doing the build. But then what does --target
control?
推荐答案
--environment
是 apps [0] .environments 对象的键. .angular-cli.json
--environment
is a key for the apps[0].environments object from .angular-cli.json
它类似于运行环境的配置文件(例如:本地,开发服务器,测试服务器,CI服务器,舞台服务器,生产服务器等). apps [0] .environments 对象的值是具有环境所有设置的文件名.在那里,您可以设置后端端点,密钥以及您想要的其他任何东西.然后,您可以在代码中使用它:
It is like a profile for the running environment (for example: local, development server, test server, CI server, stage server, production server and so on). The value of the apps[0].environments object is a file name with all settings for the environment. There you could set up backend endpoint, keys and whatever else you want. Then you could use it inside your code:
import {environment} from '@environments/environment';
const userEndPoint = `${environment.apiRoot}/user/`;
每个环境都可能是 production (environment.production === true)或非生产环境,即 development (environment.production === false).这是一个 target ,也可以使用下一个参数
Every environment could be production (environment.production === true) or non production i.e. development (environment.production === false). This is a target which could be defined also with the next parameter:
--target
是两个值的枚举:development
或production
.这是一个元"标志,它设置了其他标志:
--target
is a enum of two values: development
or production
. It is a 'meta' flags, that set other flags:
标志|
--dev
|--prod
--- | --- | ---
--aot
|false
|true
--environment
|dev
|prod
--output-hashing
|media
|all
--sourcemaps
|true
|false
--extract-css
|false
|true
--named-chunks
|true
|false
--build-optimizer
|false
|true
带有AOT和Angular 5
Flag |
--dev
|--prod
--- | --- | ---
--aot
|false
|true
--environment
|dev
|prod
--output-hashing
|media
|all
--sourcemaps
|true
|false
--extract-css
|false
|true
--named-chunks
|true
|false
--build-optimizer
|false
|true
with AOT and Angular 5
-prod还会设置以下不可举报的设置:
-如果在.angular-cli.json中配置,则添加服务工作程序.
-将模块中的process.env.NODE_ENV替换为生产值(某些库(例如react)需要此值).
-在代码上运行UglifyJS.
--prod also sets the following non-flaggable settings:
- Adds service worker if configured in .angular-cli.json.
- Replaces process.env.NODE_ENV in modules with the production value (this is needed for some libraries, like react).
- Runs UglifyJS on the code.
这篇关于Angular CLI构建目标与环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!