From 6d8f640480c56fdbfb44431536eeadbd4a996585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Thu, 10 Apr 2025 07:45:59 +0800 Subject: [PATCH 1/4] docs: add `filters` chapter --- docs/guide/index.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/guide/index.md b/docs/guide/index.md index 6e0fd129..082ff1e5 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -255,6 +255,38 @@ export const esbuildPlugin = unplugin.esbuild export const farmPlugin = unplugin.farm ``` +### Filters + +To optimize performance in native bundlers, leverage the `filter` option in `resolveId`, `transform`, and `load` +hooks to exclude files that don’t require processing. + +```ts twoslash +import { createUnplugin } from 'unplugin' + +type FilterPattern = string | RegExp | Array + +const plugin = createUnplugin(() => ({ + name: 'unplugin-starter', + transform: { + filter: { + id: { + include: [/\.js$/, '**/*.ts'], + exclude: /node_modules/, + }, + code: { + include: 'foo', + exclude: 'bar', + }, + }, + handler(code) { + // ... + }, + } +})) +``` + +More details can be found in the [Rolldown's documentation](https://rolldown.rs/guide/plugin-development#plugin-hook-filters). + ## Supported Context | Context | Rollup | Vite | webpack | esbuild | Rspack | Farm | Rolldown | From 2a316389b02ad43f24ad3affd913a80761113c01 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 10 Apr 2025 07:59:50 +0800 Subject: [PATCH 2/4] chore(deps): update devdependency @types/picomatch to v4 (#495) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- package.json | 2 +- pnpm-lock.yaml | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 36203cb4..963183ee 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "@rspack/core": "^1.3.4", "@types/fs-extra": "^11.0.4", "@types/node": "^22.14.0", - "@types/picomatch": "^3.0.2", + "@types/picomatch": "^4.0.0", "ansis": "^3.17.0", "bumpp": "^10.1.0", "esbuild": "^0.25.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d9833385..4145a03f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -49,8 +49,8 @@ importers: specifier: ^22.14.0 version: 22.14.0 '@types/picomatch': - specifier: ^3.0.2 - version: 3.0.2 + specifier: ^4.0.0 + version: 4.0.0 ansis: specifier: ^3.17.0 version: 3.17.0 @@ -1481,8 +1481,8 @@ packages: '@types/object-path@0.11.4': resolution: {integrity: sha512-4tgJ1Z3elF/tOMpA8JLVuR9spt9Ynsf7+JjqsQ2IqtiPJtcLoHoXcT6qU4E10cPFqyXX5HDm9QwIzZhBSkLxsw==} - '@types/picomatch@3.0.2': - resolution: {integrity: sha512-n0i8TD3UDB7paoMMxA3Y65vUncFJXjcUf7lQY7YyKGl6031FNjfsLs6pdLFCy2GNFxItPJG8GvvpbZc2skH7WA==} + '@types/picomatch@4.0.0': + resolution: {integrity: sha512-J1Bng+wlyEERWSgJQU1Pi0HObCLVcr994xT/M+1wcl/yNRTGBupsCxthgkdYG+GCOMaQH7iSVUY3LJVBBqG7MQ==} '@types/qs@6.9.18': resolution: {integrity: sha512-kK7dgTYDyGqS+e2Q4aK9X3D7q234CIZ1Bv0q/7Z5IwRDoADNU81xXJK/YVyLbLTZCoIwUoDoffFeF+p/eIklAA==} @@ -6681,7 +6681,7 @@ snapshots: '@types/object-path@0.11.4': {} - '@types/picomatch@3.0.2': {} + '@types/picomatch@4.0.0': {} '@types/qs@6.9.18': {} From f5a5abf4a68ffdec675b8231d94bd8ef432fa3c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Fri, 11 Apr 2025 08:34:16 +0800 Subject: [PATCH 3/4] fix(filter): correct the behavior when multiple transform filter options are specified ref: https://github.com/rollup/rollup/pull/5909 --- src/utils/filter.ts | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/utils/filter.ts b/src/utils/filter.ts index 19ef104f..c84c750b 100644 --- a/src/utils/filter.ts +++ b/src/utils/filter.ts @@ -13,11 +13,6 @@ function isAbsolute(path: string): boolean { return ABSOLUTE_PATH_REGEX.test(path) } -const FALLBACK_TRUE = 1 -const FALLBACK_FALSE = 0 -type FallbackValues = typeof FALLBACK_TRUE | typeof FALLBACK_FALSE -type PluginFilterWithFallback = (input: string) => boolean | FallbackValues - export type PluginFilter = (input: string) => boolean export type TransformHookFilter = (id: string, code: string) => boolean @@ -67,7 +62,7 @@ function patternToCodeFilter(pattern: StringOrRegExp): PluginFilter { function createFilter( exclude: PluginFilter[] | undefined, include: PluginFilter[] | undefined, -): PluginFilterWithFallback | undefined { +): PluginFilter | undefined { if (!exclude && !include) { return } @@ -79,7 +74,7 @@ function createFilter( if (include?.some(filter => filter(input))) { return true } - return !!include && include.length > 0 ? FALLBACK_FALSE : FALLBACK_TRUE + return !(include && include.length > 0) } } @@ -100,7 +95,7 @@ function normalizeFilter(filter: StringFilter): NormalizedStringFilter { } } -function createIdFilter(filter: StringFilter | undefined): PluginFilterWithFallback | undefined { +function createIdFilter(filter: StringFilter | undefined): PluginFilter | undefined { if (!filter) return const { exclude, include } = normalizeFilter(filter) @@ -109,7 +104,7 @@ function createIdFilter(filter: StringFilter | undefined): PluginFilterWithFallb return createFilter(excludeFilter, includeFilter) } -function createCodeFilter(filter: StringFilter | undefined): PluginFilterWithFallback | undefined { +function createCodeFilter(filter: StringFilter | undefined): PluginFilter | undefined { if (!filter) return const { exclude, include } = normalizeFilter(filter) @@ -134,18 +129,14 @@ function createFilterForTransform( return (id, code) => { let fallback = true if (idFilterFunction) { - const idResult = idFilterFunction(id) - if (typeof idResult === 'boolean') { - return idResult - } - fallback &&= !!idResult + fallback &&= idFilterFunction(id) + } + if (!fallback) { + return false } + if (codeFilterFunction) { - const codeResult = codeFilterFunction(code) - if (typeof codeResult === 'boolean') { - return codeResult - } - fallback &&= !!codeResult + fallback &&= codeFilterFunction(code) } return fallback } From 8a09a2092b2aa47d19e1603bd5d53e36649a54f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Fri, 11 Apr 2025 08:36:00 +0800 Subject: [PATCH 4/4] chore: release v2.3.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 963183ee..571cefbb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "unplugin", "type": "module", - "version": "2.3.0", + "version": "2.3.1", "packageManager": "pnpm@10.8.0", "description": "Unified plugin system for build tools", "license": "MIT",