有条件地包含依赖性 package.json

Conditionally Include Dependency package.json

我有一个 npm 项目中 Mac OS 只需要的依赖项,我想知道是否有某种方法可以仅在兼容平台是 [=] 时有条件地包含此依赖项 运行 npm install.

我愿意为此编写逻辑。在下面的情况下 grunt-appdmg 导致 npm 安装过程出错(出于相当明显的原因):

'/dev/cuttle/node_modules/grunt-appdmg/node_modules/appdmg/node_modules/ds-store/node_modules/macos-alias/build'
  CXX(target) Release/obj.target/volume/src/volume.o
../src/volume.cc:9:2: error: #error This platform is not implemented yet
 #error This platform is not implemented yet

package.json

{
  "name": "Cuttle",
  "homepage": "https://github.com/oakmac/cuttle",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/oakmac/cuttle.git"
  },
  "dependencies": {
    "fs-extra": "0.16.3",
    "open": "0.0.5",
    "winston": "0.8.3"
  },
  "devDependencies": {
    "grunt": "0.4.5",
    "grunt-contrib-less": "0.11.4",
    "grunt-contrib-watch": "0.6.1",
    "grunt-curl": "2.0.3",
    "grunt-download-atom-shell": "0.10.0",
    "grunt-appdmg": "0.2.0",
    "winresourcer": "0.9.0",
    "moment": "2.9.0",
    "shelljs": "0.3.0"
  }
}

您可以使用可选的依赖项。

在你的package.json中像这样:

"optionalDependencies":{
  "grunt-appdmg":"0.2.0"
}

有关 NPM documentation

的更多信息 如果失败,

npm install 将跳过它。

让我介绍一下 handpick,它可以让您定位和过滤多个依赖项。我写这个是为了加快 CI 阶段,这些阶段只需要 devDependencies 的一个片段,但最终会有更多的用例。这个项目是非常实验性的 - 请留下一些反馈。

安装

在您的系统上安装:

npm install handpick --global

用法

运行命令:

handpick [options]

-V, --version
-T, --target
-F, --filter
-M, --manager
-P, --path
-h, --help

例子

package.json 文件中定义非官方依赖项:

{
    "lintDependencies":
    {
        "eslint": "6.8.0",
        "eslint-config-redaxmedia": "2.0.0"
    },
    "testDependencies":
    {
        "chai": "4.2.0",
        "mocha": "7.1.1"
    }
}

安装 lintDependencies:

handpick --target=lintDependencies

通过 YARN 安装 devDependencieslintDependencies

handpick --target=devDependencies --target=lintDependencies --manager=yarn

安装 devDependencies 不带 testDependencies:

handpick --target=devDependencies --filter=testDependencies

在路径中安装 dependenciesdevDependencies

handpick --path=../shared