Angular CLI 构建目标与环境 [英] Angular CLI build target vs environment

查看:27
本文介绍了Angular CLI 构建目标与环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular CLI中,--target<有什么区别运行 build--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.tsenvironment.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?

推荐答案

--environmentapps[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 是两个值的枚举:developmentproduction.这是一个元"标志,用于设置其他标志:

--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.

来自 https:///github.com/angular/angular-cli/wiki/build/1cf783837c392f5fadc7286e1fb28220b9a1b507#--dev-vs---prod-builds

这篇关于Angular CLI 构建目标与环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆