简体   繁体   English

如何在 Angular 工作区配置上用属性 fileReplacements 替换文件?

[英]How can I replace file with property fileReplacements on Angular workspace configuration?

I have a problem with Angular workspace configuration when I want to replace file, I will provide some information below:当我想替换文件时,我对 Angular 工作区配置有问题,我将在下面提供一些信息:

My app info:我的应用信息:

  • Angular: ~9.0.7 Angular:~9.0.7
  • Angular CLI: 9.1.10 Angular CLI:9.1.10

Structure结构

example
---e2e
---src
------environments
---------not-supported.html
---------index.prod.html
------index.html
------not-supported.html
---angular.json

angular.json angular.json

"architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/example",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/not-supported.html"
            ],
            "styles": [
              "src/styles.scss"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ],
              "index": {
                "input": "src/index.prod.html",
                "output": "index.html"
              }
            },
            "replaceNotSupported": {
              "fileReplacements": [
                {
                  "replace": "src/not-supported.html",
                  "with": "src/environments/not-supported.html"
                }
              ]
            }
          }
        },

Script build on package.json file在 package.json 文件上构建脚本

{
   "build:prod": "ng build --configuration=production,replaceNotSupported",
}

after I build successfully, Then content on not-supported.html file is not same with content file on environment/not-supported.html .在我构建成功后,然后not-supported.html文件上的内容与environment/not-supported.html上的内容文件不同。

How can I do that?我怎样才能做到这一点?

Dose anyone same problem with me?有人跟我有同样的问题吗?

Thanks!谢谢!

I don't know if you still care about this problem.:) try to use "assets":我不知道你是否还关心这个问题。:) 尝试使用“资产”:

    "assets": [
  "src/favicon.ico",
   {
     "input": "src/deployment/staging",
     "output": "/",
    "glob": "*.ico"
   }
   ]

setup assets to every configrations.为每个配置设置资产。 see enter link description here请参阅在此处输入链接描述

Replace files in a specific angular configuration替换特定 angular 配置中的文件

"production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                },
                {
                  "replace": "src/not-supported.html",
                  "with": "src/environments/not-supported.html"
                }
              ],

Run the following command for build:运行以下命令进行构建:

ng build --configuration=production

  • You have to provide all the fileReplacements in a specific configuration in this case 'production'在这种情况下,您必须在特定配置中提供所有 fileReplacements '生产'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM