【问题标题】:Different assets in cli config for ng serve and ng build [Angular 5]ng serve 和 ng build 的 cli config 中的不同资产 [Angular 5]
【发布时间】:2018-06-04 06:12:56
【问题描述】:

当我使用ng build时可以使用不同的资产数组吗?

"assets": [
  "assets",
  "favicon.ico",
  {
    "glob": "**/*",
    "input": "../externalDir",
    "output": "./app/",
    "allowOutsideOutDir": true
  }
]

就我而言,当我使用ng build 时,我只想构建这个:

"assets": [
  "assets",
  "favicon.ico"
]

【问题讨论】:

    标签: angular typescript build angular-cli assets


    【解决方案1】:

    我的情况是在每个环境中包含不同的 robots.txt 文件。因此,在src/environments 文件夹中,我创建了两个文件:robots.txtrobots.prod.txt。在angular.json 中将其包含在assets 数组中并使用fileReplacements 如下(我只粘贴相关部分):

    "architect": {
      "build": {
        "options": {
          "assets": [
            "src/assets",
            {
              "glob": "robots.txt",
              "input": "src/environments/",
              "output": "/"
            }
          ]
        },
        "configurations": {
          "production": {
            "fileReplacements": [
              {
                "replace": "src/environments/robots.txt",
                "with": "src/environments/robots.prod.txt"
              }
            ]
          }
        }
      }
    }
    

    我正在使用@angular/cli: ~8.3.4

    【讨论】:

    【解决方案2】:

    我不认为可以选择制作特定于环境的资产。但是您可以在 angular-cli.json 中创建其他应用程序,它们基本上只是彼此的副本,但具有不同的资产。例如

    // angular-cli.json
    "apps": [
        {
            "root": "src",
            "outDir": "dist",
            "name": "devApp",
            "assets" : [
                "assets",
                "favicon.ico",
                {
                    "glob": "**/*",
                    "input": "../externalDir",
                    "output": "./app/",
                    "allowOutsideOutDir": true
                },
            ],
            ...
        },
        {
            "root": "src",
            "outDir": "dist",
            "name": "prodApp",
            "assets": [
                "assets",
                "favicon.ico"
            ],
            ...
        }
    ]
    

    现在您可以通过构建特定的“应用”以不同方式构建资产

    // dev
    ng build --app=0
    // or
    ng build --app=devApp
    
    // prod
    ng build --app=1
    // or
    ng build --app=prodApp
    

    多个应用程序上的 Angular cli docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 2020-04-19
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      相关资源
      最近更新 更多