代码之家  ›  专栏  ›  技术社区  ›  Sudarshan

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

  •  0
  • Sudarshan  · 技术社区  · 2 年前

    我有一个可用的角应用程序,其中几乎没有依赖关系 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" : "2022-04-13T08:20:47.990Z"
    

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

    我能做这个吗?如果是,如何?

    0 回复  |  直到 2 年前
        1
  •  0
  •   Sudarshan    2 年前

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

    但是,它不适用于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