Skip to main content

生命周期脚本

包可以在其清单的 scripts 字段中定义当包管理器执行特定工作流时应执行的各种操作。

¥Packages can define in the scripts field of their manifest various actions that should be executed when the package manager executes a particular workflow.

信息

请注意,我们不支持 npm 中最初存在的每个生命周期脚本。这是基于以下观察而做出的深思熟虑的决定:太多的生命周期脚本使得很难知道在哪种情况下使用哪个脚本,从而导致混乱和错误。如果提供了引人注目的用例,我们愿意根据具体情况添加缺失的功能。

¥Note that we don't support every single lifecycle script originally present in npm. This is a deliberate decision based on the observation that too many lifecycle scripts make it difficult to know which one to use in which circumstances, leading to confusion and mistakes. We are open to add the missing ones on a case-by-case basis if compelling use cases are provided.

特别是,我们有意不支持用户定义脚本(例如 prestart)的任意 prepost 钩子。此行为导致脚本隐式而不是显式,从而混淆了执行流程。它有时也会导致令人惊讶的行为,例如 yarn serve 也运行 yarn preserve

¥In particular, we intentionally don't support arbitrary pre and post hooks for user-defined scripts (such as prestart). This behavior caused scripts to be implicit rather than explicit, obfuscating the execution flow. It also sometimes led to surprising behaviors, like yarn serve also running yarn preserve.

prepackpostpack

¥prepack and postpack

这些脚本在每次调用 yarn pack 的开始和结束时被调用。它们分别用于将你的包从开发状态转变为生产状态,并清理任何残留的工件。例如,典型的 prepack 脚本会在源目录上调用 Babel 或 TypeScript,将 .ts 文件转换为 .js 文件。

¥Those script are called right at the beginning and the end of each call to yarn pack. They are respectively meant to turn your package from development into production, and cleanup any lingering artifact. For instance, a typical prepack script would call Babel or TypeScript on the source directory to turn .ts files into .js files.

信息

虽然很少被直接调用,但 yarn pack 是 Yarn 的重要组成部分。每次 Yarn 必须从 "raw" 源(例如 Git 存储库)获取依赖时,它都会自动运行 yarn installyarn pack 来生成要使用的包。

¥Although rarely called directly, yarn pack is a crucial part of Yarn. Each time Yarn has to fetch a dependency from a "raw" source (such as a Git repository), it will automatically run yarn install and yarn pack to generate the package to use.

prepublish

此脚本在 yarn npm publish 之前调用,甚至在包打包之前。这是你要检查项目是否处于正常状态的地方。

¥This script is called before yarn npm publish before the package has even been packed. This is the place where you'll want to check that the project is in an ok state.

警告

因为它只在预发布时调用,所以预发布钩子不应该有副作用。特别是不要在 prepublish 中转译包源,因为直接使用你的存储库(例如通过 git: 协议)的人将无法使用你的项目。相反,使用 prepack

¥Because it's only called on prepublish, the prepublish hook shouldn't have side effects. In particular don't transpile the package sources in prepublish, as people consuming directly your repository (such as through the git: protocol) wouldn't be able to use your project. Instead, use prepack.

postinstall

此脚本在包依赖树以任何方式更改后调用 - 通常是在添加、删除或更新依赖(或传递依赖)之后,但有时也会在项目配置或环境发生变化时调用(例如,在更改 Node.js 版本时)。

¥This script is called after the package dependency tree changed in any way -- usually after a dependency (or transitive dependency) got added, removed, or updated, but also sometimes when the project configuration or environment changed (for example when changing the Node.js version).

它保证按拓扑顺序调用(换句话说,你的依赖的 postinstall 脚本将始终在你的脚本之前运行)。

¥It is guaranteed to be called in topological order (in other words, your dependencies' postinstall scripts will always run before yours).

为了向后兼容,如果存在 preinstallinstall 脚本,则在从同一包运行 postinstall 脚本之前调用它们。一般来说,最好使用 postinstall 而不是这两个。

¥For backwards compatibility, the preinstall and install scripts, if presents, are called right before running the postinstall script from the same package. In general, prefer using postinstall over those two.

警告

应不惜一切代价避免使用安装后脚本,因为它们会使安装速度变慢且风险更大。许多用户会拒绝安装具有 postinstall 脚本的依赖。此外,由于输出不是开箱即用的,因此使用它们向用户打印消息将无法按预期工作。

¥Postinstall scripts should be avoided at all cost, as they make installs slower and riskier. Many users will refuse to install dependencies that have postinstall scripts. Additionally, since the output isn't shown out of the box, using them to print a message to the user will not work as you expect.

环境变量

¥Environment variables

运行脚本和二进制文件时,通常会提供一些环境变量:

¥When running scripts and binaries, some environment variables are usually made available:

变量描述
$INIT_CWD调用脚本的目录。这与 cwd 不同,对于脚本来说,cwd 始终等于最近的包根。
$PROJECT_CWD文件系统上的项目根目录。
$npm_package_name正在运行的包的名称。
$npm_package_version正在运行的包的版本。
$npm_package_json正在运行的包的 package.json 的绝对路径。
$npm_execpathYarn 二进制文件的绝对路径。
$npm_node_execpathNode 二进制文件的绝对路径。
$npm_config_user_agent定义当前使用的 Yarn 版本的字符串。
$npm_lifecycle_event脚本或生命周期事件的名称(如果相关)。