使用 Yarn 从另一个脚本调用一个脚本

Calling one script from another with Yarn

下面是一个示例,说明如何使用 package.json 中的 npm 从另一个脚本中 运行 脚本。

yarn 等价于什么?

{
  "name": "npm-scripts-example",
  "version": "1.0.0",
  "description": "npm scripts example",
  "scripts": {
    "clean": "rimraf ./dist && mkdir dist",
    "prebuild": "npm run clean",

  }
}

你可以做到

{
  "name": "npm-scripts-example",
  "version": "1.0.0",
  "description": "npm scripts example",
  "scripts": {
    "clean": "rimraf ./dist && mkdir dist",
    "prebuild": "yarn run clean",

  }
}

然后命令 yarn run prebuild 应该可以工作了。 你也可以做 yarn run clean.

run cli 的文档:https://yarnpkg.com/lang/en/docs/cli/run/

改进 @l-faros 答案,Yarn 还支持更短的语法

{
 "scripts": {
    "clean": "rimraf ./dist && mkdir dist",
    "prebuild": "yarn clean",

  }
}

yarn prebuild到运行脚本命令。

语法允许省略 run 参数。 文档:https://classic.yarnpkg.com/en/docs/cli/#toc-user-defined-scripts