简体   繁体   English

将环境配置添加到 angular.json 的正确方法是什么?

[英]What is the right way to add an environment configuration to angular.json?

I just updated angular cli to version 6. And of course my app is broken and have been trying to look at docs and SO questions trying to figure out how to add an environment to angular.json?我刚刚将 angular cli 更新到版本 6。当然,我的应用程序坏了,一直在尝试查看文档和 SO 问题,试图找出如何向 angular.json 添加环境? I want to be able to run the equivalent of ng serve --env=local and then have it run a local build in the local server... I have come part way and my angular.json file looks like this...我希望能够运行相当于 ng serve --env=local 的东西,然后让它在本地服务器中运行本地构建......我已经走到了一半,我的 angular.json 文件看起来像这样......

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "bb-admin": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "sass"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/bb-admin",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.sass"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            },
            "local": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.local.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "bb-admin:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "bb-admin:build:production"
            },
            "local": {
              "browserTarget": "bb-admin:build:local"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "bb-admin:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.sass"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "bb-admin-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "bb-admin:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "bb-admin"
}

But when I run ng serve --configuration=local但是当我运行 ng serve --configuration=local

I get the error.我得到了错误。

ERROR in Either route or analyzedModules has to be specified!必须指定路由或分析模块中的错误!

What's causing this how can I run a different environment file for building and running the cli server locally?是什么原因导致我如何运行不同的环境文件来在本地构建和运行 cli 服务器? Thanks!谢谢!

AFAIK This is not really documented anywhere yet. AFAIK 这还没有真正记录在任何地方。 However you should be able to override the build options you specify with browserTarget.但是,您应该能够覆盖您使用 browserTarget 指定的构建选项。 Alternatively, specify another build config in the build command section.或者,在构建命令部分指定另一个构建配置。

Best "docs" I've found so far are here https://blog.ninja-squad.com/2018/05/04/angular-cli-6.0/到目前为止我发现的最好的“文档”在这里https://blog.ninja-squad.com/2018/05/04/angular-cli-6.0/

UPDATE: There's an issue for this issue, https://github.com/angular/angular-cli/issues/10612更新:这个问题有一个问题, https://github.com/angular/angular-cli/issues/10612

Also some not-especially-amazing docs https://github.com/angular/angular-cli/wiki/angular-workspace .还有一些不太特别的文档https://github.com/angular/angular-cli/wiki/angular-workspace For now you must specify each config individually, regardless of overlapping options.现在,您必须单独指定每个配置,而不考虑重叠选项。 The only "workaround" I can think of for this is to use some other utility to merge json files pre serve/build我能想到的唯一“解决方法”是使用其他一些实用程序来合并 json 文件 pre serve/build

    "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "browserTarget": "bb-admin:build"
      },
      "configurations": {
        "local": {
          "browserTarget": "bb-admin:build:local"
        }
      }
    },

I add in package.json under "scripts" something like我在“脚本”下添加 package.json 类似

"myscript": "ng serve -c local-version"

Than I add to angular.json under "configurations"比我在“配置”下添加到 angular.json

"local-version": {
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.local.ts"
                }
            ]
        }

and under "serve"在“服务”下

"configurations": {
        "local-version": {
          "browserTarget": "your-app:build:local-version"
        }

I start the app with:我启动应用程序:

npm run myscript

Of course you have to create a file called environment.local.ts当然你必须创建一个名为 environment.local.ts 的文件

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

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