天天看点

从 ng build 支持的参数 --prod,谈谈 Angular workspace configuration

语法:ng build project options

作用:编译

Angular

应用到 output 文件夹里,通常名称为 dist. 必须在工作空间目录下执行。

从 ng build 支持的参数 --prod,谈谈 Angular workspace configuration

输入参数:project, 可以为应用或者 library.

支持的参数

使用 ng build --help 查看所有支持的参数。

其中这个选项值得一说:–prod

Shorthand for “–configuration=production”.

是 --configuration=production 的简写形式。

When true, sets the build configuration to the production target.

将 build configuration 设置成 production target.

By default, the production target is set up in the workspace configuration such that all builds make use of bundling, limited tree-shaking, and also limited dead code elimination.

默认情况下,production target 在工作空间配置中设置,参考官网。

Workspace configuration 对应的文件是 angular.json.

A file named angular.json at the root level of an Angular workspace provides workspace-wide and project-specific configuration defaults for build and development tools provided by the Angular CLI.

下图是 SAP Spartacus workspace configuration 内容:

从 ng build 支持的参数 --prod,谈谈 Angular workspace configuration

version: The configuration-file version.

newProjectRoot: Path where new projects are created. Absolute or relative to the workspace folder.

新项目创建,一律放在 feature-libs 文件夹下面。

从 ng build 支持的参数 --prod,谈谈 Angular workspace configuration
从 ng build 支持的参数 --prod,谈谈 Angular workspace configuration

options:This section contains default build target options, used when no named alternative configuration is specified.

包含默认的 build target 选项。

Alternate build configurations

Angular CLI comes with two build configurations: production and development.

By default, the ng build command uses the production configuration

默认情况下,ng build 使用 production 配置。

Production 配置意味着下列优化:

Bundling files - 将文件打包合并在一起

Minimizing excess whitespace - 删除所有空白字符

Removing comments and dead code - 删除注释和永远不会执行的代码

Rewriting code to use short, mangled names (minification) - 精简变量名

You can define and name additional alternate configurations (such as stage, for instance) appropriate to your development process. Some examples of different build configurations are stable, archive and next used by AIO itself, and the individual locale-specific configurations required for building localized versions of an app.

可以自定义配置,比如 stage,archive 等等。

可以一次性传入多个配置:ng build --configuration stage,fr

In this case, the command parses the named configurations from left to right. If multiple configurations change the same setting, the last-set value is the final one.

如果多个配置都修改了同一个设置,以最后一个配置为准。

从 ng build 支持的参数 --prod,谈谈 Angular workspace configuration

Assets configuration

build 的过程中直接拷贝的资源。

Each build target configuration can include an assets array that lists files or folders you want to copy as-is when building your project. By default, the src/assets/ folder and src/favicon.ico are copied over.

继续阅读