Skip to main content

配置已发布的文件

¥Configuring Published Files

将包发布到注册表时,默认情况下会发布包源目录中的所有内容。这并不总是最佳的,因为通常存在仅与开发相关的文件,例如测试和配置文件,并且你可能首先编译源文件并将它们输出到 monorepo 设置中的集中位置。

¥When publishing a package to a registry, the default is to publish everything in the package's source directory. This is not always optimal, since there are often files only relevant for development, such as tests and configuration files, and it could be that you first compile your source files and output them to a centralized location in a monorepo setup.

Lerna 提供了许多配置选项,以确保仅打包适当的文件并将其发布到注册表。

¥Lerna provides a number of configuration options to ensure that only the appropriate files are packed and published to a registry.

"files" 和 .gitignore

¥"files" and .gitignore

Lerna 总是使用 npm 的工具进行发布,并且它有一些内置的方法来包含或排除文件。配置已发布包中包含哪些文件的最简单方法是通过 package.json.gitignore 中的 "files" 属性。有关 npm 如何识别要发布的文件的更多信息,请参阅 npm 文档

¥Lerna always publishes using npm's tooling, and it has a few built in ways to include or exclude files. The easiest way to configure which files are included in the published package are via the "files" property in package.json and .gitignore. See the npm documentation for more information on how npm recognizes files for publishing.

--contents [旧版 -> 更喜欢 --directory]

¥--contents [legacy -> prefer --directory]

许多命令(包括 lerna publish)支持通用 --contents 选项,该选项设置要为所有包发布的目录。

¥A number of commands, including lerna publish, support a generalized --contents option, which sets the directory to publish for ALL packages.

仅当你的 monorepo 中的包具有简单、统一的输出结构时,这才对发布有用。传递给 --contents 的参数必须是每个正在发布的包中存在的子目录。详细信息请参见 lerna 发布文档

¥This is only useful for publishing if the packages in your monorepo have a simplistic, uniform output structure. The argument passed to --contents must be a subdirectory that exists within every package being published. See the lerna publish documentation for details.

在 v7 中,我们为 lerna publish 引入了更强大、更集中的 --directory 选项。请优先选择该选项,而不是 --contents 选项,后者将来可能会被弃用。

¥In v7, we introduced a more powerful, more focused --directory option for lerna publish. Please prefer that over the --contents option, which will likely be deprecated in future.

--directory

在 v7 中,我们为 lerna publish 引入了更强大、更集中的 --directory 选项。

¥In v7, we introduced a more powerful, more focused --directory option for lerna publish.

它可以在你的 lerna.json 中进行配置,并支持使用以下动态占位符:{workspaceRoot}{projectRoot}{projectName}。这些值将在发布时动态替换。

¥It can be configured in your lerna.json and supports using the following dynamic placeholders: {workspaceRoot}, {projectRoot}, {projectName}. These values will be dynamically replaced at publish time.

这意味着你现在可以简洁地表达风格统一的设置,但在所有包中并非完全相同。

¥This means you can now succinctly express setups which are uniform in terms of their style, but not literally identical across all packages.

例如,假设我们有一个 monorepo,我们在其中构建所有包,并将它们的输出设置为转到集中位置(例如,这在 Nx 工作区中很常见):

¥For example, let's say we have a monorepo where we build all packages and their outputs are set to go to a centralized location (as is common in Nx workspaces, for example):

我们有 packages/package-a 将其构建输出写入 dist/packages/package-a,以及 packages/package-b 将其构建输出写入 dist/packages/package-b

¥We have packages/package-a which writes its build output to dist/packages/package-a, and packages/package-b which writes its build output to dist/packages/package-b.

尽管路径完全不同,但我们现在可以使用占位符来表达一致的方法:

¥Even though the paths are strictly different, we have a consistent approach we can now express using the placeholders:

{workspaceRoot}/dist/{projectRoot}

{workspaceRoot} 将替换为 lerna 存储库的绝对路径,如果是 package-a,则 {projectRoot} 将被替换为 packages/package-a;如果是 package-b,则将替换为 packages/package-b

¥{workspaceRoot} will be replaced by the absolute path to our lerna repo, and {projectRoot} will be replace by packages/package-a in the case of package-a, and packages/package-b in the case of package-b.

我们在 lerna.json 中的应用方式如下:

¥The way we apply that in the lerna.json is as follows:

// lerna.json
{
"version": "1.0.0",
"command": {
"publish": {
"directory": "{workspaceRoot}/dist/{projectRoot}"
}
}
}

如果你需要为特定包完全自定义某些内容,你还可以设置或覆盖包的 package.json 中的发布目录选项。

¥You can also set or override the publish directory option within a package's package.json, if you need to something completely custom for that particular package.

从存储库根目录中的 dist/packages/foo 文件夹发布的包的示例配置:

¥An example configuration for a package that publishes from a dist/packages/foo folder in the root of the repo:

// packages/foo/package.json
{
"name": "foo",
"version": "1.0.0",
"lerna": {
"command": {
"publish": {
"directory": "../../dist/packages/foo"
}
}
}
}
信息

你需要确保你的自定义目录位置包含将用于注册表发布的有效 package.json。如果你需要涉及更复杂的自定义逻辑,你可以通过生命周期脚本(例如 prepareprepublishOnlyprepack)创建它,或者只是通过将其配置为资源来自动从包的源中复制它。有关完整详细信息,请参阅即将发布的在已发布的包中包含其他资源的部分。

¥You will need to make sure that your custom directory location contains a valid package.json which will be used for the registry publish. You could create this via a lifecycle script such as prepare, prepublishOnly, or prepack if you need more complex custom logic involved, or simply have it copied for you from the package's source automatically by configuring it as an asset. See the upcoming section on Including Additional Assets in Published Packages for full details.

如果你想让你的包之一像标准 lerna 包一样运行并从源代码发布,你可以像这样覆盖它的发布配置:

¥If you wanted to make one of your packages behave like a standard lerna package and publish from source, you could override its publish config like so:

// packages/foo/package.json
{
"name": "foo",
"version": "1.0.0",
"lerna": {
"command": {
"publish": {
"directory": "."
}
}
}
}

在已发布的包中包含其他资源

¥Including Additional Assets in Published Packages

Lerna 可以将文件从源目录复制到指定用于发布的目录。正如 directory 选项一样,可以在 lerna.json 中(包括在资源定义中使用动态占位符)或特定包的 package.json 中进行配置。

¥Lerna can copy files from your source directory to the directory specified for publishing. Just as with the directory option, this can be configured in the lerna.json (including using dynamic placeholders within asset definitions), or within the package.json of a particular package.

无论在哪个文件中配置,"assets" 属性都应该是具有 "from""to" 属性的 glob 模式或对象的数组。"from" 属性应该是与源目录中的文件匹配的特定文件或通配符模式,"to" 属性是将文件复制到发布目录中的路径。

¥Regardless of which file it is configured in, the "assets" property should be an array of glob patterns or objects with a "from" and "to" property. The "from" property should be a specific file or glob pattern that matches files in the source directory, and the "to" property is the path to copy the file to within the publish directory.

此示例包将其输出构建到根 dist/packages/bar 目录。Lerna 配置为将其他文件复制到此目录,然后将 dist/packages/bar 的内容发布到 npm。

¥This example package builds its output to a root dist/packages/bar directory. Lerna is configured to copy additional files to this directory, then publish the contents of dist/packages/bar to npm.

// packages/bar/package.json
{
"name": "bar",
"version": "1.0.0",
"lerna": {
"command": {
"publish": {
"directory": "../../dist/packages/bar",
"assets": [
"README.md",
"package.json",
"docs/*.md",
{
"from": "static/images/*",
"to": "assets"
},
{
"from": "../../CONTRIBUTING.md",
"to": "./"
}
]
}
}
}
}

Lerna 将使用上述配置并将适当的文件复制到 dist 目录,生成如下结构:

¥Lerna will consume the above configuration and copy the appropriate files to the dist directory, producing a structure like this:

dist/packages/bar
├── assets
│ ├── my-image-1.png
│ └── my-image-2.png
├── CONTRIBUTING.md
├── docs
│ ├── my-doc-1.md
│ └── my-doc-2.md
├── package.json
└── README.md