Provide Angular build context to external tool configuration files

未关闭
#20,253 6 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞
技术栈
angular, tailwindcss, typescript

调研方向

首先检查 ng build 如何读取 angular.json 中的构建选项和配置,然后阅读 #20015 中相关的 PurgeCSS 讨论。在开始实现之前,需要从所提出的环境变量方案中作出决策;完成的条件是具备一致同意的设计、文档化的配置语义,以及对配置合并和平台行为的覆盖。

由索引模型根据 Issue 内容生成。

描述

area: @angular-devkit/build-angular devkit/build-angular:browser feature feature: insufficient votes

🚀 Feature request

Command (mark with an x)
  • build
Description

Spin off from PurgeCSS discussion in #20015.

It would be nice for external tool configuration files (like Tailwind) to have some knowledge about the Angular build context in order to conditionalize their configurations. This could allow ng build --configuration production to change the config used by other tools and affect their builds as well. This allows other tools in a project to be more intelligent about the intent of a build and do what the user expects.

Describe the solution you'd like

There are a couple possible implementations of this.

  1. Set an environment variable like NG_BUILD_CONFIGS="foo,bar,baz". Then config files could do something like:
const enableOptimizations = process.env['NG_BUILD_CONFIGS'].split(',').includes('production');

This would decouple the Angular CLI from any specific tools and allow the individual tool configs to define the semantics for each configuration. Similarly, the semantics for each configuration are also defined directly in the tool's config file rather than angular.json. That seems weird to me, but arguably it's a benefit so 🤷‍♂️. It also means that the config needs some knowledge of Angular, which isn't too bad considering its in the config, not the tool library, but this can still be undesirable.

  1. Add an env option in angular.json. Each configuration in the angular.json file could include an env map that looks like:
{
  "projects": {
    "my-project": {
      "architect": {
        "build": {
          "options": {
            "env": {
              "MY_BUILD_ENV": "development",
            },
          },
        },
        "configurations": {
          "production": {
            "env": {
              "MY_BUILD_ENV": "production",
            },
          },
        },
      },
    },
  },
}

The Angular CLI would parse and set the relevant environment variables during a build. Then tool configs could look for the project-specific environment variables like so:

const enableOptimizations = process.env['MY_BUILD_ENV'] === 'production';

This would still give users full control over the semantics of any given environment variable and they can choose its value on a per-configuration basis. The challenges here include merging multiple configuration environment variables which might conflict and that this environment would apply to all tools used by the Angular CLI. Users might also break something by setting a variable they probably shouldn't (such as PATH or other system variables) and breaking the CLI.

  1. Just have users set the environment variable on the command line:
MY_BUILD_ENV="production" ng build --configuration production

Then use that variable in the tool config just like previously:

const enableOptimizations = process.env['MY_BUILD_ENV'] === 'production';

This requires no effort on the Angular CLI side and users can do this today. The downside here is that it requires special knowledge of how to build a particular app. A new developer would reasonably expect ng build --configuration production to do everything it needs to, and then be surprised something didn't optimize because they didn't set MY_BUILD_ENV="production". Projects could wrap such a command into an npm run build:prod which sets the variable, but not everyone will use it. This is also a cross platform issue, as I don't think there is a Unix and Windows compatible way of doing this (see this question), so supporting both environment for build is more hassle for the user.

There are likely other possible implementations that have their own trade offs. We'll need to discuss this more and come to a decision on how to move forward here.

主要语言
TypeScript
星标
27k
派生
11.8k
平均合并
16 小时 35 分钟
30 天内合并 PR
176

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

angular/angular-cli 的其他 Issue

查看 angular/angular-cli 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。