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

效果如下动图:

参考

相关推荐
java_heartLake12 分钟前
Vue3之性能优化
javascript·vue.js·性能优化
Swift社区16 分钟前
HarmonyOS 实践 - 设计模式在代码中的作用
javascript
哑巴语天雨36 分钟前
React+Vite项目框架
前端·react.js·前端框架
初遇你时动了情1 小时前
react 项目打包二级目 使用BrowserRouter 解决页面刷新404 找不到路由
前端·javascript·react.js
乔峰不是张无忌3301 小时前
【HTML】动态闪烁圣诞树+雪花+音效
前端·javascript·html·圣诞树
鸿蒙自习室1 小时前
鸿蒙UI开发——组件滤镜效果
开发语言·前端·javascript
m0_748250741 小时前
高性能Web网关:OpenResty 基础讲解
前端·openresty
机器之心2 小时前
图学习新突破:一个统一框架连接空域和频域
人工智能·后端
前端没钱2 小时前
从 Vue 迈向 React:平滑过渡与关键注意点全解析
前端·vue.js·react.js
汪洪墩2 小时前
【Mars3d】设置backgroundImage、map.scene.skyBox、backgroundImage来回切换
开发语言·javascript·python·ecmascript·webgl·cesium