@rolldown/pluginutils 1.0.0-beta.35 → 1.0.0-beta.37

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rolldown/pluginutils",
3
- "version": "1.0.0-beta.35",
3
+ "version": "1.0.0-beta.37",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -11,14 +11,12 @@
11
11
  "publishConfig": {
12
12
  "access": "public"
13
13
  },
14
- "main": "./dist/index.cjs",
15
- "module": "./dist/index.js",
16
- "types": "./dist/index.d.ts",
14
+ "main": "./dist/index.mjs",
15
+ "module": "./dist/index.mjs",
16
+ "types": "./dist/index.d.mts",
17
17
  "exports": {
18
- ".": {
19
- "import": "./dist/index.js",
20
- "require": "./dist/index.cjs"
21
- }
18
+ ".": "./dist/index.mjs",
19
+ "./package.json": "./package.json"
22
20
  },
23
21
  "files": [
24
22
  "dist"
@@ -26,9 +24,13 @@
26
24
  "devDependencies": {
27
25
  "@types/picomatch": "^4.0.0",
28
26
  "picomatch": "^4.0.2",
29
- "tsdown": "0.14.2",
27
+ "tsdown": "0.15.0",
30
28
  "vitest": "^3.0.1"
31
29
  },
30
+ "tsdown": {
31
+ "exports": true,
32
+ "fixedExtension": true
33
+ },
32
34
  "scripts": {
33
35
  "build": "tsdown",
34
36
  "test": "vitest --typecheck"
package/dist/index.cjs DELETED
@@ -1,266 +0,0 @@
1
-
2
- //#region src/utils.ts
3
- const postfixRE = /[?#].*$/;
4
- function cleanUrl(url) {
5
- return url.replace(postfixRE, "");
6
- }
7
- function extractQueryWithoutFragment(url) {
8
- const questionMarkIndex = url.indexOf("?");
9
- if (questionMarkIndex === -1) return "";
10
- const fragmentIndex = url.indexOf("#", questionMarkIndex);
11
- if (fragmentIndex === -1) return url.substring(questionMarkIndex);
12
- else return url.substring(questionMarkIndex, fragmentIndex);
13
- }
14
-
15
- //#endregion
16
- //#region src/composable-filters.ts
17
- var And = class {
18
- kind;
19
- args;
20
- constructor(...args) {
21
- if (args.length === 0) throw new Error("`And` expects at least one operand");
22
- this.args = args;
23
- this.kind = "and";
24
- }
25
- };
26
- var Or = class {
27
- kind;
28
- args;
29
- constructor(...args) {
30
- if (args.length === 0) throw new Error("`Or` expects at least one operand");
31
- this.args = args;
32
- this.kind = "or";
33
- }
34
- };
35
- var Not = class {
36
- kind;
37
- expr;
38
- constructor(expr) {
39
- this.expr = expr;
40
- this.kind = "not";
41
- }
42
- };
43
- var Id = class {
44
- kind;
45
- pattern;
46
- params;
47
- constructor(pattern, params) {
48
- this.pattern = pattern;
49
- this.kind = "id";
50
- this.params = params ?? { cleanUrl: false };
51
- }
52
- };
53
- var ModuleType = class {
54
- kind;
55
- pattern;
56
- constructor(pattern) {
57
- this.pattern = pattern;
58
- this.kind = "moduleType";
59
- }
60
- };
61
- var Code = class {
62
- kind;
63
- pattern;
64
- constructor(expr) {
65
- this.pattern = expr;
66
- this.kind = "code";
67
- }
68
- };
69
- var Query = class {
70
- kind;
71
- key;
72
- pattern;
73
- constructor(key, pattern) {
74
- this.pattern = pattern;
75
- this.key = key;
76
- this.kind = "query";
77
- }
78
- };
79
- var Include = class {
80
- kind;
81
- expr;
82
- constructor(expr) {
83
- this.expr = expr;
84
- this.kind = "include";
85
- }
86
- };
87
- var Exclude = class {
88
- kind;
89
- expr;
90
- constructor(expr) {
91
- this.expr = expr;
92
- this.kind = "exclude";
93
- }
94
- };
95
- function and(...args) {
96
- return new And(...args);
97
- }
98
- function or(...args) {
99
- return new Or(...args);
100
- }
101
- function not(expr) {
102
- return new Not(expr);
103
- }
104
- function id(pattern, params) {
105
- return new Id(pattern, params);
106
- }
107
- function moduleType(pattern) {
108
- return new ModuleType(pattern);
109
- }
110
- function code(pattern) {
111
- return new Code(pattern);
112
- }
113
- function query(key, pattern) {
114
- return new Query(key, pattern);
115
- }
116
- function include(expr) {
117
- return new Include(expr);
118
- }
119
- function exclude(expr) {
120
- return new Exclude(expr);
121
- }
122
- /**
123
- * convert a queryObject to FilterExpression like
124
- * ```js
125
- * and(query(k1, v1), query(k2, v2))
126
- * ```
127
- * @param queryFilterObject The query filter object needs to be matched.
128
- * @returns a `And` FilterExpression
129
- */
130
- function queries(queryFilter) {
131
- let arr = Object.entries(queryFilter).map(([key, value]) => {
132
- return new Query(key, value);
133
- });
134
- return and(...arr);
135
- }
136
- function interpreter(exprs, code$1, id$1, moduleType$1) {
137
- let arr = [];
138
- if (Array.isArray(exprs)) arr = exprs;
139
- else arr = [exprs];
140
- return interpreterImpl(arr, code$1, id$1, moduleType$1);
141
- }
142
- function interpreterImpl(expr, code$1, id$1, moduleType$1, ctx = {}) {
143
- let hasInclude = false;
144
- for (const e of expr) switch (e.kind) {
145
- case "include":
146
- hasInclude = true;
147
- if (exprInterpreter(e.expr, code$1, id$1, moduleType$1, ctx)) return true;
148
- break;
149
- case "exclude":
150
- if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return false;
151
- break;
152
- }
153
- return !hasInclude;
154
- }
155
- function exprInterpreter(expr, code$1, id$1, moduleType$1, ctx = {}) {
156
- switch (expr.kind) {
157
- case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx));
158
- case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx));
159
- case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1, ctx);
160
- case "id":
161
- if (id$1 === void 0) throw new Error("`id` is required for `id` expression");
162
- if (expr.params.cleanUrl) id$1 = cleanUrl(id$1);
163
- return typeof expr.pattern === "string" ? id$1 === expr.pattern : expr.pattern.test(id$1);
164
- case "moduleType":
165
- if (moduleType$1 === void 0) throw new Error("`moduleType` is required for `moduleType` expression");
166
- return moduleType$1 === expr.pattern;
167
- case "code":
168
- if (code$1 === void 0) throw new Error("`code` is required for `code` expression");
169
- return typeof expr.pattern === "string" ? code$1.includes(expr.pattern) : expr.pattern.test(code$1);
170
- case "query": {
171
- if (id$1 === void 0) throw new Error("`id` is required for `Query` expression");
172
- if (!ctx.urlSearchParamsCache) {
173
- let queryString = extractQueryWithoutFragment(id$1);
174
- ctx.urlSearchParamsCache = new URLSearchParams(queryString);
175
- }
176
- let urlParams = ctx.urlSearchParamsCache;
177
- if (typeof expr.pattern === "boolean") if (expr.pattern) return urlParams.has(expr.key);
178
- else return !urlParams.has(expr.key);
179
- else if (typeof expr.pattern === "string") return urlParams.get(expr.key) === expr.pattern;
180
- else return expr.pattern.test(urlParams.get(expr.key) ?? "");
181
- }
182
- default: throw new Error(`Expression ${JSON.stringify(expr)} is not expected.`);
183
- }
184
- }
185
-
186
- //#endregion
187
- //#region src/simple-filters.ts
188
- /**
189
- * Constructs a RegExp that matches the exact string specified.
190
- *
191
- * This is useful for plugin hook filters.
192
- *
193
- * @param str the string to match.
194
- * @param flags flags for the RegExp.
195
- *
196
- * @example
197
- * ```ts
198
- * import { exactRegex } from '@rolldown/pluginutils';
199
- * const plugin = {
200
- * name: 'plugin',
201
- * resolveId: {
202
- * filter: { id: exactRegex('foo') },
203
- * handler(id) {} // will only be called for `foo`
204
- * }
205
- * }
206
- * ```
207
- */
208
- function exactRegex(str, flags) {
209
- return new RegExp(`^${escapeRegex(str)}$`, flags);
210
- }
211
- /**
212
- * Constructs a RegExp that matches a value that has the specified prefix.
213
- *
214
- * This is useful for plugin hook filters.
215
- *
216
- * @param str the string to match.
217
- * @param flags flags for the RegExp.
218
- *
219
- * @example
220
- * ```ts
221
- * import { prefixRegex } from '@rolldown/pluginutils';
222
- * const plugin = {
223
- * name: 'plugin',
224
- * resolveId: {
225
- * filter: { id: prefixRegex('foo') },
226
- * handler(id) {} // will only be called for IDs starting with `foo`
227
- * }
228
- * }
229
- * ```
230
- */
231
- function prefixRegex(str, flags) {
232
- return new RegExp(`^${escapeRegex(str)}`, flags);
233
- }
234
- const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
235
- function escapeRegex(str) {
236
- return str.replace(escapeRegexRE, "\\$&");
237
- }
238
- function makeIdFiltersToMatchWithQuery(input) {
239
- if (!Array.isArray(input)) return makeIdFilterToMatchWithQuery(input);
240
- return input.map((i) => makeIdFilterToMatchWithQuery(i));
241
- }
242
- function makeIdFilterToMatchWithQuery(input) {
243
- if (typeof input === "string") return `${input}{?*,}`;
244
- return makeRegexIdFilterToMatchWithQuery(input);
245
- }
246
- function makeRegexIdFilterToMatchWithQuery(input) {
247
- return new RegExp(input.source.replace(/(?<!\\)\$/g, "(?:\\?.*)?$"), input.flags);
248
- }
249
-
250
- //#endregion
251
- exports.and = and;
252
- exports.code = code;
253
- exports.exactRegex = exactRegex;
254
- exports.exclude = exclude;
255
- exports.exprInterpreter = exprInterpreter;
256
- exports.id = id;
257
- exports.include = include;
258
- exports.interpreter = interpreter;
259
- exports.interpreterImpl = interpreterImpl;
260
- exports.makeIdFiltersToMatchWithQuery = makeIdFiltersToMatchWithQuery;
261
- exports.moduleType = moduleType;
262
- exports.not = not;
263
- exports.or = or;
264
- exports.prefixRegex = prefixRegex;
265
- exports.queries = queries;
266
- exports.query = query;
package/dist/index.d.ts DELETED
@@ -1,157 +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/simple-filters.d.ts
86
- /**
87
- * Constructs a RegExp that matches the exact string specified.
88
- *
89
- * This is useful for plugin hook filters.
90
- *
91
- * @param str the string to match.
92
- * @param flags flags for the RegExp.
93
- *
94
- * @example
95
- * ```ts
96
- * import { exactRegex } from '@rolldown/pluginutils';
97
- * const plugin = {
98
- * name: 'plugin',
99
- * resolveId: {
100
- * filter: { id: exactRegex('foo') },
101
- * handler(id) {} // will only be called for `foo`
102
- * }
103
- * }
104
- * ```
105
- */
106
- declare function exactRegex(str: string, flags?: string): RegExp;
107
- /**
108
- * Constructs a RegExp that matches a value that has the specified prefix.
109
- *
110
- * This is useful for plugin hook filters.
111
- *
112
- * @param str the string to match.
113
- * @param flags flags for the RegExp.
114
- *
115
- * @example
116
- * ```ts
117
- * import { prefixRegex } from '@rolldown/pluginutils';
118
- * const plugin = {
119
- * name: 'plugin',
120
- * resolveId: {
121
- * filter: { id: prefixRegex('foo') },
122
- * handler(id) {} // will only be called for IDs starting with `foo`
123
- * }
124
- * }
125
- * ```
126
- */
127
- declare function prefixRegex(str: string, flags?: string): RegExp;
128
- type WidenString<T> = T extends string ? string : T;
129
- /**
130
- * Converts a id filter to match with an id with a query.
131
- *
132
- * @param input the id filters to convert.
133
- *
134
- * @example
135
- * ```ts
136
- * import { makeIdFiltersToMatchWithQuery } from '@rolldown/pluginutils';
137
- * const plugin = {
138
- * name: 'plugin',
139
- * transform: {
140
- * filter: { id: makeIdFiltersToMatchWithQuery(['**' + '/*.js', /\.ts$/]) },
141
- * // The handler will be called for IDs like:
142
- * // - foo.js
143
- * // - foo.js?foo
144
- * // - foo.txt?foo.js
145
- * // - foo.ts
146
- * // - foo.ts?foo
147
- * // - foo.txt?foo.ts
148
- * handler(code, id) {}
149
- * }
150
- * }
151
- * ```
152
- */
153
- declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: T): WidenString<T>;
154
- declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: readonly T[]): WidenString<T>[];
155
- declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
156
- //#endregion
157
- export { FilterExpression, FilterExpressionKind, QueryFilterObject, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, queries, query };
File without changes
File without changes