File size: 2,286 Bytes
d2efaca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# @rolldown/pluginutils

A utility library for building flexible, composable filter expressions that can be used in plugin hook filters of Rolldown/Vite/Rollup/Unplugin plugins.

## Installation

```sh
pnpm add @rolldown/pluginutils
```

## Usage

### Simple Filters

```ts
import {
  exactRegex,
  makeIdFiltersToMatchWithQuery,
  prefixRegex,
} from '@rolldown/pluginutils';

// Match exactly 'foo.js'
const filter = exactRegex('foo.js');

// Match any id starting with 'lib/'
const prefix = prefixRegex('lib/');

// Match ids with query params (e.g. 'foo.js?bar')
const idFilters = makeIdFiltersToMatchWithQuery(['**/*.js', /\.ts$/]);

// Usage in a plugin to define a hook filter
const myPlugin = {
  resolveId: {
    filter: {
      id: [exactRegex('MY_ID_TO_CHECK'), /some-other-regex/],
    },
    handler(id) {
      // Your code here
    },
  },
};
```

### Composable Filters

> [!WARNING] Composable filters are not yet supported in Vite, Rolldown-Vite or unplugin. They can be used in Rolldown plugins only.

```ts
import { and, id, include, moduleType, query } from '@rolldown/pluginutils';

// Build a filter expression
const filterExpr = and(
  id(/\.ts$/),
  moduleType('ts'),
  query('foo', true),
);

// Usage in a plugin to define a hook filter
const myPlugin = {
  transform: {
    filter: [include(filterExpr)],
    handler(code, id, options) {
      // Your code here
    },
  },
};
```

## API Reference

### Simple Filters

- `exactRegex(str: string, flags?: string): RegExp` β€” Matches the exact string.
- `prefixRegex(str: string, flags?: string): RegExp` β€” Matches values with the given prefix.
- `makeIdFiltersToMatchWithQuery(input: string | RegExp | (string | RegExp)[]): string | RegExp | (string | RegExp)[]` β€” Adapts filters to match ids with query params.

### Composable Filters

- `and(...exprs)` / `or(...exprs)` / `not(expr)` β€” Logical composition of filter expressions.
- `id(pattern, params?)` β€” Filter by id (string or RegExp).
- `moduleType(type)` β€” Filter by module type (e.g. 'js', 'tsx', or 'json').
- `code(pattern)` β€” Filter by code content.
- `query(key, pattern)` β€” Filter by query parameter.
- `include(expr)` / `exclude(expr)` β€” Top-level include/exclude wrappers.
- `queries(obj)` β€” Compose multiple query filters.