0

当您有一个要安装到当前项目或 NPM 包中的依赖项时,很容易确定它是“deps”还是“devDeps”,但是如何确定它是“deps”还是“ peerDeps'?

4

1 回答 1

0

我试着自己回答这个问题。看看是否有人同意或可以纠正我。

如何确定一个 deps 是否应该放入“devDependencies”或“peerDependencies”?

if (your source code /src/ depends on it) {
  if (you can't live without it) {
    if (you don't want to affect the version that consumer choose to install) {
      // This is usually for big or heavy deps, like framework level, for example: react, redux
      Put it into 'peerDependencies'
    } else {
      // This is regular case
      Put it into 'dependencies'
    }
  } else {
    // This is a special case, means it is an optional for you in just some of the use cases.
    // For example, [Swiper](https://github.com/nolimits4web/swiper), it supports react | vue | angular, but people use vue won't want to see a peerDeps of 'react', right?
    Do nothing
  }
} else if (only development environment depends on it) {
  // This is usually used for compile and test tools.
  Put it into 'devDependencies'
}

如果项目有,如何处理/安装本地环境peerDeps

if (you want to make package.json clear enough, without confusion) {
  Just Leave 'peerDeps' in 'peerDependencies'
  Then use 3rd party tool to install 'peerDeps' locally, without updating package.json.
  See https://github.com/helloint/peerdeps-test
} else {
  Put 'peerDeps' in 'devDependencies' as well as 'peerDependencies'
  This way, npm install will also install these 'peerDeps', just that they are not really 'devDeps'
}

注意:顶级项目不应该有直接的“peerDeps”。'peerDeps' 是下层包到上层包或项目的关系描述。对于project来说,已经是top了,不用说了。

于 2021-08-26T15:14:37.117 回答