Different assets in cli config for ng serve and ng build [Angular 5]

前端 未结 2 734
自闭症患者
自闭症患者 2021-01-06 08:27

Is possible to use different assets array when i use ng build?

\"assets\": [
  \"assets\",
  \"favicon.ico\",
  {
    \"glob\": \"**/*\",
    \"         


        
相关标签:
2条回答
  • 2021-01-06 09:11

    I don't believe there is an option to do environment specific assets. But you can make additional apps in your angular-cli.json which are basically just copies of each other, but with different assets. For example

    // 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"
            ],
            ...
        }
    ]
    

    Now you can build your assets differently by building a specific "app"

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

    Angular cli docs on multiple apps.

    0 讨论(0)
  • 2021-01-06 09:23

    My case was to include different robots.txt file per environment. So, in the src/environments folder I created two files: robots.txt and robots.prod.txt. In angular.json include it in assets array and use fileReplacements as follow (I paste only the relevant parts):

    "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"
              }
            ]
          }
        }
      }
    }
    

    I am using @angular/cli: ~8.3.4.

    0 讨论(0)
提交回复
热议问题