篩選

篩選功能允許您將命令限制為特定的套件子集合。

pnpm 支援豐富多元的選取器語法,可使用名稱或關係選取套件。

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

pnpm --filter <package_selector> <command>

比對#

--filter <package_name>#

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

Examples:

pnpm --篩選 "@babel/core" 測試
pnpm --篩選 "@babel/*" 測試
pnpm --篩選 "*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>...#

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 <package_name>^...#

若要僅選取封裝的相依項 (直接和非直接), 請以前述的省略符號搭配>形箭號作為名稱尾碼。 For instance, the next command will run tests for all of foo's dependencies:

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

--filter ...<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 "...^<package_name>"#

若要僅選擇封裝的相依項 (直接和非直接),請以省略符號之後跟隨>形箭號 作為封裝名稱首碼。 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>

包括指定目錄下的所有專案。

也可以與省略符號和>形符號運算子一起使用,以選取 依存項/相依項:

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>

legacyDirFiltering#

  • 預設值:false
  • 型別:布林值

{<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:

pnpm-workspace.yaml
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>]"#

選取自指定認可/分支變更的所有封裝。 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

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#

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

You may also set this permanently with a failIfNoMatch setting.

排除#

任何篩選器規則都可以執行排除操作,只需在前綴加上 "!"。 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 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 allows detecting whether the modified files are related to tests. 如果是,此被修改套件的被依賴套件將不會被包含在內。

此選項對 "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