过滤
过滤允许你将命令限制于软件包的特定子集。
pnpm 支持丰富的选择器语法,可以通过名称或关系选择包。
可通过 --filter (或 -F) 标志指定选择器:
pnpm --filter <package_selector> <command>
匹配#
--filter <package_name>#
要选择准确的包,只需指定其名称 (@scope/pkg) 或使用
模式来选择一组包 (@scope/*)。
示例:
pnpm --filter "@babel/core" test
pnpm --filter "@babel/*" test
pnpm --filter "*core" test
指定包的范围是可选的,因此如果未找到 core,--filter=core 将选择 @babel/core。但是,如果工作空间有多个同名的包(例如,@babel/core 和 @types/core),则没有范围的过滤将不会选择任何内容。
--filter <package_name>...#
要选择一个软件包及其依赖项(直接和非直接),请在包名称后加上省略号: <package_name>...。例如,下一个命令将运行 foo 及其所有依赖的测试:
pnpm --filter foo... test
你可以使用模式来选择一组根目录包:
pnpm --filter "@babel/preset-*..." test
--filter <package_name>^...#
要只选择一个包的依赖项(直接和非直接),在包名后添加一个山形符号加上先前提到的省略号。例如,下面的命令将运行所有 foo 的依赖项的测试:
pnpm --filter "foo^..." test
--filter ...<package_name>#
要选择一个包及被其依赖的包(直接和非直接),在包名前添加一个省略号: ...<package_name>。例如,这将运行 foo 以及依赖于它的所有包的测试:
pnpm --filter ...foo test
--filter "...^<package_name>"#
要只选择一个包的被依赖项 (直接和非直接) ,在包名前添加一个省略号与山形符号。例如,这将运行所有依赖于 foo 的包的测试:
pnpm --filter "...^foo" test
--filter ./<glob>, --filter {<glob>}#
相对于当前工作目录匹配项目的全局模式。
pnpm --filter "./packages/**" <cmd>
包括指定目录下的所有项目。
也可以使用省略号与山形符号来选择依赖项与被依赖项:
pnpm --filter ...{<directory>} <cmd>
pnpm --filter {<directory>}... <cmd>
pnpm --filter ...{<directory>}... <cmd>
它也可以与 [<since>] 结合使用。例如,在某个目录中选择所有已更改的项目:
pnpm --filter "{packages/**}[origin/master]" <cmd>
pnpm --filter "...{packages/**}[origin/master]" <cmd>
pnpm --filter "{packages/**}[origin/master]..." <cmd>
pnpm --filter "...{packages/**}[origin/master]..." <cmd>
The pattern language is the usual one: * and ? match within one path
segment, ** crosses segments, and a character class such as [ab] matches one
of the listed characters. Neither * nor ? matches a name that begins with a
dot, so packages/* skips packages/.cache. Name a dot-prefixed directory
explicitly to select it.
或者你可以从某个目录中选择符合给定的通配符的所有包:
pnpm --filter "@babel/*{components/**}" <cmd>
pnpm --filter "@babel/*{components/**}[origin/master]" <cmd>
pnpm --filter "...@babel/*{components/**}[origin/master]" <cmd>
legacyDirFiltering#
- 默认值: false
- 类型:Boolean
{<dir>} is matched as a glob pattern: {packages/*} selects the projects one
level below packages/, and it takes a ** to reach deeper. Set
legacyDirFiltering to true in pnpm-workspace.yaml to restore the older
behavior, where the selector named a directory and matched every project in
the subtree below it:
legacyDirFiltering: true
The setting only applies to the selectors you write. The workspace-root
selectors pnpm generates for itself (the !{<workspace-root>} a recursive
run / exec / add / test appends, and the {<workspace-root>} that
--workspace-root appends) are always matched as globs.
--filter "[<since>]"#
选择自指定的提交/分支以来有更改的所有包。可以在前后添加 ... 来包括依赖项/被依赖项:
例如,以下命令将运行自 master 以来所有变动过的包以及被其依赖的包的测试:
pnpm --filter "...[origin/master]" test
Since v12.7.0, <since> is compared against the commit where the current
branch forked from it (the merge base), so projects changed only by newer
commits on <since> are not selected. Uncommitted changes are still included.
In a shallow clone that lacks the merge base, pnpm compares against <since>
directly.
This selector requires Git 2.24 or newer.
A project also counts as changed when:
- a version it takes from a catalog changes in
pnpm-workspace.yaml; - files were moved out of it and git detects the move as a rename.
--fail-if-no-match#
如果你希望 CLI 在没有包与过滤器匹配的情况下失败,请使用此标志。
你也可以使用 failIfNoMatch 设置 永久设置此行为。
--no-fail-if-no-match overrides failIfNoMatch: true for one command.
排除#
任何过滤规则选择器都可以作为排除项,只要在开头添加一个"!"。在 zsh (可能还有其他 shells) 中, "!" 应转义: \!.
例如,这将在除 foo 以外的所有项目中运行一个命令:
pnpm --filter=!foo <cmd>
And this will run a command in all projects that are not in the lib
directory or below it:
pnpm --filter='!./lib/**' <cmd>
Since v12.7.0, selectors are evaluated in the order they are given, so a later inclusion can bring back a project that an earlier exclusion removed:
pnpm --filter "!./packages/**" --filter @scope/core <cmd>
多重性#
When packages are filtered, every package is taken that matches at least one of the inclusion selectors, unless a later exclusion removes it. When the first selector is an exclusion, filtering starts from every project in the workspace. 你可以使用无限数量的过滤器:
pnpm --filter ...foo --filter bar --filter baz... test
--filter-prod <filtering_pattern>#
Acts the same as --filter but omits devDependencies when selecting dependency projects
from the workspace.
--test-pattern <glob>#
test-pattern 允许检测修改后的文件是否与测试有关。如果是这样,经这种修改的包的被依赖包不会包括在内。
此选项对 "changed since" 过滤器很有用。例如,以下命令将在所有更改的包中运行测试, 如果包的源码发生变更,则测试也将在其依赖的包中运行:
pnpm --filter="...[origin/master]" --test-pattern="test/*" test
To set it for every command, use the testPattern setting in
pnpm-workspace.yaml:
- 默认值:[]
- 类型:string[]
--changed-files-ignore-pattern <glob>#
在筛选自指定提交/分支以来更改的项目时,允许通过 glob 模式忽略已更改的文件。
用法示例:
pnpm --filter="...[origin/master]" --changed-files-ignore-pattern="**/README.md" run build
To set it for every command, use the changedFilesIgnorePattern setting in
pnpm-workspace.yaml:
- 默认值:[]
- 类型:string[]