将Angular CLI应用程序拆分为两个版本

时间:2019-01-11 23:25:55

标签: angular split configuration angular-cli

我使用CLI在Angular 7上构建了一个角度解决方案。该应用程序有2个区域。它们没有以任何方式连接,但是它们使用相同的代码库。

现在我可以转到http://localhost:4200/app1http://localhost:4200/app2。 但是它们在同一实例上运行。我想修改我的配置,以便可以写类似

ng build --app=app1

并获得独立解决方案。有什么建议,我该怎么办?

下面是我的angular.json文件。如您所见,我已经准备好用于开发,登台和生产的设置。这意味着目前,我可以写

ng build --build-optimizer --configuration=staging

并获得用于登台环境的构建。

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

1 个答案:

答案 0 :(得分:1)

使用cli,您可以在同一代码库(repo)中生成多个角度项目,并且可以分别构建,开发或服务它们。这样,您可以在前端级别实施微服务架构。如果您在同一个域中拥有不同的产品,并且希望通过不同的路线将它们分开,这是最好的方法。

以下是一些步骤,可以帮助您构建这种架构

  1. 生成多个角度应用程序:ng generate application <project-name> click在此处获取信息

  2. 分别构建并提供它们:ng build project-nameng serve project-name

  3. 配置路由:要使此路由成为可能,您必须配置nginx代理服务器。

您必须在nginx脚本中使用位置块才能从不同的项目提供服务,以下脚本仅用于演示,请参考docs以获得完整的实现。

server {
    listen 4200;

    location / {
        proxy_pass http://localhost:4201;  # your root project
    }

    location /app1 {
        proxy_pass http://localhost:4202;  # your app1 project
    }

    location /app2 {
        proxy_pass http://localhost:4203;  # your app2 project
    }

}

对于生产,您可以在位置块中提供相应项目的index.html文件