跳至主要內容
版本:7.x

篩選

篩選功能允許您將命令限制為封裝的特定子集。

pnpm 支援多種選取器,用於依名稱或關係選取封裝。

可以透過 --filter (或 -F) 旗標指定選擇器:

pnpm --篩選 <package_selector> <command>

比對

--篩選 <package_name>

要選取一個完全符合的封裝,只需指定它的名稱 (@scope/pkg) 或使用 一個模式來選取一組封裝 (@scope/*)。

Examples:

pnpm --篩選 "@babel/core" 測試
pnpm --篩選 "@babel/*" 測試
pnpm --篩選 "*core" 測試

指定封裝的範圍是選用的,因此如果找不到 core--filter=core 將選取 @babel/core。 但是,如果工作區具有多個同名的封裝 (例如, @babel/core@types/core), 則沒有範圍的篩選將不選擇任何內容。

--filter <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

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

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

--filter <封裝名稱>^...

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 ...<封裝名稱>

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 "...^<封裝名稱>"

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>}

相對於當前工作目錄相符專案的 Glob 模式。

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

Includes all projects that are under the specified directory.

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

pnpm --filter ...{<目錄>} <cmd>
pnpm --filter {<目錄>}... <cmd>
pnpm --filter ...{<目錄>}... <cmd>

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.

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

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

排除

Any of the filter selectors may work as exclusion operators when they have a leading "!". 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>

使用多個篩選器

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>

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

--test-pattern <glob>

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>

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