配置
¥Configuration
Lerna 的配置分为两个文件:lerna.json
和 nx.json
。
¥Lerna's configuration is split into two files: lerna.json
and nx.json
.
Lerna.json
npmClient
如果你不使用 npm
作为包管理器(例如,如果你使用 yarn
或 pnpm
),那么设置此值很重要,以便 lerna 在解析配置和包时可以调整其一些内部逻辑。对于 pnpm
来说尤其如此,因为它使用单独的 pnpm-workspaces.yaml
文件来定义其工作区配置。
¥It is important to set this value if you are not using npm
as your package manager (e.g. if you are using yarn
or pnpm
) so that lerna can adjust some of its internal logic when resolving configuration and packages. This is particularly true in the case of pnpm
because it uses a separate pnpm-workspaces.yaml
file to define its workspaces configuration.
packages
默认情况下,lerna 将尝试重用你从你选择的包管理器中获得的任何 workspaces
配置。如果你希望指定 lerna 操作的可用包的子集,你可以使用 packages
属性,该属性将告诉 Lerna 在哪里查找 package.json
文件。
¥By default, lerna will try and reuse any workspaces
configuration you may have from your package manager of choice. If you prefer to specify a subset of your available packages for lerna to operate on, you can use the packages
property which will tell Lerna where to look for package.json
files.
{
"packages": ["packages/*"]
}
version
Lerna 有两种发布包的模式:fixed
和 independent
。当使用固定模式时,所有受影响的包将使用相同的版本发布。最后发布的版本记录在 lerna.json
中如下:
¥Lerna has two modes of publishing packages: fixed
and independent
. When using the fixed mode, all the affected packages will be published using the same version. The last published version is recorded in lerna.json
as follows:
{
"version": "1.2.0"
}
当使用独立模式时,每个包都是单独版本的,lerna.json
将如下所示:
¥When using the independent mode, every package is versioned separately, and lerna.json
will look as follows:
{
"version": "independent"
}
有关详细信息,请参阅 版本和发布文档。
¥See the version and publish docs for more details.
commands
lerna.json
文件还可以对每个命令的选项进行编码,如下所示:
¥The lerna.json
files can also encode options for each command like so:
{
"command": {
"version": {
"allowBranch": "main",
"conventionalCommits": true
}
}
}
在 API 文档 中查找可用选项。
¥Find the available options in the API docs.
Nx.json
注意:"{projectRoot}" 和 "{workspaceRoot}" 是任务运行程序支持的特殊语法,当命令运行时,它们将在内部进行适当的插值。因此,你不应将 "{projectRoot}" 或 "{workspaceRoot}" 替换为固定路径,因为这会降低你的配置灵活性。
¥NOTE: "{projectRoot}" and "{workspaceRoot}" are special syntax supported by the task-runner, which will be appropriately interpolated internally when the command runs. You should therefore not replace "{projectRoot}" or "{workspaceRoot}" with fixed paths as this makes your configuration less flexible.
{
"namedInputs": {
"default": ["{projectRoot}/**/*"],
"prod": ["!{projectRoot}/**/*.spec.tsx"]
},
"targetDefaults": {
"build": {
"dependsOn": ["prebuild", "^build"],
"inputs": ["prod", "^prod"],
"outputs": ["{projectRoot}/dist"],
"cache": true
},
"test": {
"inputs": ["default", "^prod", "{workspaceRoot}/jest.config.ts"],
"cache": true
}
}
}
目标默认值
¥Target Defaults
目标是 npm 脚本名称。你可以在 targetDefaults
部分的存储库中添加与每个项目的构建脚本关联的元数据。
¥Targets are npm script names. You can add metadata associated with say the build script of each project in the repo in
the targetDefaults
section.
cache
当设置为 true
时,告诉 Nx 缓存运行脚本的结果。在大多数存储库中,所有非长时间运行的任务(即不是 serve
)都应该是可缓存的。
¥When set to true
, tells Nx to cache the results of running the script. In most repos all
non-long running tasks (i.e., not serve
) should be cacheable.
dependsOn
目标可以依赖于其他目标。一个常见的场景是在构建项目之前必须先构建项目的依赖。dependsOn
属性可用于定义单个目标的依赖。
¥Targets can depend on other targets. A common scenario is having to build dependencies of a project first before
building the project. The dependsOn
property can be used to define the dependencies of an individual target.
"dependsOn": [ "prebuild", "^build"]
告诉 Nx,每个构建脚本都需要同一个项目的预构建脚本和所有依赖的构建脚本首先运行。
¥"dependsOn": [ "prebuild", "^build"]
tells Nx that every build script requires the prebuild script of the same
project and the build script of all the dependencies to run first.
inputs & namedInputs
inputs
数组告诉 Nx 要考虑什么来确定脚本的特定调用是否应该缓存命中。输入分为三种类型:
¥The inputs
array tells Nx what to consider to determine whether a particular invocation of a script should be a cache
hit or not. There are three types of inputs:
文件集
¥Filesets
示例:
¥Examples:
-
{projectRoot}/**.*.ts
-
与
{fileset: "{projectRoot}/**/*.ts"}
相同¥same as
{fileset: "{projectRoot}/**/*.ts"}
-
{workspaceRoot}/jest.config.ts
-
与
{fileset: "{workspaceRoot}/jest.config.ts}
相同¥same as
{fileset: "{workspaceRoot}/jest.config.ts}
运行时输入
¥Runtime Inputs
示例:
¥Examples:
{runtime: "node -v"}
节点结果值经过哈希处理,因此永远不会显示。
¥Node the result value is hashed, so it is never displayed.
环境变量
¥Env Variables
示例:
¥Examples:
{env: "MY_ENV_VAR"}
节点结果值经过哈希处理,因此永远不会显示。
¥Node the result value is hashed, so it is never displayed.
命名输入
¥Named Inputs
示例:
¥Examples:
-
inputs: ["prod"]
-
与
inputs: [{input: "prod", projects: "self"}]
相同¥same as
inputs: [{input: "prod", projects: "self"}]
通常相同的 glob 会出现在许多地方(例如,prod 文件集将排除所有项目的规范文件)。由于保持它们同步很容易出错,因此我们建议定义命名输入,然后你可以在所有这些位置引用它们。
¥Often the same glob will appear in many places (e.g., prod fileset will exclude spec files for all projects).. Because keeping them in sync is error-prone, we recommend defining named inputs, which you can then reference in all of those places.
使用 ^
¥Using ^
示例:
¥Examples:
-
inputs: ["^prod"]
-
与
inputs: [{input: "prod", projects: "dependencies"}]
相同¥same as
inputs: [{input: "prod", projects: "dependencies"}]
与 dependsOn
类似,"^" 符号表示 "dependencies"。这是一个非常重要的想法,所以让我们用一个例子来说明它。
¥Similar to dependsOn
, the "^" symbols means "dependencies". This is a very important idea, so let's illustrate it with
an example.
"test": {
"inputs": [ "default", "^prod" ]
}
上面的配置意味着测试目标依赖于给定项目的所有源文件,并且仅依赖于其依赖的 prod 源(非测试源)。换句话说,它将测试源视为私有的。如果你的 remixapp
项目依赖于 header
库,则更改 header
测试不会对 remixapp
测试目标产生任何影响。
¥The configuration above means that the test target depends on all source files of a given project and only prod
sources (non-test sources) of its dependencies. In other words, it treats test sources as private. If your remixapp
project depends on the header
library, changing the header
tests will not have any effect on the remixapp
test
target.
outputs
"outputs": ["{projectRoot}/dist"]
告诉 Nx 构建脚本将在哪里创建文件工件。提供的值实际上是默认值,因此在这种情况下我们可以省略它。"outputs": []
告诉 Nx 测试目标不会在磁盘上创建任何工件。你可以列出任意数量的输出。你还可以使用 glob 或单个文件作为输出。
¥"outputs": ["{projectRoot}/dist"]
tells Nx where the build script is going to create file artifacts. The provided
value is actually the default, so we can omit it in this case. "outputs": []
tells Nx that the test target doesn't
create any artifacts on disk. You can list as many outputs as you many. You can also use globs or individual files as
outputs.
通常不需要此配置。Nx 具有合理的默认值,可以实现上述配置。
¥This configuration is usually not needed. Nx comes with reasonable defaults which implement the configuration above.
项目特定配置
¥Project-Specific Configuration
对于许多项目相似的工作区,nx.json
将包含整个 Nx 配置。有时,将特定于项目的配置放置在项目的 package.json
文件中很有用。
¥For a lot of workspaces, where projects are similar, nx.json
will contain the whole Nx configuration. Sometimes, it's
useful to have a project-specific configuration, which is placed in the project's package.json
file.
{
"name": "parent",
"scripts": {
"build": "...",
"test": "..."
},
"dependencies": {...},
"nx": {
"namedInputs": {
"prod": [
"!{projectRoot}/**/*.test.tsx",
"{workspaceRoot}/configs/webpack.conf.js"
]
},
"targets": {
"build": {
"dependsOn": [
"^build"
],
"inputs": [
"prod",
"^prod"
],
"outputs": [
"{workspaceRoot}/dist/parent"
]
}
}
"implicitDependencies": ["projecta", "!projectb"]
}
}
请注意,nx.json
中定义的 namedInputs
和 targetDefaults
只是默认值。如果你采用该配置并将其复制到每个项目的 package.json
文件中,结果将是相同的。
¥Note, the namedInputs
and targetDefaults
defined in nx.json
are simply defaults. If you take that configuration
and copy it into every project's package.json
file, the results will be the same.
换句话说,每个项目都有一组命名输入,其定义为:{...namedInputsFromNxJson, ...namedInputsFromProjectsPackageJson}
。每个目标/脚本的 dependsOn
都定义为 dependsOnFromProjectsPackageJson || dependsOnFromNxJson
。这同样适用于 inputs
和 outputs
。
¥In other words, every project has a set of named inputs, and it's defined as: {...namedInputsFromNxJson, ...namedInputsFromProjectsPackageJson}
. Every target/script's dependsOn
is defined
as dependsOnFromProjectsPackageJson || dependsOnFromNxJson
. The same applies to inputs
and outputs
.
inputs & namedInputs
为给定目标定义 inputs
将替换 nx.json
中定义的该目标名称的输入集。使用伪代码 inputs = packageJson.targets.build.inputs || nxJson.targetDefaults.build.inputs
。
¥Defining inputs
for a given target would replace the set of inputs for that target name defined in nx.json
.
Using pseudocode inputs = packageJson.targets.build.inputs || nxJson.targetDefaults.build.inputs
.
你还可以定义和重新定义命名输入。这实现了一个关键用例,你的 nx.json
可以定义如下内容(适用于每个项目):
¥You can also define and redefine named inputs. This enables one key use case, where your nx.json
can define things
like this (which applies to every project):
"test": {
"inputs": [
"default",
"^prod"
]
}
项目可以定义其 prod 文件集,而无需重新定义 test
目标的输入。
¥And projects can define their prod fileset, without having to redefine the inputs for the test
target.
{
"name": "parent",
"scripts": {
"build": "...",
"test": "..."
},
"dependencies": {...},
"nx": {
"namedInputs": {
"prod": [
"!{projectRoot}/**/*.test.js",
"{workspacRoot}/jest.config.js"
]
}
}
}
在这种情况下,Nx 将为每个项目使用正确的 prod
输入。
¥In this case Nx will use the right prod
input for each project.
dependsOn
为给定目标定义 dependsOn
将替换 nx.json
中定义的目标名称的 dependsOn
。使用伪代码 dependsOn = packageJson.targets.build.dependsOn || nxJson.targetDefaults.build.dependsOn
。
¥Defining dependsOn
for a given target would replace dependsOn
for that target name defined in nx.json
.
Using pseudocode dependsOn = packageJson.targets.build.dependsOn || nxJson.targetDefaults.build.dependsOn
.
outputs
为给定目标定义 outputs
将替换 nx.json
中定义的目标名称的 outputs
。使用伪代码 outputs = packageJson.targets.build.outputs || nxJson.targetDefaults.build.outputs
。
¥Defining outputs
for a given target would replace outputs
for that target name defined in nx.json
.
Using pseudocode outputs = packageJson.targets.build.outputs || nxJson.targetDefaults.build.outputs
.
implicitDependencies
"implicitDependencies": ["projecta", "!projectb"]
行告诉 Nx,父项目依赖于 projecta
,即使其 package.json
中没有依赖。Nx 将以与处理显式依赖相同的方式处理此类依赖。它还告诉 Nx,即使存在对 projectb
的显式依赖,也应该忽略它。
¥The "implicitDependencies": ["projecta", "!projectb"]
line tells Nx that the parent project depends on projecta
even
though there is no dependency in its package.json
. Nx will treat such a dependency in the same way it treats explicit
dependencies. It also tells Nx that even though there is an explicit dependency on projectb
, it should be ignored.
附加配置
¥Additional Configuration
有关配置任务和缓存的其他方法,请参阅相关的 NX 文档。
¥For additional ways to configure tasks and caching, see the relevant Nx documentation.