任务管道配置
¥Task Pipeline Configuration
Lerna 将 npm 脚本(复刻进程等)的运行委托给 Nx。nx.json
文件是你可以配置 Nx 如何执行操作的地方。
¥Lerna delegates the running of npm scripts (forking processes etc) to Nx. The nx.json
file is the place where you can
configure how Nx does it.
如果没有 nx.json
,请运行 npx lerna add-caching
。
¥If you don't have nx.json
, run npx lerna add-caching
.
并行运行任务
¥Run Tasks in Parallel
如果要将运行脚本的进程数量增加到 5 个(默认情况下为 3),请传递以下命令:
¥If you want to increase the number of processes running the scripts to, say, 5 (by default, it is 3), pass the following:
npx lerna run build --concurrency=5
定义任务依赖(也称为任务管道)
¥Define Task Dependencies (aka Task Pipelines)
没有我们的帮助,Lerna 无法知道哪些目标(脚本)有先决条件,哪些没有。你可以在 nx.json
文件中定义任务依赖:
¥Without our help Lerna cannot know what targets (scripts) have prerequisites and which ones don't. You can define task dependencies in the nx.json
file:
{
...
"targetDefaults": {
"build": {
"dependsOn": ["^build"]
}
}
}
由此,Lerna 知道在构建项目之前,需要首先构建其所有依赖。然而,对测试没有任何限制。
¥With this, Lerna knows that before it can build a project, it needs to build all of its dependencies first. There are, however, no constraints on tests.
一旦定义了
targetDefaults
属性,排序标志就会被忽略。¥Once you define the
targetDefaults
property the sort flag is ignored.
这个机制非常灵活。让我们看一下这个例子:
¥This mechanism is very flexible. Let's look at this example:
{
...
"targetDefaults": {
"build": {
"dependsOn": ["^build", "prebuild"]
},
"test": {
"dependsOn": ["build"]
}
}
}
请注意,旧版本的 Nx 使用 targetDependency 而不是 targetDefaults。两者仍然有效,但建议使用 targetDefaults。
¥Note, older versions of Nx used targetDependencies instead of targetDefaults. Both still work, but targetDefaults is recommended.
^
符号(又名插入符号)仅表示依赖。因此,"test": { "dependsOn": ["build"] }
表示特定项目的 "test" 目标依赖于其自己的 "build" 目标在运行前已完成,而 "build": { "dependsOn": ["^build"] }
表示特定项目的 "build" 目标依赖于该项目的所有依赖的 "build" 目标在运行前已完成。
¥The ^
symbol (a.k.a the caret symbol) simply means dependencies. Therefore whereas "test": { "dependsOn": ["build"] }
means a particular project's "test" target depends on its own "build" target to have already completed before running, "build": { "dependsOn": ["^build"] }
means that a particular project's "build" target depends on the "build" target of all of the project's dependencies to have already completed before running.
当运行 lerna run test --scope=myproj
时,上面的配置会告诉 Lerna
¥When running lerna run test --scope=myproj
, the above configuration would tell Lerna to
-
为
myproj
运行test
命令¥Run the
test
command formyproj
-
但由于存在从
test -> build
定义的依赖,Lerna 首先为myproj
运行build
。¥But since there's a dependency defined from
test -> build
, Lerna runsbuild
formyproj
first. -
build
本身定义了对prebuild
(在同一个项目上)的依赖以及所有依赖的build
。因此,它将运行prebuild
脚本,并将为所有依赖运行build
脚本。¥
build
itself defines a dependency onprebuild
(on the same project) as well asbuild
of all the dependencies. Therefore, it will run theprebuild
script and will run thebuild
script for all the dependencies.
请注意,Lerna 在开始运行测试之前不必运行所有构建。只要满足约束条件,任务编排器就会并行运行尽可能多的任务。
¥Note, Lerna doesn't have to run all builds before it starts running tests. The task orchestrator will run as many tasks in parallel as possible as long as the constraints are met.
像这样的情况很常见:
¥Situations like this are pretty common:
因为我们在 nx.json
中描述了规则,所以它们将适用于存储库中的所有项目。你还可以通过将特定于项目的规则添加到项目的 package.json
。
¥Because we described the rules in nx.json
, they will apply to all the projects in the repo. You can also define
project-specific rules by adding them the project's package.json
.
{
...
"nx": {
"targets": {
"test": {
"dependsOn": [
"build"
]
}
}
}
}