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 | diff --git a/package.json b/package.json index 36203cb4..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", @@ -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': {} 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 }