Angular CLI - 不同的开发和生产资产集

Angular CLI - different sets of dev and prod assets

每当我使用 ng build--prod 开关时,我似乎无法找到是否以及如何使用不同的资产集。

用例是我有多个项目,共享文件夹中有一组公共资产,所以angular.json中每个项目的配置是这样的:

"assets": [
  {
    "glob": "**/*",
    "input": "src/assets",
    "output": "/assets/"
  },
  {
    "glob": "**/*",
    "input": "projects/project1/src/assets/",
    "output": "/assets/"
  }
],
...
"assets": [
  {
    "glob": "**/*",
    "input": "src/assets",
    "output": "/assets/"
  },
  {
    "glob": "**/*",
    "input": "projects/project2/src/assets/",
    "output": "/assets/"
  }
],

制作生产版本时,我想删除通用的 src/assets 路径。

可能吗?

您需要检查设置配置。此配置将帮助您为项目创建不同的环境。在你的案例项目 2.

您需要在 angular.json

上设置不同的环境
"project2": {
          "fileReplacements": [
            {
              "replace": "", //project 1 folder
              "with": "" // project 2 folder
            },
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ]
        }

设置完成后你可以点击命令

ng build --configuration project2

注意:您需要在新环境中设置必要的参数,如下所示

"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,

详细官方文档linkhttps://angular.io/guide/build

我认为答案就在我眼皮底下:每个 configurations 对象都允许重新定义 assets 属性.

所以我的需求配置很简单:

{
"configurations": {
  "production": {
     "assets": [
       {
         "glob": "**/*",
         "input": "projects/project1/src/assets/",
         "output": "/assets/"
       }
     ],
     ...
  }
}

它就像一个魅力。