Angular CLI 构建目标与环境 [英] Angular CLI build target vs environment
问题描述
在Angular CLI中,--target<有什么区别运行 build时的/code> 和
--environment
选项a> 命令?
In the Angular CLI, what is the difference between the --target
and --environment
options when running the build command?
来自文档:
ng build 可以指定构建目标(--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 的关键em> 来自 .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/`;
每个环境都可以是生产(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
|假
|真
--环境
|开发
|产品
--output-hashing
|媒体
|全部
--sourcemaps
|true
|假
--extract-css
|假
|真
--named-chunks
|true
|假
--build-optimizer
|假
|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屋!