Skip to main content
Version: 9.x

过滤

过滤允许你将命令限制为包的特定子集。

¥Filtering allows you to restrict commands to specific subsets of packages.

pnpm 支持丰富的选择器语法,用于按名称或关系选择包。

¥pnpm supports a rich selector syntax for picking packages by name or by relation.

选择器可以通过 --filter(或 -F)标志指定:

¥Selectors may be specified via the --filter (or -F) flag:

pnpm --filter <package_selector> <command>

匹配

¥Matching

--filter <package_name>

要选择精确的包,只需指定其名称 (@scope/pkg) 或使用模式来选择一组包 (@scope/*)。

¥To select an exact package, just specify its name (@scope/pkg) or use a pattern to select a set of packages (@scope/*).

示例:

¥Examples:

pnpm --filter "@babel/core" test
pnpm --filter "@babel/*" test
pnpm --filter "*core" test

指定包的范围是可选的,因此如果找不到 core--filter=core 将选择 @babel/core。但是,如果工作区有多个同名的包(例如 @babel/core@types/core),则不带范围的过滤将不会选择任何内容。

¥Specifying the scope of the package is optional, so --filter=core will pick @babel/core if core is not found. However, if the workspace has multiple packages with the same name (for instance, @babel/core and @types/core), then filtering without scope will pick nothing.

--filter <package_name>...

要选择包及其依赖(直接和非直接),请在包名称后添加省略号:<package_name>...。例如,下一个命令将运行 foo 及其所有依赖的测试:

¥To select a package and its dependencies (direct and non-direct), suffix the package name with an ellipsis: <package_name>.... For instance, the next command will run tests of foo and all of its dependencies:

pnpm --filter foo... test

你可以使用模式来选择一组根包:

¥You may use a pattern to select a set of root packages:

pnpm --filter "@babel/preset-*..." test

--filter <package_name>^...

要仅选择包的依赖(直接和非直接),请在名称后添加上述省略号,前面加上 V 形符号。例如,下一个命令将为 foo 的所有依赖运行测试:

¥To ONLY select the dependencies of a package (both direct and non-direct), suffix the name with the aforementioned ellipsis preceded by a chevron. For instance, the next command will run tests for all of foo's dependencies:

pnpm --filter "foo^..." test

--filter ...<package_name>

要选择包及其依赖包(直接和非直接),请在包名称前添加省略号前缀:...<package_name>。例如,这将运行 foo 以及依赖它的所有包的测试:

¥To select a package and its dependent packages (direct and non-direct), prefix the package name with an ellipsis: ...<package_name>. For instance, this will run the tests of foo and all packages dependent on it:

pnpm --filter ...foo test

--filter "...^<package_name>"

要仅选择包的依赖(直接和非直接),请在包名称前添加省略号和 V 形符号。例如,这将为所有依赖于 foo 的包运行测试:

¥To ONLY select a package's dependents (both direct and non-direct), prefix the package name with an ellipsis followed by a chevron. For instance, this will run tests for all packages dependent on foo:

pnpm --filter "...^foo" test

--filter ./<glob>, --filter {<glob>}

相对于当前工作目录匹配项目的通配符模式。

¥A glob pattern relative to the current working directory matching projects.

pnpm --filter "./packages/**" <cmd>

包括指定目录下的所有项目。

¥Includes all projects that are under the specified directory.

它也可以与省略号和 V 形运算符一起使用来选择依赖/依赖:

¥It may be used with the ellipsis and chevron operators to select dependents/dependencies as well:

pnpm --filter ...{<directory>} <cmd>
pnpm --filter {<directory>}... <cmd>
pnpm --filter ...{<directory>}... <cmd>

它也可以与 [<since>] 结合使用。例如,要选择目录中所有已更改的项目:

¥It may also be combined with [<since>]. For instance, to select all changed projects inside a directory:

pnpm --filter "{packages/**}[origin/master]" <cmd>
pnpm --filter "...{packages/**}[origin/master]" <cmd>
pnpm --filter "{packages/**}[origin/master]..." <cmd>
pnpm --filter "...{packages/**}[origin/master]..." <cmd>

或者你可以从目录中选择名称与给定模式匹配的所有包:

¥Or you may select all packages from a directory with names matching the given pattern:

pnpm --filter "@babel/*{components/**}" <cmd>
pnpm --filter "@babel/*{components/**}[origin/master]" <cmd>
pnpm --filter "...@babel/*{components/**}[origin/master]" <cmd>

--filter "[<since>]"

选择自指定提交/分支以来更改的所有包。可以以 ... 为后缀或前缀以包含依赖/依赖。

¥Selects all the packages changed since the specified commit/branch. May be suffixed or prefixed with ... to include dependencies/dependents.

例如,下一个命令将在自 master 以来的所有更改的包以及任何依赖包上运行测试:

¥For example, the next command will run tests in all changed packages since master and on any dependent packages:

pnpm --filter "...[origin/master]" test

--fail-if-no-match

如果你希望 CLI 在没有包与过滤器匹配的情况下失败,请使用此标志。

¥Use this flag if you want the CLI to fail if no packages have matched the filters.

排除

¥Excluding

当任何过滤器选择器具有前导 "!" 时,它们都可以用作排除运算符。在 zsh(可能还有其他 shell)中,"!" 应该被转义:\!

¥Any of the filter selectors may work as exclusion operators when they have a leading "!". In zsh (and possibly other shells), "!" should be escaped: \!.

例如,这将在除 foo 之外的所有项目中运行命令:

¥For instance, this will run a command in all projects except for foo:

pnpm --filter=!foo <cmd>

这将在不在 lib 目录下的所有项目中运行命令:

¥And this will run a command in all projects that are not under the lib directory:

pnpm --filter=!./lib <cmd>

多重性

¥Multiplicity

过滤包时,将采用至少与一个选择器匹配的每个包。你可以根据需要使用任意多个过滤器:

¥When packages are filtered, every package is taken that matches at least one of the selectors. You can use as many filters as you want:

pnpm --filter ...foo --filter bar --filter baz... test

--filter-prod <filtering_pattern>

从工作区选择依赖目时,其行为与 --filter 相同,但省略 devDependencies

¥Acts the same a --filter but omits devDependencies when selecting dependency projects from the workspace.

--test-pattern <glob>

test-pattern 允许检测修改的文件是否与测试相关。如果是,则不包括此类修改包的依赖包。

¥test-pattern allows detecting whether the modified files are related to tests. If they are, the dependent packages of such modified packages are not included.

此选项对于 "变更自" 过滤器很有用。例如,下一个命令将在所有更改的包中运行测试,如果更改位于包的源代码中,则测试也将在依赖包中运行:

¥This option is useful with the "changed since" filter. For instance, the next command will run tests in all changed packages, and if the changes are in the source code of the package, tests will run in the dependent packages as well:

pnpm --filter="...[origin/master]" --test-pattern="test/*" test

--changed-files-ignore-pattern <glob>

允许在过滤自指定提交/分支以来更改的项目时按 glob 模式忽略更改的文件。

¥Allows to ignore changed files by glob patterns when filtering for changed projects since the specified commit/branch.

使用示例:

¥Usage example:

pnpm --filter="...[origin/master]" --changed-files-ignore-pattern="**/README.md" run build