跳到主内容
版本:9.x

过滤

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

pnpm 支持丰富选择器语法,可以通过名称或关系选择包。

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

pnpm --filter <package_selector> <command>

匹配

--filter &lt;package_name>

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

示例:

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

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 &lt;package_name>...

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

您可以使用通配符来选择一组根目录包:

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

--filter &lt;package_name>^...

要只选择一个包的依赖项(直接和非直接), 在包名前添加一个山形符号加上上面提到的省略号。 For instance, the next command will run tests for all of foo's dependencies:

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

--filter ...&lt;package_name>

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 "...^&lt;package_name>"

要只选择一个包的被依赖项 (直接和非直接) ,在包名前添加一个省略号与山形符号。 For instance, this will run tests for all packages dependent on 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>

It may also be combined with [<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>

--filter "[&lt;since>]"

选择自指定的 commit / branch 以来有更改的所有包。 May be suffixed or prefixed with ... to include dependencies/dependents.

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 在没有包与过滤器匹配的情况下失败,请使用此标志。

排除

任何过滤规则选择器都可以作为排除项,只要在开头添加一个"!"。 In zsh (and possibly other shells), "!" should be escaped: \!.

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

pnpm --filter=!foo <cmd>

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

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

多重性

当包被过滤时,每个都会被匹配到至少一个选择器。 你可以使用无限数量的过滤器:

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

--filter-prod &lt;filtering_pattern>

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

--test-pattern &lt;glob>

test-pattern allows detecting whether the modified files are related to tests. 如果是这样,经这种修改的包的被依赖包并不包括在内。

此选项对 "changed since" 过滤器很有用。 例如,以下命令将在所有更改的包中运行测试, 如果报的源码发生变更,则测试也将在其依赖的包中运行:

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

--changed-files-ignore-pattern &lt;glob>

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

用法示例:

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