Angular 多项目管理

今天,我们谈谈,怎么使用 Angular 来管理多个项目呢?

在平常的开发中,我们一个项目建立一个仓库,不仅维护起来难,而且那个"无穷大"node_modules 也太尴尬了吧,多个项目多份 node_modules,要是同类型的项目,使用同份 node_modules 就好了 🥺

Demo 测试环境:

  • MacBook Air (M1, 2020)
  • Visual Studio Code - 版本: 1.83.1 (Universal)
  • http-server - ^14.1.1

初始项目

我们通过 NG-ZORRO 来初始化名为 jimmy-demo项目。那么,我们就可以在 package.json 上看到默认的启动和构建命令行:

json 复制代码
{
  "scripts": {
    "start": "ng serve",
    "build": "ng build"
  }
}

实际上,它等同于运行了👇:

json 复制代码
{
  "scripts": {
    "start": "ng serve jimmy-demo",
    "build": "ng build jimmy-demo"
  }
}

我们怎么知道它是默认运行了 jimmy-demo 项目呢?

这个问题的答案,我们可以通过查找 angular.json 文件来解答。

angular.json 就是默认导出了该项目:

json 复制代码
"defaultProject": "jimmy-demo"

很好,现在我们知道了,我们运行 ng serve 是运行导出的默认的文件。运行 ng serve jimmy-demo 是运行执行的 jimmy-demo 文件。

我们来看看 build 之后的目录结构,如下:

是的,这个导出的目录,也可以在 angular.json 中找到答案:

json 复制代码
"outputPath": "dist/jimmy-demo"

整个的 angular.json 文件如下:

json 复制代码
{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "cli": {
    "analytics": "5ac13bc0-9b57-4c18-9446-fdb86e29d674"
  },
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "jimmy-demo": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/jimmy-demo",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "jimmy-demo:build:production"
            },
            "development": {
              "browserTarget": "jimmy-demo:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "jimmy-demo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "jimmy-demo"
}

我们简单 run 一下打包文件:

http-server -p 8081 dist/jimmy-demo

多项目配置

👌,上面我们已经知道了,是通过配置 angular.json 文件实现了项目的运行和打包等。

那么,我们有没有发现,整个默认的项目,配置的文件,其实都是放在字段 projects 下面。

哦😯,那么我们可以通过在 projects 下面配置另外一个项目不就行了吗?

在开始前,我们先看看项目 jimmy-demo 下的文件:

嗯~,我们参考该目录结构做下调整,并添加一个名为 app01app02 的项目,并把默认的项目放在 app01。调整后的 jimmy-demo 目录如下:

然后我们更改下 app01angular.json 中文件,完整如下:

json 复制代码
{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "cli": {
    "analytics": "5ac13bc0-9b57-4c18-9446-fdb86e29d674"
  },
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "app01": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "projects/app01",
      "sourceRoot": "projects/app01/src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/app01",
            "index": "projects/app01/src/index.html",
            "main": "projects/app01/src/main.ts",
            "polyfills": "projects/app01/src/polyfills.ts",
            "tsConfig": "projects/app01/tsconfig.app.json",
            "inlineStyleLanguage": "scss",
            "assets": [
              "projects/app01/src/favicon.ico",
              "projects/app01/src/assets"
            ],
            "styles": [
              "projects/app01/src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "projects/app01/src/environments/environment.ts",
                  "with": "projects/app01/src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "app01:build:production"
            },
            "development": {
              "browserTarget": "app01:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "app01:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/app01/src/test.ts",
            "polyfills": "projects/app01/src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "inlineStyleLanguage": "scss",
            "assets": [
              "projects/app01/src/favicon.ico",
              "projects/app01/src/assets"
            ],
            "styles": [
              "projects/app01/src/styles.scss"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "app01"
}

然后更改下 app01/tsconfig.app.json 的路径指向:

json 复制代码
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/app",
    "types": []
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

然后我们重新启动项目:

ng serve app01

看到的效果和项目初始化的一样:

呀呀呀~试验成功!

那么,我们依瓢画葫芦,在 angular.json 中进行配置项目 app02(感兴趣的读者自行尝试)。

然后我们再更改下 package.json 的启动命令行:

json 复制代码
{
  "script": {
    "startApplication": "ng serve $APPLICATION",
    "start:app01": "APPLICATION=app01 npm run startApplication",
    "start:app02": "APPLICATION=app01 npm run startApplication",
    "buildApplication": "ng build $APPLICATION",
    "build:app01": "APPLICATION=app01 npm run buildApplication",
    "build:app02": "APPLICATION=app02 npm run buildApplication",
  }
}

我们打包这两个项目,得到 dist 内容。

最后,我们模拟下发布查看:

bash 复制代码
http-server -p 8081 dist/app01
http-server -p 8082 dist/app02

效果如下动图:

参考

相关推荐
NightCyberpunk17 分钟前
HTML、CSS
前端·css·html
xcLeigh27 分钟前
HTML5超酷响应式视频背景动画特效(六种风格,附源码)
前端·音视频·html5
zhenryx29 分钟前
前端-react(class组件和Hooks)
前端·react.js·前端框架
颜淡慕潇29 分钟前
【K8S系列】kubectl describe pod显示ImagePullBackOff,如何进一步排查?
后端·云原生·容器·kubernetes
lv程序媛30 分钟前
el-table表头前几列固定,后面几列根据接口返回的值不同展示不同
javascript·vue.js·elementui
ZwaterZ30 分钟前
el-table-column自动生成序号&&在序号前插入图标
前端·javascript·c#·vue
蒟蒻的贤1 小时前
vue学习11.21
javascript·vue.js·学习
Clarify1 小时前
docker部署go游戏服务器(进阶版)
后端
IT书架1 小时前
golang面试题
开发语言·后端·golang
初遇你时动了情2 小时前
uniapp 城市选择插件
开发语言·javascript·uni-app