@rolldown/pluginutils 1.0.0-beta.44 → 1.0.0-beta.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/composable-filters.d.ts +83 -0
- package/dist/composable-filters.js +228 -0
- package/dist/filter-vite-plugins.d.ts +28 -0
- package/dist/filter-vite-plugins.js +75 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/simple-filters.d.ts +71 -0
- package/dist/simple-filters.js +70 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +17 -0
- package/package.json +8 -12
- package/dist/index.d.mts +0 -187
- package/dist/index.mjs +0 -306
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
type StringOrRegExp = string | RegExp;
|
|
2
|
+
type PluginModuleType = 'js' | 'jsx' | 'ts' | 'tsx' | 'json' | 'text' | 'base64' | 'dataurl' | 'binary' | 'empty' | (string & {});
|
|
3
|
+
export type FilterExpressionKind = FilterExpression['kind'];
|
|
4
|
+
export type FilterExpression = And | Or | Not | Id | ModuleType | Code | Query;
|
|
5
|
+
export type TopLevelFilterExpression = Include | Exclude;
|
|
6
|
+
declare class And {
|
|
7
|
+
kind: 'and';
|
|
8
|
+
args: FilterExpression[];
|
|
9
|
+
constructor(...args: FilterExpression[]);
|
|
10
|
+
}
|
|
11
|
+
declare class Or {
|
|
12
|
+
kind: 'or';
|
|
13
|
+
args: FilterExpression[];
|
|
14
|
+
constructor(...args: FilterExpression[]);
|
|
15
|
+
}
|
|
16
|
+
declare class Not {
|
|
17
|
+
kind: 'not';
|
|
18
|
+
expr: FilterExpression;
|
|
19
|
+
constructor(expr: FilterExpression);
|
|
20
|
+
}
|
|
21
|
+
export interface QueryFilterObject {
|
|
22
|
+
[key: string]: StringOrRegExp | boolean;
|
|
23
|
+
}
|
|
24
|
+
interface IdParams {
|
|
25
|
+
cleanUrl?: boolean;
|
|
26
|
+
}
|
|
27
|
+
declare class Id {
|
|
28
|
+
kind: 'id';
|
|
29
|
+
pattern: StringOrRegExp;
|
|
30
|
+
params: IdParams;
|
|
31
|
+
constructor(pattern: StringOrRegExp, params?: IdParams);
|
|
32
|
+
}
|
|
33
|
+
declare class ModuleType {
|
|
34
|
+
kind: 'moduleType';
|
|
35
|
+
pattern: PluginModuleType;
|
|
36
|
+
constructor(pattern: PluginModuleType);
|
|
37
|
+
}
|
|
38
|
+
declare class Code {
|
|
39
|
+
kind: 'code';
|
|
40
|
+
pattern: StringOrRegExp;
|
|
41
|
+
constructor(expr: StringOrRegExp);
|
|
42
|
+
}
|
|
43
|
+
declare class Query {
|
|
44
|
+
kind: 'query';
|
|
45
|
+
key: string;
|
|
46
|
+
pattern: StringOrRegExp | boolean;
|
|
47
|
+
constructor(key: string, pattern: StringOrRegExp | boolean);
|
|
48
|
+
}
|
|
49
|
+
declare class Include {
|
|
50
|
+
kind: 'include';
|
|
51
|
+
expr: FilterExpression;
|
|
52
|
+
constructor(expr: FilterExpression);
|
|
53
|
+
}
|
|
54
|
+
declare class Exclude {
|
|
55
|
+
kind: 'exclude';
|
|
56
|
+
expr: FilterExpression;
|
|
57
|
+
constructor(expr: FilterExpression);
|
|
58
|
+
}
|
|
59
|
+
export declare function and(...args: FilterExpression[]): And;
|
|
60
|
+
export declare function or(...args: FilterExpression[]): Or;
|
|
61
|
+
export declare function not(expr: FilterExpression): Not;
|
|
62
|
+
export declare function id(pattern: StringOrRegExp, params?: IdParams): Id;
|
|
63
|
+
export declare function moduleType(pattern: PluginModuleType): ModuleType;
|
|
64
|
+
export declare function code(pattern: StringOrRegExp): Code;
|
|
65
|
+
export declare function query(key: string, pattern: StringOrRegExp | boolean): Query;
|
|
66
|
+
export declare function include(expr: FilterExpression): Include;
|
|
67
|
+
export declare function exclude(expr: FilterExpression): Exclude;
|
|
68
|
+
/**
|
|
69
|
+
* convert a queryObject to FilterExpression like
|
|
70
|
+
* ```js
|
|
71
|
+
* and(query(k1, v1), query(k2, v2))
|
|
72
|
+
* ```
|
|
73
|
+
* @param queryFilterObject The query filter object needs to be matched.
|
|
74
|
+
* @returns a `And` FilterExpression
|
|
75
|
+
*/
|
|
76
|
+
export declare function queries(queryFilter: QueryFilterObject): And;
|
|
77
|
+
export declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
|
|
78
|
+
interface InterpreterCtx {
|
|
79
|
+
urlSearchParamsCache?: URLSearchParams;
|
|
80
|
+
}
|
|
81
|
+
export declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean;
|
|
82
|
+
export declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean;
|
|
83
|
+
export {};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { cleanUrl, extractQueryWithoutFragment } from './utils.js';
|
|
2
|
+
class And {
|
|
3
|
+
kind;
|
|
4
|
+
args;
|
|
5
|
+
constructor(...args) {
|
|
6
|
+
if (args.length === 0) {
|
|
7
|
+
throw new Error('`And` expects at least one operand');
|
|
8
|
+
}
|
|
9
|
+
this.args = args;
|
|
10
|
+
this.kind = 'and';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
class Or {
|
|
14
|
+
kind;
|
|
15
|
+
args;
|
|
16
|
+
constructor(...args) {
|
|
17
|
+
if (args.length === 0) {
|
|
18
|
+
throw new Error('`Or` expects at least one operand');
|
|
19
|
+
}
|
|
20
|
+
this.args = args;
|
|
21
|
+
this.kind = 'or';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
class Not {
|
|
25
|
+
kind;
|
|
26
|
+
expr;
|
|
27
|
+
constructor(expr) {
|
|
28
|
+
this.expr = expr;
|
|
29
|
+
this.kind = 'not';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
class Id {
|
|
33
|
+
kind;
|
|
34
|
+
pattern;
|
|
35
|
+
params;
|
|
36
|
+
constructor(pattern, params) {
|
|
37
|
+
this.pattern = pattern;
|
|
38
|
+
this.kind = 'id';
|
|
39
|
+
this.params = params ?? {
|
|
40
|
+
cleanUrl: false,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
class ModuleType {
|
|
45
|
+
kind;
|
|
46
|
+
pattern;
|
|
47
|
+
constructor(pattern) {
|
|
48
|
+
this.pattern = pattern;
|
|
49
|
+
this.kind = 'moduleType';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
class Code {
|
|
53
|
+
kind;
|
|
54
|
+
pattern;
|
|
55
|
+
constructor(expr) {
|
|
56
|
+
this.pattern = expr;
|
|
57
|
+
this.kind = 'code';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
class Query {
|
|
61
|
+
kind;
|
|
62
|
+
key;
|
|
63
|
+
pattern;
|
|
64
|
+
constructor(key, pattern) {
|
|
65
|
+
this.pattern = pattern;
|
|
66
|
+
this.key = key;
|
|
67
|
+
this.kind = 'query';
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
class Include {
|
|
71
|
+
kind;
|
|
72
|
+
expr;
|
|
73
|
+
constructor(expr) {
|
|
74
|
+
this.expr = expr;
|
|
75
|
+
this.kind = 'include';
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
class Exclude {
|
|
79
|
+
kind;
|
|
80
|
+
expr;
|
|
81
|
+
constructor(expr) {
|
|
82
|
+
this.expr = expr;
|
|
83
|
+
this.kind = 'exclude';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export function and(...args) {
|
|
87
|
+
return new And(...args);
|
|
88
|
+
}
|
|
89
|
+
export function or(...args) {
|
|
90
|
+
return new Or(...args);
|
|
91
|
+
}
|
|
92
|
+
export function not(expr) {
|
|
93
|
+
return new Not(expr);
|
|
94
|
+
}
|
|
95
|
+
export function id(pattern, params) {
|
|
96
|
+
return new Id(pattern, params);
|
|
97
|
+
}
|
|
98
|
+
export function moduleType(pattern) {
|
|
99
|
+
return new ModuleType(pattern);
|
|
100
|
+
}
|
|
101
|
+
export function code(pattern) {
|
|
102
|
+
return new Code(pattern);
|
|
103
|
+
}
|
|
104
|
+
/*
|
|
105
|
+
* There are three kinds of conditions are supported:
|
|
106
|
+
* 1. `boolean`: if the value is `true`, the key must exist and be truthy. if the value is `false`, the key must not exist or be falsy.
|
|
107
|
+
* 2. `string`: the key must exist and be equal to the value.
|
|
108
|
+
* 3. `RegExp`: the key must exist and match the value.
|
|
109
|
+
*/
|
|
110
|
+
export function query(key, pattern) {
|
|
111
|
+
return new Query(key, pattern);
|
|
112
|
+
}
|
|
113
|
+
export function include(expr) {
|
|
114
|
+
return new Include(expr);
|
|
115
|
+
}
|
|
116
|
+
export function exclude(expr) {
|
|
117
|
+
return new Exclude(expr);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* convert a queryObject to FilterExpression like
|
|
121
|
+
* ```js
|
|
122
|
+
* and(query(k1, v1), query(k2, v2))
|
|
123
|
+
* ```
|
|
124
|
+
* @param queryFilterObject The query filter object needs to be matched.
|
|
125
|
+
* @returns a `And` FilterExpression
|
|
126
|
+
*/
|
|
127
|
+
export function queries(queryFilter) {
|
|
128
|
+
let arr = Object.entries(queryFilter).map(([key, value]) => {
|
|
129
|
+
return new Query(key, value);
|
|
130
|
+
});
|
|
131
|
+
return and(...arr);
|
|
132
|
+
}
|
|
133
|
+
export function interpreter(exprs, code, id, moduleType) {
|
|
134
|
+
let arr = [];
|
|
135
|
+
if (Array.isArray(exprs)) {
|
|
136
|
+
arr = exprs;
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
arr = [exprs];
|
|
140
|
+
}
|
|
141
|
+
return interpreterImpl(arr, code, id, moduleType);
|
|
142
|
+
}
|
|
143
|
+
export function interpreterImpl(expr, code, id, moduleType, ctx = {}) {
|
|
144
|
+
let hasInclude = false;
|
|
145
|
+
for (const e of expr) {
|
|
146
|
+
switch (e.kind) {
|
|
147
|
+
case 'include': {
|
|
148
|
+
hasInclude = true;
|
|
149
|
+
if (exprInterpreter(e.expr, code, id, moduleType, ctx)) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case 'exclude': {
|
|
155
|
+
if (exprInterpreter(e.expr, code, id, moduleType)) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return !hasInclude;
|
|
163
|
+
}
|
|
164
|
+
export function exprInterpreter(expr, code, id, moduleType, ctx = {}) {
|
|
165
|
+
switch (expr.kind) {
|
|
166
|
+
case 'and': {
|
|
167
|
+
return expr.args.every((e) => exprInterpreter(e, code, id, moduleType, ctx));
|
|
168
|
+
}
|
|
169
|
+
case 'or': {
|
|
170
|
+
return expr.args.some((e) => exprInterpreter(e, code, id, moduleType, ctx));
|
|
171
|
+
}
|
|
172
|
+
case 'not': {
|
|
173
|
+
return !exprInterpreter(expr.expr, code, id, moduleType, ctx);
|
|
174
|
+
}
|
|
175
|
+
case 'id': {
|
|
176
|
+
if (id === undefined) {
|
|
177
|
+
throw new Error('`id` is required for `id` expression');
|
|
178
|
+
}
|
|
179
|
+
if (expr.params.cleanUrl) {
|
|
180
|
+
id = cleanUrl(id);
|
|
181
|
+
}
|
|
182
|
+
return typeof expr.pattern === 'string'
|
|
183
|
+
? id === expr.pattern
|
|
184
|
+
: expr.pattern.test(id);
|
|
185
|
+
}
|
|
186
|
+
case 'moduleType': {
|
|
187
|
+
if (moduleType === undefined) {
|
|
188
|
+
throw new Error('`moduleType` is required for `moduleType` expression');
|
|
189
|
+
}
|
|
190
|
+
return moduleType === expr.pattern;
|
|
191
|
+
}
|
|
192
|
+
case 'code': {
|
|
193
|
+
if (code === undefined) {
|
|
194
|
+
throw new Error('`code` is required for `code` expression');
|
|
195
|
+
}
|
|
196
|
+
return typeof expr.pattern === 'string'
|
|
197
|
+
? code.includes(expr.pattern)
|
|
198
|
+
: expr.pattern.test(code);
|
|
199
|
+
}
|
|
200
|
+
case 'query': {
|
|
201
|
+
if (id === undefined) {
|
|
202
|
+
throw new Error('`id` is required for `Query` expression');
|
|
203
|
+
}
|
|
204
|
+
if (!ctx.urlSearchParamsCache) {
|
|
205
|
+
let queryString = extractQueryWithoutFragment(id);
|
|
206
|
+
ctx.urlSearchParamsCache = new URLSearchParams(queryString);
|
|
207
|
+
}
|
|
208
|
+
let urlParams = ctx.urlSearchParamsCache;
|
|
209
|
+
if (typeof expr.pattern === 'boolean') {
|
|
210
|
+
if (expr.pattern) {
|
|
211
|
+
return urlParams.has(expr.key);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
return !urlParams.has(expr.key);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
else if (typeof expr.pattern === 'string') {
|
|
218
|
+
return urlParams.get(expr.key) === expr.pattern;
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
return expr.pattern.test(urlParams.get(expr.key) ?? '');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
default: {
|
|
225
|
+
throw new Error(`Expression ${JSON.stringify(expr)} is not expected.`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filters out Vite plugins that have `apply: 'serve'` set.
|
|
3
|
+
*
|
|
4
|
+
* Since Rolldown operates in build mode, plugins marked with `apply: 'serve'`
|
|
5
|
+
* are intended only for Vite's dev server and should be excluded from the build process.
|
|
6
|
+
*
|
|
7
|
+
* @param plugins - Array of plugins (can include nested arrays)
|
|
8
|
+
* @returns Filtered array with serve-only plugins removed
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { defineConfig } from 'rolldown';
|
|
13
|
+
* import { filterVitePlugins } from '@rolldown/pluginutils';
|
|
14
|
+
* import viteReact from '@vitejs/plugin-react';
|
|
15
|
+
*
|
|
16
|
+
* export default defineConfig({
|
|
17
|
+
* plugins: filterVitePlugins([
|
|
18
|
+
* viteReact(),
|
|
19
|
+
* {
|
|
20
|
+
* name: 'dev-only',
|
|
21
|
+
* apply: 'serve', // This will be filtered out
|
|
22
|
+
* // ...
|
|
23
|
+
* }
|
|
24
|
+
* ])
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function filterVitePlugins<T = any>(plugins: T | T[] | null | undefined | false): T[];
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filters out Vite plugins that have `apply: 'serve'` set.
|
|
3
|
+
*
|
|
4
|
+
* Since Rolldown operates in build mode, plugins marked with `apply: 'serve'`
|
|
5
|
+
* are intended only for Vite's dev server and should be excluded from the build process.
|
|
6
|
+
*
|
|
7
|
+
* @param plugins - Array of plugins (can include nested arrays)
|
|
8
|
+
* @returns Filtered array with serve-only plugins removed
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { defineConfig } from 'rolldown';
|
|
13
|
+
* import { filterVitePlugins } from '@rolldown/pluginutils';
|
|
14
|
+
* import viteReact from '@vitejs/plugin-react';
|
|
15
|
+
*
|
|
16
|
+
* export default defineConfig({
|
|
17
|
+
* plugins: filterVitePlugins([
|
|
18
|
+
* viteReact(),
|
|
19
|
+
* {
|
|
20
|
+
* name: 'dev-only',
|
|
21
|
+
* apply: 'serve', // This will be filtered out
|
|
22
|
+
* // ...
|
|
23
|
+
* }
|
|
24
|
+
* ])
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function filterVitePlugins(plugins) {
|
|
29
|
+
if (!plugins) {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
const pluginArray = Array.isArray(plugins) ? plugins : [plugins];
|
|
33
|
+
const result = [];
|
|
34
|
+
for (const plugin of pluginArray) {
|
|
35
|
+
// Skip falsy values
|
|
36
|
+
if (!plugin) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
// Handle nested arrays recursively
|
|
40
|
+
if (Array.isArray(plugin)) {
|
|
41
|
+
result.push(...filterVitePlugins(plugin));
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
// Check if plugin has apply property
|
|
45
|
+
const pluginWithApply = plugin;
|
|
46
|
+
if ('apply' in pluginWithApply) {
|
|
47
|
+
const applyValue = pluginWithApply.apply;
|
|
48
|
+
// If apply is a function, call it with build mode
|
|
49
|
+
if (typeof applyValue === 'function') {
|
|
50
|
+
try {
|
|
51
|
+
const shouldApply = applyValue({}, // config object
|
|
52
|
+
{ command: 'build', mode: 'production' });
|
|
53
|
+
if (shouldApply) {
|
|
54
|
+
result.push(plugin);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// If function throws, include the plugin to be safe
|
|
59
|
+
result.push(plugin);
|
|
60
|
+
}
|
|
61
|
+
} // If apply is 'serve', skip this plugin
|
|
62
|
+
else if (applyValue === 'serve') {
|
|
63
|
+
continue;
|
|
64
|
+
} // If apply is 'build' or anything else, include it
|
|
65
|
+
else {
|
|
66
|
+
result.push(plugin);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
// No apply property, include the plugin
|
|
71
|
+
result.push(plugin);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Constructs a RegExp that matches the exact string specified.
|
|
3
|
+
*
|
|
4
|
+
* This is useful for plugin hook filters.
|
|
5
|
+
*
|
|
6
|
+
* @param str the string to match.
|
|
7
|
+
* @param flags flags for the RegExp.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { exactRegex } from '@rolldown/pluginutils';
|
|
12
|
+
* const plugin = {
|
|
13
|
+
* name: 'plugin',
|
|
14
|
+
* resolveId: {
|
|
15
|
+
* filter: { id: exactRegex('foo') },
|
|
16
|
+
* handler(id) {} // will only be called for `foo`
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function exactRegex(str: string, flags?: string): RegExp;
|
|
22
|
+
/**
|
|
23
|
+
* Constructs a RegExp that matches a value that has the specified prefix.
|
|
24
|
+
*
|
|
25
|
+
* This is useful for plugin hook filters.
|
|
26
|
+
*
|
|
27
|
+
* @param str the string to match.
|
|
28
|
+
* @param flags flags for the RegExp.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { prefixRegex } from '@rolldown/pluginutils';
|
|
33
|
+
* const plugin = {
|
|
34
|
+
* name: 'plugin',
|
|
35
|
+
* resolveId: {
|
|
36
|
+
* filter: { id: prefixRegex('foo') },
|
|
37
|
+
* handler(id) {} // will only be called for IDs starting with `foo`
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function prefixRegex(str: string, flags?: string): RegExp;
|
|
43
|
+
type WidenString<T> = T extends string ? string : T;
|
|
44
|
+
/**
|
|
45
|
+
* Converts a id filter to match with an id with a query.
|
|
46
|
+
*
|
|
47
|
+
* @param input the id filters to convert.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { makeIdFiltersToMatchWithQuery } from '@rolldown/pluginutils';
|
|
52
|
+
* const plugin = {
|
|
53
|
+
* name: 'plugin',
|
|
54
|
+
* transform: {
|
|
55
|
+
* filter: { id: makeIdFiltersToMatchWithQuery(['**' + '/*.js', /\.ts$/]) },
|
|
56
|
+
* // The handler will be called for IDs like:
|
|
57
|
+
* // - foo.js
|
|
58
|
+
* // - foo.js?foo
|
|
59
|
+
* // - foo.txt?foo.js
|
|
60
|
+
* // - foo.ts
|
|
61
|
+
* // - foo.ts?foo
|
|
62
|
+
* // - foo.txt?foo.ts
|
|
63
|
+
* handler(code, id) {}
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: T): WidenString<T>;
|
|
69
|
+
export declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: readonly T[]): WidenString<T>[];
|
|
70
|
+
export declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
|
|
71
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Constructs a RegExp that matches the exact string specified.
|
|
3
|
+
*
|
|
4
|
+
* This is useful for plugin hook filters.
|
|
5
|
+
*
|
|
6
|
+
* @param str the string to match.
|
|
7
|
+
* @param flags flags for the RegExp.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { exactRegex } from '@rolldown/pluginutils';
|
|
12
|
+
* const plugin = {
|
|
13
|
+
* name: 'plugin',
|
|
14
|
+
* resolveId: {
|
|
15
|
+
* filter: { id: exactRegex('foo') },
|
|
16
|
+
* handler(id) {} // will only be called for `foo`
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function exactRegex(str, flags) {
|
|
22
|
+
return new RegExp(`^${escapeRegex(str)}$`, flags);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Constructs a RegExp that matches a value that has the specified prefix.
|
|
26
|
+
*
|
|
27
|
+
* This is useful for plugin hook filters.
|
|
28
|
+
*
|
|
29
|
+
* @param str the string to match.
|
|
30
|
+
* @param flags flags for the RegExp.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { prefixRegex } from '@rolldown/pluginutils';
|
|
35
|
+
* const plugin = {
|
|
36
|
+
* name: 'plugin',
|
|
37
|
+
* resolveId: {
|
|
38
|
+
* filter: { id: prefixRegex('foo') },
|
|
39
|
+
* handler(id) {} // will only be called for IDs starting with `foo`
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function prefixRegex(str, flags) {
|
|
45
|
+
return new RegExp(`^${escapeRegex(str)}`, flags);
|
|
46
|
+
}
|
|
47
|
+
const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
|
|
48
|
+
function escapeRegex(str) {
|
|
49
|
+
return str.replace(escapeRegexRE, '\\$&');
|
|
50
|
+
}
|
|
51
|
+
export function makeIdFiltersToMatchWithQuery(input) {
|
|
52
|
+
if (!Array.isArray(input)) {
|
|
53
|
+
return makeIdFilterToMatchWithQuery(
|
|
54
|
+
// Array.isArray cannot narrow the type
|
|
55
|
+
// https://github.com/microsoft/TypeScript/issues/17002
|
|
56
|
+
input);
|
|
57
|
+
}
|
|
58
|
+
return input.map((i) => makeIdFilterToMatchWithQuery(i));
|
|
59
|
+
}
|
|
60
|
+
function makeIdFilterToMatchWithQuery(input) {
|
|
61
|
+
if (typeof input === 'string') {
|
|
62
|
+
return `${input}{?*,}`;
|
|
63
|
+
}
|
|
64
|
+
return makeRegexIdFilterToMatchWithQuery(input);
|
|
65
|
+
}
|
|
66
|
+
function makeRegexIdFilterToMatchWithQuery(input) {
|
|
67
|
+
return new RegExp(
|
|
68
|
+
// replace `$` with `(?:\?.*)?$` (ignore `\$`)
|
|
69
|
+
input.source.replace(/(?<!\\)\$/g, '(?:\\?.*)?$'), input.flags);
|
|
70
|
+
}
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const postfixRE = /[?#].*$/;
|
|
2
|
+
export function cleanUrl(url) {
|
|
3
|
+
return url.replace(postfixRE, '');
|
|
4
|
+
}
|
|
5
|
+
export function extractQueryWithoutFragment(url) {
|
|
6
|
+
const questionMarkIndex = url.indexOf('?');
|
|
7
|
+
if (questionMarkIndex === -1) {
|
|
8
|
+
return '';
|
|
9
|
+
}
|
|
10
|
+
const fragmentIndex = url.indexOf('#', questionMarkIndex); // Search for # after ?
|
|
11
|
+
if (fragmentIndex === -1) {
|
|
12
|
+
return url.substring(questionMarkIndex);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
return url.substring(questionMarkIndex, fragmentIndex);
|
|
16
|
+
}
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rolldown/pluginutils",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.45",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -11,11 +11,11 @@
|
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public"
|
|
13
13
|
},
|
|
14
|
-
"main": "./dist/index.
|
|
15
|
-
"module": "./dist/index.
|
|
16
|
-
"types": "./dist/index.d.
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
17
|
"exports": {
|
|
18
|
-
".": "./dist/index.
|
|
18
|
+
".": "./dist/index.js",
|
|
19
19
|
"./package.json": "./package.json"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
@@ -24,15 +24,11 @@
|
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/picomatch": "^4.0.0",
|
|
26
26
|
"picomatch": "^4.0.2",
|
|
27
|
-
"
|
|
28
|
-
"vitest": "^
|
|
29
|
-
},
|
|
30
|
-
"tsdown": {
|
|
31
|
-
"exports": true,
|
|
32
|
-
"fixedExtension": true
|
|
27
|
+
"typescript": "^5.8.3",
|
|
28
|
+
"vitest": "^4.0.1"
|
|
33
29
|
},
|
|
34
30
|
"scripts": {
|
|
35
|
-
"build": "
|
|
31
|
+
"build": "tsc",
|
|
36
32
|
"test": "vitest --typecheck"
|
|
37
33
|
}
|
|
38
34
|
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
//#region src/composable-filters.d.ts
|
|
2
|
-
type StringOrRegExp = string | RegExp;
|
|
3
|
-
type PluginModuleType = "js" | "jsx" | "ts" | "tsx" | "json" | "text" | "base64" | "dataurl" | "binary" | "empty" | (string & {});
|
|
4
|
-
type FilterExpressionKind = FilterExpression["kind"];
|
|
5
|
-
type FilterExpression = And | Or | Not | Id | ModuleType | Code | Query;
|
|
6
|
-
type TopLevelFilterExpression = Include | Exclude;
|
|
7
|
-
declare class And {
|
|
8
|
-
kind: "and";
|
|
9
|
-
args: FilterExpression[];
|
|
10
|
-
constructor(...args: FilterExpression[]);
|
|
11
|
-
}
|
|
12
|
-
declare class Or {
|
|
13
|
-
kind: "or";
|
|
14
|
-
args: FilterExpression[];
|
|
15
|
-
constructor(...args: FilterExpression[]);
|
|
16
|
-
}
|
|
17
|
-
declare class Not {
|
|
18
|
-
kind: "not";
|
|
19
|
-
expr: FilterExpression;
|
|
20
|
-
constructor(expr: FilterExpression);
|
|
21
|
-
}
|
|
22
|
-
interface QueryFilterObject {
|
|
23
|
-
[key: string]: StringOrRegExp | boolean;
|
|
24
|
-
}
|
|
25
|
-
interface IdParams {
|
|
26
|
-
cleanUrl?: boolean;
|
|
27
|
-
}
|
|
28
|
-
declare class Id {
|
|
29
|
-
kind: "id";
|
|
30
|
-
pattern: StringOrRegExp;
|
|
31
|
-
params: IdParams;
|
|
32
|
-
constructor(pattern: StringOrRegExp, params?: IdParams);
|
|
33
|
-
}
|
|
34
|
-
declare class ModuleType {
|
|
35
|
-
kind: "moduleType";
|
|
36
|
-
pattern: PluginModuleType;
|
|
37
|
-
constructor(pattern: PluginModuleType);
|
|
38
|
-
}
|
|
39
|
-
declare class Code {
|
|
40
|
-
kind: "code";
|
|
41
|
-
pattern: StringOrRegExp;
|
|
42
|
-
constructor(expr: StringOrRegExp);
|
|
43
|
-
}
|
|
44
|
-
declare class Query {
|
|
45
|
-
kind: "query";
|
|
46
|
-
key: string;
|
|
47
|
-
pattern: StringOrRegExp | boolean;
|
|
48
|
-
constructor(key: string, pattern: StringOrRegExp | boolean);
|
|
49
|
-
}
|
|
50
|
-
declare class Include {
|
|
51
|
-
kind: "include";
|
|
52
|
-
expr: FilterExpression;
|
|
53
|
-
constructor(expr: FilterExpression);
|
|
54
|
-
}
|
|
55
|
-
declare class Exclude {
|
|
56
|
-
kind: "exclude";
|
|
57
|
-
expr: FilterExpression;
|
|
58
|
-
constructor(expr: FilterExpression);
|
|
59
|
-
}
|
|
60
|
-
declare function and(...args: FilterExpression[]): And;
|
|
61
|
-
declare function or(...args: FilterExpression[]): Or;
|
|
62
|
-
declare function not(expr: FilterExpression): Not;
|
|
63
|
-
declare function id(pattern: StringOrRegExp, params?: IdParams): Id;
|
|
64
|
-
declare function moduleType(pattern: PluginModuleType): ModuleType;
|
|
65
|
-
declare function code(pattern: StringOrRegExp): Code;
|
|
66
|
-
declare function query(key: string, pattern: StringOrRegExp | boolean): Query;
|
|
67
|
-
declare function include(expr: FilterExpression): Include;
|
|
68
|
-
declare function exclude(expr: FilterExpression): Exclude;
|
|
69
|
-
/**
|
|
70
|
-
* convert a queryObject to FilterExpression like
|
|
71
|
-
* ```js
|
|
72
|
-
* and(query(k1, v1), query(k2, v2))
|
|
73
|
-
* ```
|
|
74
|
-
* @param queryFilterObject The query filter object needs to be matched.
|
|
75
|
-
* @returns a `And` FilterExpression
|
|
76
|
-
*/
|
|
77
|
-
declare function queries(queryFilter: QueryFilterObject): And;
|
|
78
|
-
declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
|
|
79
|
-
interface InterpreterCtx {
|
|
80
|
-
urlSearchParamsCache?: URLSearchParams;
|
|
81
|
-
}
|
|
82
|
-
declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean;
|
|
83
|
-
declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean;
|
|
84
|
-
//#endregion
|
|
85
|
-
//#region src/filter-vite-plugins.d.ts
|
|
86
|
-
/**
|
|
87
|
-
* Filters out Vite plugins that have `apply: 'serve'` set.
|
|
88
|
-
*
|
|
89
|
-
* Since Rolldown operates in build mode, plugins marked with `apply: 'serve'`
|
|
90
|
-
* are intended only for Vite's dev server and should be excluded from the build process.
|
|
91
|
-
*
|
|
92
|
-
* @param plugins - Array of plugins (can include nested arrays)
|
|
93
|
-
* @returns Filtered array with serve-only plugins removed
|
|
94
|
-
*
|
|
95
|
-
* @example
|
|
96
|
-
* ```ts
|
|
97
|
-
* import { defineConfig } from 'rolldown';
|
|
98
|
-
* import { filterVitePlugins } from '@rolldown/pluginutils';
|
|
99
|
-
* import viteReact from '@vitejs/plugin-react';
|
|
100
|
-
*
|
|
101
|
-
* export default defineConfig({
|
|
102
|
-
* plugins: filterVitePlugins([
|
|
103
|
-
* viteReact(),
|
|
104
|
-
* {
|
|
105
|
-
* name: 'dev-only',
|
|
106
|
-
* apply: 'serve', // This will be filtered out
|
|
107
|
-
* // ...
|
|
108
|
-
* }
|
|
109
|
-
* ])
|
|
110
|
-
* });
|
|
111
|
-
* ```
|
|
112
|
-
*/
|
|
113
|
-
declare function filterVitePlugins<T = any>(plugins: T | T[] | null | undefined | false): T[];
|
|
114
|
-
//#endregion
|
|
115
|
-
//#region src/simple-filters.d.ts
|
|
116
|
-
/**
|
|
117
|
-
* Constructs a RegExp that matches the exact string specified.
|
|
118
|
-
*
|
|
119
|
-
* This is useful for plugin hook filters.
|
|
120
|
-
*
|
|
121
|
-
* @param str the string to match.
|
|
122
|
-
* @param flags flags for the RegExp.
|
|
123
|
-
*
|
|
124
|
-
* @example
|
|
125
|
-
* ```ts
|
|
126
|
-
* import { exactRegex } from '@rolldown/pluginutils';
|
|
127
|
-
* const plugin = {
|
|
128
|
-
* name: 'plugin',
|
|
129
|
-
* resolveId: {
|
|
130
|
-
* filter: { id: exactRegex('foo') },
|
|
131
|
-
* handler(id) {} // will only be called for `foo`
|
|
132
|
-
* }
|
|
133
|
-
* }
|
|
134
|
-
* ```
|
|
135
|
-
*/
|
|
136
|
-
declare function exactRegex(str: string, flags?: string): RegExp;
|
|
137
|
-
/**
|
|
138
|
-
* Constructs a RegExp that matches a value that has the specified prefix.
|
|
139
|
-
*
|
|
140
|
-
* This is useful for plugin hook filters.
|
|
141
|
-
*
|
|
142
|
-
* @param str the string to match.
|
|
143
|
-
* @param flags flags for the RegExp.
|
|
144
|
-
*
|
|
145
|
-
* @example
|
|
146
|
-
* ```ts
|
|
147
|
-
* import { prefixRegex } from '@rolldown/pluginutils';
|
|
148
|
-
* const plugin = {
|
|
149
|
-
* name: 'plugin',
|
|
150
|
-
* resolveId: {
|
|
151
|
-
* filter: { id: prefixRegex('foo') },
|
|
152
|
-
* handler(id) {} // will only be called for IDs starting with `foo`
|
|
153
|
-
* }
|
|
154
|
-
* }
|
|
155
|
-
* ```
|
|
156
|
-
*/
|
|
157
|
-
declare function prefixRegex(str: string, flags?: string): RegExp;
|
|
158
|
-
type WidenString<T> = T extends string ? string : T;
|
|
159
|
-
/**
|
|
160
|
-
* Converts a id filter to match with an id with a query.
|
|
161
|
-
*
|
|
162
|
-
* @param input the id filters to convert.
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* ```ts
|
|
166
|
-
* import { makeIdFiltersToMatchWithQuery } from '@rolldown/pluginutils';
|
|
167
|
-
* const plugin = {
|
|
168
|
-
* name: 'plugin',
|
|
169
|
-
* transform: {
|
|
170
|
-
* filter: { id: makeIdFiltersToMatchWithQuery(['**' + '/*.js', /\.ts$/]) },
|
|
171
|
-
* // The handler will be called for IDs like:
|
|
172
|
-
* // - foo.js
|
|
173
|
-
* // - foo.js?foo
|
|
174
|
-
* // - foo.txt?foo.js
|
|
175
|
-
* // - foo.ts
|
|
176
|
-
* // - foo.ts?foo
|
|
177
|
-
* // - foo.txt?foo.ts
|
|
178
|
-
* handler(code, id) {}
|
|
179
|
-
* }
|
|
180
|
-
* }
|
|
181
|
-
* ```
|
|
182
|
-
*/
|
|
183
|
-
declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: T): WidenString<T>;
|
|
184
|
-
declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: readonly T[]): WidenString<T>[];
|
|
185
|
-
declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
|
|
186
|
-
//#endregion
|
|
187
|
-
export { FilterExpression, FilterExpressionKind, QueryFilterObject, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, filterVitePlugins, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, queries, query };
|
package/dist/index.mjs
DELETED
|
@@ -1,306 +0,0 @@
|
|
|
1
|
-
//#region src/utils.ts
|
|
2
|
-
const postfixRE = /[?#].*$/;
|
|
3
|
-
function cleanUrl(url) {
|
|
4
|
-
return url.replace(postfixRE, "");
|
|
5
|
-
}
|
|
6
|
-
function extractQueryWithoutFragment(url) {
|
|
7
|
-
const questionMarkIndex = url.indexOf("?");
|
|
8
|
-
if (questionMarkIndex === -1) return "";
|
|
9
|
-
const fragmentIndex = url.indexOf("#", questionMarkIndex);
|
|
10
|
-
if (fragmentIndex === -1) return url.substring(questionMarkIndex);
|
|
11
|
-
else return url.substring(questionMarkIndex, fragmentIndex);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
//#endregion
|
|
15
|
-
//#region src/composable-filters.ts
|
|
16
|
-
var And = class {
|
|
17
|
-
kind;
|
|
18
|
-
args;
|
|
19
|
-
constructor(...args) {
|
|
20
|
-
if (args.length === 0) throw new Error("`And` expects at least one operand");
|
|
21
|
-
this.args = args;
|
|
22
|
-
this.kind = "and";
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
var Or = class {
|
|
26
|
-
kind;
|
|
27
|
-
args;
|
|
28
|
-
constructor(...args) {
|
|
29
|
-
if (args.length === 0) throw new Error("`Or` expects at least one operand");
|
|
30
|
-
this.args = args;
|
|
31
|
-
this.kind = "or";
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
var Not = class {
|
|
35
|
-
kind;
|
|
36
|
-
expr;
|
|
37
|
-
constructor(expr) {
|
|
38
|
-
this.expr = expr;
|
|
39
|
-
this.kind = "not";
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
var Id = class {
|
|
43
|
-
kind;
|
|
44
|
-
pattern;
|
|
45
|
-
params;
|
|
46
|
-
constructor(pattern, params) {
|
|
47
|
-
this.pattern = pattern;
|
|
48
|
-
this.kind = "id";
|
|
49
|
-
this.params = params ?? { cleanUrl: false };
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
var ModuleType = class {
|
|
53
|
-
kind;
|
|
54
|
-
pattern;
|
|
55
|
-
constructor(pattern) {
|
|
56
|
-
this.pattern = pattern;
|
|
57
|
-
this.kind = "moduleType";
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
var Code = class {
|
|
61
|
-
kind;
|
|
62
|
-
pattern;
|
|
63
|
-
constructor(expr) {
|
|
64
|
-
this.pattern = expr;
|
|
65
|
-
this.kind = "code";
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
var Query = class {
|
|
69
|
-
kind;
|
|
70
|
-
key;
|
|
71
|
-
pattern;
|
|
72
|
-
constructor(key, pattern) {
|
|
73
|
-
this.pattern = pattern;
|
|
74
|
-
this.key = key;
|
|
75
|
-
this.kind = "query";
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
var Include = class {
|
|
79
|
-
kind;
|
|
80
|
-
expr;
|
|
81
|
-
constructor(expr) {
|
|
82
|
-
this.expr = expr;
|
|
83
|
-
this.kind = "include";
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
var Exclude = class {
|
|
87
|
-
kind;
|
|
88
|
-
expr;
|
|
89
|
-
constructor(expr) {
|
|
90
|
-
this.expr = expr;
|
|
91
|
-
this.kind = "exclude";
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
function and(...args) {
|
|
95
|
-
return new And(...args);
|
|
96
|
-
}
|
|
97
|
-
function or(...args) {
|
|
98
|
-
return new Or(...args);
|
|
99
|
-
}
|
|
100
|
-
function not(expr) {
|
|
101
|
-
return new Not(expr);
|
|
102
|
-
}
|
|
103
|
-
function id(pattern, params) {
|
|
104
|
-
return new Id(pattern, params);
|
|
105
|
-
}
|
|
106
|
-
function moduleType(pattern) {
|
|
107
|
-
return new ModuleType(pattern);
|
|
108
|
-
}
|
|
109
|
-
function code(pattern) {
|
|
110
|
-
return new Code(pattern);
|
|
111
|
-
}
|
|
112
|
-
function query(key, pattern) {
|
|
113
|
-
return new Query(key, pattern);
|
|
114
|
-
}
|
|
115
|
-
function include(expr) {
|
|
116
|
-
return new Include(expr);
|
|
117
|
-
}
|
|
118
|
-
function exclude(expr) {
|
|
119
|
-
return new Exclude(expr);
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* convert a queryObject to FilterExpression like
|
|
123
|
-
* ```js
|
|
124
|
-
* and(query(k1, v1), query(k2, v2))
|
|
125
|
-
* ```
|
|
126
|
-
* @param queryFilterObject The query filter object needs to be matched.
|
|
127
|
-
* @returns a `And` FilterExpression
|
|
128
|
-
*/
|
|
129
|
-
function queries(queryFilter) {
|
|
130
|
-
return and(...Object.entries(queryFilter).map(([key, value]) => {
|
|
131
|
-
return new Query(key, value);
|
|
132
|
-
}));
|
|
133
|
-
}
|
|
134
|
-
function interpreter(exprs, code$1, id$1, moduleType$1) {
|
|
135
|
-
let arr = [];
|
|
136
|
-
if (Array.isArray(exprs)) arr = exprs;
|
|
137
|
-
else arr = [exprs];
|
|
138
|
-
return interpreterImpl(arr, code$1, id$1, moduleType$1);
|
|
139
|
-
}
|
|
140
|
-
function interpreterImpl(expr, code$1, id$1, moduleType$1, ctx = {}) {
|
|
141
|
-
let hasInclude = false;
|
|
142
|
-
for (const e of expr) switch (e.kind) {
|
|
143
|
-
case "include":
|
|
144
|
-
hasInclude = true;
|
|
145
|
-
if (exprInterpreter(e.expr, code$1, id$1, moduleType$1, ctx)) return true;
|
|
146
|
-
break;
|
|
147
|
-
case "exclude":
|
|
148
|
-
if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return false;
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
151
|
-
return !hasInclude;
|
|
152
|
-
}
|
|
153
|
-
function exprInterpreter(expr, code$1, id$1, moduleType$1, ctx = {}) {
|
|
154
|
-
switch (expr.kind) {
|
|
155
|
-
case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx));
|
|
156
|
-
case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx));
|
|
157
|
-
case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1, ctx);
|
|
158
|
-
case "id":
|
|
159
|
-
if (id$1 === void 0) throw new Error("`id` is required for `id` expression");
|
|
160
|
-
if (expr.params.cleanUrl) id$1 = cleanUrl(id$1);
|
|
161
|
-
return typeof expr.pattern === "string" ? id$1 === expr.pattern : expr.pattern.test(id$1);
|
|
162
|
-
case "moduleType":
|
|
163
|
-
if (moduleType$1 === void 0) throw new Error("`moduleType` is required for `moduleType` expression");
|
|
164
|
-
return moduleType$1 === expr.pattern;
|
|
165
|
-
case "code":
|
|
166
|
-
if (code$1 === void 0) throw new Error("`code` is required for `code` expression");
|
|
167
|
-
return typeof expr.pattern === "string" ? code$1.includes(expr.pattern) : expr.pattern.test(code$1);
|
|
168
|
-
case "query": {
|
|
169
|
-
if (id$1 === void 0) throw new Error("`id` is required for `Query` expression");
|
|
170
|
-
if (!ctx.urlSearchParamsCache) {
|
|
171
|
-
let queryString = extractQueryWithoutFragment(id$1);
|
|
172
|
-
ctx.urlSearchParamsCache = new URLSearchParams(queryString);
|
|
173
|
-
}
|
|
174
|
-
let urlParams = ctx.urlSearchParamsCache;
|
|
175
|
-
if (typeof expr.pattern === "boolean") if (expr.pattern) return urlParams.has(expr.key);
|
|
176
|
-
else return !urlParams.has(expr.key);
|
|
177
|
-
else if (typeof expr.pattern === "string") return urlParams.get(expr.key) === expr.pattern;
|
|
178
|
-
else return expr.pattern.test(urlParams.get(expr.key) ?? "");
|
|
179
|
-
}
|
|
180
|
-
default: throw new Error(`Expression ${JSON.stringify(expr)} is not expected.`);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
//#endregion
|
|
185
|
-
//#region src/filter-vite-plugins.ts
|
|
186
|
-
/**
|
|
187
|
-
* Filters out Vite plugins that have `apply: 'serve'` set.
|
|
188
|
-
*
|
|
189
|
-
* Since Rolldown operates in build mode, plugins marked with `apply: 'serve'`
|
|
190
|
-
* are intended only for Vite's dev server and should be excluded from the build process.
|
|
191
|
-
*
|
|
192
|
-
* @param plugins - Array of plugins (can include nested arrays)
|
|
193
|
-
* @returns Filtered array with serve-only plugins removed
|
|
194
|
-
*
|
|
195
|
-
* @example
|
|
196
|
-
* ```ts
|
|
197
|
-
* import { defineConfig } from 'rolldown';
|
|
198
|
-
* import { filterVitePlugins } from '@rolldown/pluginutils';
|
|
199
|
-
* import viteReact from '@vitejs/plugin-react';
|
|
200
|
-
*
|
|
201
|
-
* export default defineConfig({
|
|
202
|
-
* plugins: filterVitePlugins([
|
|
203
|
-
* viteReact(),
|
|
204
|
-
* {
|
|
205
|
-
* name: 'dev-only',
|
|
206
|
-
* apply: 'serve', // This will be filtered out
|
|
207
|
-
* // ...
|
|
208
|
-
* }
|
|
209
|
-
* ])
|
|
210
|
-
* });
|
|
211
|
-
* ```
|
|
212
|
-
*/
|
|
213
|
-
function filterVitePlugins(plugins) {
|
|
214
|
-
if (!plugins) return [];
|
|
215
|
-
const pluginArray = Array.isArray(plugins) ? plugins : [plugins];
|
|
216
|
-
const result = [];
|
|
217
|
-
for (const plugin of pluginArray) {
|
|
218
|
-
if (!plugin) continue;
|
|
219
|
-
if (Array.isArray(plugin)) {
|
|
220
|
-
result.push(...filterVitePlugins(plugin));
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
const pluginWithApply = plugin;
|
|
224
|
-
if ("apply" in pluginWithApply) {
|
|
225
|
-
const applyValue = pluginWithApply.apply;
|
|
226
|
-
if (typeof applyValue === "function") try {
|
|
227
|
-
if (applyValue({}, {
|
|
228
|
-
command: "build",
|
|
229
|
-
mode: "production"
|
|
230
|
-
})) result.push(plugin);
|
|
231
|
-
} catch {
|
|
232
|
-
result.push(plugin);
|
|
233
|
-
}
|
|
234
|
-
else if (applyValue === "serve") continue;
|
|
235
|
-
else result.push(plugin);
|
|
236
|
-
} else result.push(plugin);
|
|
237
|
-
}
|
|
238
|
-
return result;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
//#endregion
|
|
242
|
-
//#region src/simple-filters.ts
|
|
243
|
-
/**
|
|
244
|
-
* Constructs a RegExp that matches the exact string specified.
|
|
245
|
-
*
|
|
246
|
-
* This is useful for plugin hook filters.
|
|
247
|
-
*
|
|
248
|
-
* @param str the string to match.
|
|
249
|
-
* @param flags flags for the RegExp.
|
|
250
|
-
*
|
|
251
|
-
* @example
|
|
252
|
-
* ```ts
|
|
253
|
-
* import { exactRegex } from '@rolldown/pluginutils';
|
|
254
|
-
* const plugin = {
|
|
255
|
-
* name: 'plugin',
|
|
256
|
-
* resolveId: {
|
|
257
|
-
* filter: { id: exactRegex('foo') },
|
|
258
|
-
* handler(id) {} // will only be called for `foo`
|
|
259
|
-
* }
|
|
260
|
-
* }
|
|
261
|
-
* ```
|
|
262
|
-
*/
|
|
263
|
-
function exactRegex(str, flags) {
|
|
264
|
-
return new RegExp(`^${escapeRegex(str)}$`, flags);
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Constructs a RegExp that matches a value that has the specified prefix.
|
|
268
|
-
*
|
|
269
|
-
* This is useful for plugin hook filters.
|
|
270
|
-
*
|
|
271
|
-
* @param str the string to match.
|
|
272
|
-
* @param flags flags for the RegExp.
|
|
273
|
-
*
|
|
274
|
-
* @example
|
|
275
|
-
* ```ts
|
|
276
|
-
* import { prefixRegex } from '@rolldown/pluginutils';
|
|
277
|
-
* const plugin = {
|
|
278
|
-
* name: 'plugin',
|
|
279
|
-
* resolveId: {
|
|
280
|
-
* filter: { id: prefixRegex('foo') },
|
|
281
|
-
* handler(id) {} // will only be called for IDs starting with `foo`
|
|
282
|
-
* }
|
|
283
|
-
* }
|
|
284
|
-
* ```
|
|
285
|
-
*/
|
|
286
|
-
function prefixRegex(str, flags) {
|
|
287
|
-
return new RegExp(`^${escapeRegex(str)}`, flags);
|
|
288
|
-
}
|
|
289
|
-
const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
|
|
290
|
-
function escapeRegex(str) {
|
|
291
|
-
return str.replace(escapeRegexRE, "\\$&");
|
|
292
|
-
}
|
|
293
|
-
function makeIdFiltersToMatchWithQuery(input) {
|
|
294
|
-
if (!Array.isArray(input)) return makeIdFilterToMatchWithQuery(input);
|
|
295
|
-
return input.map((i) => makeIdFilterToMatchWithQuery(i));
|
|
296
|
-
}
|
|
297
|
-
function makeIdFilterToMatchWithQuery(input) {
|
|
298
|
-
if (typeof input === "string") return `${input}{?*,}`;
|
|
299
|
-
return makeRegexIdFilterToMatchWithQuery(input);
|
|
300
|
-
}
|
|
301
|
-
function makeRegexIdFilterToMatchWithQuery(input) {
|
|
302
|
-
return new RegExp(input.source.replace(/(?<!\\)\$/g, "(?:\\?.*)?$"), input.flags);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
//#endregion
|
|
306
|
-
export { and, code, exactRegex, exclude, exprInterpreter, filterVitePlugins, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, queries, query };
|