Different assets in cli config for ng serve and ng

2020-03-06 08:13发布

问题:

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

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

In my case when i use ng build i want to build only this:

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

回答1:

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.



回答2:

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.