在 angular 中执行构建操作时在 `package.json` 中添加自定义字段

Add custom field(s) in `package.json` while performing build operation in angular

我有一个工作的 angular 应用程序,其中几乎没有依赖项。是Package.json

{
  "name": "ngrx-poc",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --port 4202",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~13.1.0",
    "@angular/common": "~13.1.0",
    "@angular/compiler": "~13.1.0",
    "@angular/core": "~13.1.0",
    "@angular/forms": "~13.1.0",
    "@angular/localize": "~13.1.0",
    "@angular/platform-browser": "~13.1.0",
    "@angular/platform-browser-dynamic": "~13.1.0",
    "@angular/router": "~13.1.0",
    "@lottiefiles/lottie-player": "^1.5.6",
    "@ng-bootstrap/ng-bootstrap": "^12.0.0",
    "@ngrx/store": "^13.0.2",
    "@popperjs/core": "^2.10.2",
    "apexcharts": "^3.33.2",
    "bootstrap": "^5.1.3",
    "lottie-web": "^5.9.1",
    "ng-apexcharts": "^1.7.0",
    "ng-otp-input": "^1.8.5",
    "ngx-lottie": "^8.0.1",
    "ngx-toastr": "^14.2.1",
    "rxjs": "~7.4.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~13.1.2",
    "@angular/cli": "~13.1.2",
    "@angular/compiler-cli": "~13.1.0",
    "@types/jasmine": "~3.10.0",
    "@types/node": "^12.11.1",
    "jasmine-core": "~3.10.0",
    "karma": "~6.3.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.1.0",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "~1.7.0",
    "typescript": "~4.5.2"
  }
}

现在,我想在 package.json 中添加名为 build_version ex

的自定义字段
"build_version" : "2022-04-13T08:20:47.990Z"

此 属性 应在构建时添加到 package.json 中。即当我将使用 ng build --prod

我可以这样做吗?如果是怎么办?

我尝试使用 replace-in-file 修改 package.json

但是,它不适用于 angular(或更具体地说在浏览器中),维护者也坚持相同。

https://github.com/adamreisnz/replace-in-file/issues/154#issue-1203080403

因此,我使用了它并且对我来说效果很好。

https://gist.github.com/Mustafa-Omran/bb0810c9e51a1453de8c298b524b217e?permalink_comment_id=4131520#gistcomment-4131520