生命周期脚本
包可以在其清单的 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
)的任意 pre
和 post
钩子。此行为导致脚本隐式而不是显式,从而混淆了执行流程。它有时也会导致令人惊讶的行为,例如 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
.
prepack
和 postpack
¥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 install
和 yarn 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).
为了向后兼容,如果存在 preinstall
和 install
脚本,则在从同一包运行 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.