过滤
过滤允许你将命令限制于软件包的特定子集。
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>
或者你可以从某个目录中选择符合给定的通配符的所有包:
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, since v11.24.0. Read
as subtree matches they named every project below the root, so a recursive
command under this setting selected nothing at all, and --workspace-root
pulled in the whole workspace instead of the root alone
(#14101).
--filter "[<since>]"#
选择自指定的提交/分支以来有更改的所有包。可以在前后添加 ... 来包括依赖项/被依赖项:
例如,以下命令将运行自 master 以来所有变动过的包以及被其依赖的包的测试:
pnpm --filter "...[origin/master]" test
Since v11.28.0, pnpm compares against the commit where the current branch
forked from <since> (their 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 change to a version in a catalog in pnpm-workspace.yaml
selects the projects that use that catalog entry. A project that files were
moved out of is selected when Git detects the move as a rename.
--fail-if-no-match#
如果你希望 CLI 在没有包与过滤器匹配的情况下失败,请使用此标志。
你也可以使用 failIfNoMatch 设置 永久设置此行为。
排除#
任何过滤规则选择器都可以作为排除项,只要在开头添加一个"!"。在 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 v11.28.0, selectors are applied in the order they are given. An exclusion
removes projects selected by the selectors before it, and an inclusion after it
can add excluded projects back. When the first selector is an exclusion, it
starts from every project in the workspace. For instance, this runs a command
in a but in no other project under packages:
pnpm --filter="!./packages/**" --filter=a <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
--changed-files-ignore-pattern <glob>#
在筛选自指定提交/分支以来更改的项目时,允许通过 glob 模式忽略已更改的文件。
用法示例:
pnpm --filter="...[origin/master]" --changed-files-ignore-pattern="**/README.md" run build