Angular 和 NPM 依赖项 VS devDependencies

Angular and NPM dependencies VS devDependencies

在对此事进行大量阅读并遵循这个非常有用的 post 之后:What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?

我知道依赖项应该引用每个运行时库,而 devDependencies 引用每个对开发依赖项有用的东西。

但我现在很困惑。对于生产用途,我将不得不 ng build --production 我的 Angular 应用程序。如果我做 npm install --production,我什至不能用 ng build --production 编译。我 需要 npm install 并安装每个 devDependencies。知道这一点后,我尝试将所有内容都放在 devDependencies 下,然后我做了一个 ng build --prod,生成的 [dist] 文件夹包含了所有需要的东西,可以作为一个完整的 angular 应用程序。

所以,第一个问题是:Appart 使运行时库和开发库之间存在 "on paper" 差异,我为什么要使用依赖项?

其他问题:为了避免依赖项的依赖项的漏洞,我可以通过将此库放在 package.json 中来强制使用特定版本(只要主要版本相同)。但是我想避免将这个库放在 dependencies 或 devDependencies 中,我应该把这个库放在 peerDependencies 下吗?可选依赖项?捆绑依赖?

感谢您对此的任何意见。

编辑

示例 package.json 已被要求:

{
  "name": "ANGULAR_PROJECT",
  "version": "X.Y.Z",
  "repository": {
    "type": "git",
    "url": "A_GIT_URL"
  },
  "scripts": {
    "ng": "ng",
    "test": "ng test",
    "e2e": "protractor e2e/conf/protractor.conf.js",
    "e2e:dev": "protractor e2e/conf/protractor-dev.conf.js",
  },
  "private": true,
  "dependencies": {
  },
  "devDependencies": {
    "@angular/pwa": "^0.12.4",
    "@angular/animations": "^8.2.11",
    "@angular/cdk": "^8.2.3",
    "@angular/common": "^8.2.11",
    "@angular/compiler": "^8.2.11",
    "@angular/core": "^8.2.11",
    "@angular/forms": "^8.2.11",
    "@angular/material": "^8.2.3",
    "@angular/material-moment-adapter": "^8.2.3",
    "@angular/platform-browser": "^8.2.11",
    "@angular/platform-browser-dynamic": "^8.2.11",
    "@angular/platform-server": "^8.2.11",
    "@angular/router": "^8.2.11",
    "@angular/service-worker": "^8.2.11",
    "@mat-datetimepicker/core": "^2.0.1",
    "@mat-datetimepicker/moment": "^2.0.1",
    "@ngrx/effects": "8.4.0",
    "@ngrx/router-store": "8.4.0",
    "@ngrx/schematics": "8.4.0",
    "@ngrx/store": "8.4.0",
    "@ngrx/store-devtools": "8.4.0",
    "@swimlane/ngx-charts": "12.0.1",
    "angular-gridster2": "^8.2.0",
    "angular-particle": "^1.0.4",
    "array-flat-polyfill": "^1.0.1",
    "apollo-angular": "^1.8.0",
    "apollo-angular-link-http": "^1.9.0",
    "apollo-cache-inmemory": "^1.6.3",
    "apollo-client": "^2.6.4",
    "apollo-link": "^1.2.13",
    "classlist.js": "^1.1.20150312",
    "core-js": "^2.5.4",
    "graphql": "14.0.0",
    "graphql-tag": "^2.10.1",
    "hammerjs": "^2.0.8",
    "json-server": "^0.14.2",
    "jsonpath": "^1.0.2",
    "moment": "^2.22.2",
    "ngrx-store-freeze": "^0.2.4",
    "ngrx-store-localstorage": "^8.0.0",
    "ngx-cacheable": "^1.2.5",
    "ngx-parallax": "^4.0.0",
    "node-sass": "4.13.1",
    "rxjs": "^6.5.3",
    "web-animations-js": "^2.3.2-pr208",
    "xlsx": "^0.14.2",
    "zone.js": "~0.9.1",
    ----------------------------------------------- REAL DEV DEPENDENCIES UNDER
    "@angular-devkit/build-angular": "^0.803.10",
    "@angular/cli": "^8.3.10",
    "@angular/compiler-cli": "^8.2.11",
    "@angular/language-service": "^8.2.11",
    "@commitlint/cli": "^7.2.1",
    "@types/chai": "^4.2.6",
    "@types/cucumber": "^6.0.0",
    "@types/jasmine": "~3.4.6",
    "@types/jasminewd2": "~2.0.8",
    "@types/node": "~8.9.4",
    "chai": "^4.2.0",
    "codelyzer": "~5.2.0",
    "commitizen": "^4.0.3",
    "conventional-changelog-cli": "^2.0.1",
    "cucumber": "5.1.0",
    "cucumber-html-reporter": "^5.0.2",
    "cz-customizable": "^5.3.0",
    "cz-customizable-ghooks": "^1.5.0",
    "husky": "^4.2.1",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.4.1",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "karma-sonarqube-reporter": "^1.2.5",
    "karma-sonarqube-unit-reporter": "^0.0.21",
    "karma-spec-reporter": "0.0.32",
    "lint-staged": "^8.1.0",
    "mkdirp": "^0.5.1",
    "ng-mocks": "^8.1.0",
    "prettier": "^1.15.3",
    "protractor": "5.4.2",
    "protractor-cucumber-framework": "^6.2.0",
    "rxjs-compat": "^6.3.3",
    "ts-comparators": "^1.0.0",
    "ts-mockery": "1.2.0",
    "ts-node": "~7.0.1",
    "tslint": "~5.11.0",
    "tslint-config-prettier": "^1.18.0",
    "tslint-plugin-prettier": "^2.0.1",
    "typescript": "^3.1.6"
  },
  "peerDependencies": {

  }
}

除了在运行时库和开发库之间做出“纸上”区别之外,我为什么要使用依赖项?

回应:做你想做的没有区别。但要知道:

  • 了解一个依赖项是生产依赖项还是开发依赖项是很好的,即使它是“纸上”的差异,所以尽量准确。
  • 要确定最终构建中是否真的使用了依赖项,您可以为此使用特殊的库。我偏爱 https://www.npmjs.com/package/webpack-bundle-analyser,因为它可以通过搜索字段搜索依赖项
  • 如果您需要使用带有 --prod 的一些 npm 命令,这些将只使用 package.json 中的“依赖项”堆栈。 npm ls @angular-devkit/build-angular --json --prod 如果此依赖项在“devDependencies”中,则不会 return 任何结果。但如果@angular-devkit/build-angular 在“dependencies”
  • 中,它将 return 结果

另一个问题: 为了避免依赖项的依赖项上的漏洞,我可以通过将此库放在 package.json 中来强制使用特定版本(只要主要版本相同)。但是我想避免将这个库放在 dependencies 或 devDependencies 中,我应该把这个库放在 peerDependencies 下吗?可选依赖项? bundledDependencies ?

响应:最好的方法是使用 npm 工具:npm-force-resolutions 并且不要在任何这些条目下引用这些依赖项

关于 npm-force-resolutions 的更多信息:例如: