@rolldown/pluginutils 1.0.0-beta.8-commit.e5c11c6 → 1.0.0-beta.8-commit.2a5c6a6

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/index.cjs ADDED
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+
3
+ //#region src/utils.ts
4
+ const postfixRE = /[?#].*$/;
5
+ function cleanUrl(url) {
6
+ return url.replace(postfixRE, "");
7
+ }
8
+
9
+ //#endregion
10
+ //#region src/composable-filters.ts
11
+ var And = class {
12
+ kind;
13
+ args;
14
+ constructor(...args) {
15
+ if (args.length === 0) throw new Error("`And` expects at least one operand");
16
+ this.args = args;
17
+ this.kind = "and";
18
+ }
19
+ };
20
+ var Or = class {
21
+ kind;
22
+ args;
23
+ constructor(...args) {
24
+ if (args.length === 0) throw new Error("`Or` expects at least one operand");
25
+ this.args = args;
26
+ this.kind = "or";
27
+ }
28
+ };
29
+ var Not = class {
30
+ kind;
31
+ expr;
32
+ constructor(expr) {
33
+ this.expr = expr;
34
+ this.kind = "not";
35
+ }
36
+ };
37
+ var Id = class {
38
+ kind;
39
+ pattern;
40
+ params;
41
+ constructor(pattern, params) {
42
+ this.pattern = pattern;
43
+ this.kind = "id";
44
+ this.params = params ?? { cleanUrl: false };
45
+ }
46
+ };
47
+ var ModuleType = class {
48
+ kind;
49
+ pattern;
50
+ constructor(pattern) {
51
+ this.pattern = pattern;
52
+ this.kind = "moduleType";
53
+ }
54
+ };
55
+ var Code = class {
56
+ kind;
57
+ pattern;
58
+ constructor(expr) {
59
+ this.pattern = expr;
60
+ this.kind = "code";
61
+ }
62
+ };
63
+ var Include = class {
64
+ kind;
65
+ expr;
66
+ constructor(expr) {
67
+ this.expr = expr;
68
+ this.kind = "include";
69
+ }
70
+ };
71
+ var Exclude = class {
72
+ kind;
73
+ expr;
74
+ constructor(expr) {
75
+ this.expr = expr;
76
+ this.kind = "exclude";
77
+ }
78
+ };
79
+ function and(...args) {
80
+ return new And(...args);
81
+ }
82
+ function or(...args) {
83
+ return new Or(...args);
84
+ }
85
+ function not(expr) {
86
+ return new Not(expr);
87
+ }
88
+ function id(pattern, params) {
89
+ return new Id(pattern, params);
90
+ }
91
+ function moduleType(pattern) {
92
+ return new ModuleType(pattern);
93
+ }
94
+ function code(pattern) {
95
+ return new Code(pattern);
96
+ }
97
+ function include(expr) {
98
+ return new Include(expr);
99
+ }
100
+ function exclude(expr) {
101
+ return new Exclude(expr);
102
+ }
103
+ function interpreter(exprs, code$1, id$1, moduleType$1) {
104
+ let arr = [];
105
+ if (Array.isArray(exprs)) arr = exprs;
106
+ else arr = [exprs];
107
+ return interpreterImpl(arr, code$1, id$1, moduleType$1);
108
+ }
109
+ function interpreterImpl(expr, code$1, id$1, moduleType$1) {
110
+ let hasInclude = false;
111
+ for (const e of expr) switch (e.kind) {
112
+ case "include": {
113
+ hasInclude = true;
114
+ if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return true;
115
+ break;
116
+ }
117
+ case "exclude": {
118
+ if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return false;
119
+ break;
120
+ }
121
+ }
122
+ return !hasInclude;
123
+ }
124
+ function exprInterpreter(expr, code$1, id$1, moduleType$1) {
125
+ switch (expr.kind) {
126
+ case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1));
127
+ case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1));
128
+ case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1);
129
+ case "id": {
130
+ if (id$1 === void 0) throw new Error("`id` is required for `id` expression");
131
+ if (expr.params.cleanUrl) id$1 = cleanUrl(id$1);
132
+ return typeof expr.pattern === "string" ? id$1 === expr.pattern : expr.pattern.test(id$1);
133
+ }
134
+ case "moduleType": {
135
+ if (moduleType$1 === void 0) throw new Error("`moduleType` is required for `moduleType` expression");
136
+ return moduleType$1 === expr.pattern;
137
+ }
138
+ case "code": {
139
+ if (code$1 === void 0) throw new Error("`code` is required for `code` expression");
140
+ return typeof expr.pattern === "string" ? code$1.includes(expr.pattern) : expr.pattern.test(code$1);
141
+ }
142
+ default: throw new Error(`Expression kind ${expr.kind} is not expected.`);
143
+ }
144
+ }
145
+
146
+ //#endregion
147
+ //#region src/simple-filters.ts
148
+ /**
149
+ * Constructs a RegExp that matches the exact string specified.
150
+ *
151
+ * This is useful for plugin hook filters.
152
+ *
153
+ * @param str the string to match.
154
+ * @param flags flags for the RegExp.
155
+ */
156
+ function exactRegex(str, flags) {
157
+ return new RegExp(`^${escapeRegex(str)}$`, flags);
158
+ }
159
+ /**
160
+ * Constructs a RegExp that matches a value that has the specified prefix.
161
+ *
162
+ * This is useful for plugin hook filters.
163
+ *
164
+ * @param str the string to match.
165
+ * @param flags flags for the RegExp.
166
+ */
167
+ function prefixRegex(str, flags) {
168
+ return new RegExp(`^${escapeRegex(str)}`, flags);
169
+ }
170
+ const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
171
+ function escapeRegex(str) {
172
+ return str.replace(escapeRegexRE, "\\$&");
173
+ }
174
+ function makeIdFiltersToMatchWithQuery(input) {
175
+ if (!Array.isArray(input)) return makeIdFilterToMatchWithQuery(
176
+ // Array.isArray cannot narrow the type
177
+ // https://github.com/microsoft/TypeScript/issues/17002
178
+ input
179
+ );
180
+ return input.map((i) => makeIdFilterToMatchWithQuery(i));
181
+ }
182
+ function makeIdFilterToMatchWithQuery(input) {
183
+ if (typeof input === "string") return `${input}{?*,}`;
184
+ return makeRegexIdFilterToMatchWithQuery(input);
185
+ }
186
+ function makeRegexIdFilterToMatchWithQuery(input) {
187
+ return new RegExp(
188
+ // replace `$` with `(?:\?.*)?$` (ignore `\$`)
189
+ input.source.replace(/(?<!\\)\$/g, "(?:\\?.*)?$"),
190
+ input.flags
191
+ );
192
+ }
193
+
194
+ //#endregion
195
+ exports.And = And
196
+ exports.and = and
197
+ exports.code = code
198
+ exports.exactRegex = exactRegex
199
+ exports.exclude = exclude
200
+ exports.exprInterpreter = exprInterpreter
201
+ exports.id = id
202
+ exports.include = include
203
+ exports.interpreter = interpreter
204
+ exports.interpreterImpl = interpreterImpl
205
+ exports.makeIdFiltersToMatchWithQuery = makeIdFiltersToMatchWithQuery
206
+ exports.moduleType = moduleType
207
+ exports.not = not
208
+ exports.or = or
209
+ exports.prefixRegex = prefixRegex
@@ -0,0 +1,94 @@
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 | Include | Exclude;
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 IdParams {
23
+ cleanUrl?: boolean;
24
+ }
25
+ declare class Id {
26
+ kind: "id";
27
+ pattern: StringOrRegExp;
28
+ params: IdParams;
29
+ constructor(pattern: StringOrRegExp, params?: IdParams);
30
+ }
31
+ declare class ModuleType {
32
+ kind: "moduleType";
33
+ pattern: PluginModuleType;
34
+ constructor(pattern: PluginModuleType);
35
+ }
36
+ declare class Code {
37
+ kind: "code";
38
+ pattern: StringOrRegExp;
39
+ constructor(expr: StringOrRegExp);
40
+ }
41
+ declare class Include {
42
+ kind: "include";
43
+ expr: FilterExpression;
44
+ constructor(expr: FilterExpression);
45
+ }
46
+ declare class Exclude {
47
+ kind: "exclude";
48
+ expr: FilterExpression;
49
+ constructor(expr: FilterExpression);
50
+ }
51
+ declare function and(...args: FilterExpression[]): And;
52
+ declare function or(...args: FilterExpression[]): Or;
53
+ declare function not(expr: FilterExpression): Not;
54
+ declare function id(pattern: StringOrRegExp, params?: IdParams): Id;
55
+ declare function moduleType(pattern: PluginModuleType): ModuleType;
56
+ declare function code(pattern: StringOrRegExp): Code;
57
+ declare function include(expr: FilterExpression): Include;
58
+ declare function exclude(expr: FilterExpression): Exclude;
59
+ declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
60
+ declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
61
+ declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType): boolean;
62
+
63
+ //#endregion
64
+ //#region src/simple-filters.d.ts
65
+ /**
66
+ * Constructs a RegExp that matches the exact string specified.
67
+ *
68
+ * This is useful for plugin hook filters.
69
+ *
70
+ * @param str the string to match.
71
+ * @param flags flags for the RegExp.
72
+ */
73
+ declare function exactRegex(str: string, flags?: string): RegExp;
74
+ /**
75
+ * Constructs a RegExp that matches a value that has the specified prefix.
76
+ *
77
+ * This is useful for plugin hook filters.
78
+ *
79
+ * @param str the string to match.
80
+ * @param flags flags for the RegExp.
81
+ */
82
+ declare function prefixRegex(str: string, flags?: string): RegExp;
83
+ type WidenString<T> = T extends string ? string : T;
84
+ /**
85
+ * Converts a id filter to match with an id with a query.
86
+ *
87
+ * @param input the id filters to convert.
88
+ */
89
+ declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: T): WidenString<T>;
90
+ declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: readonly T[]): WidenString<T>[];
91
+ declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
92
+
93
+ //#endregion
94
+ export { And, FilterExpression, FilterExpressionKind, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex };
@@ -0,0 +1,94 @@
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 | Include | Exclude;
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 IdParams {
23
+ cleanUrl?: boolean;
24
+ }
25
+ declare class Id {
26
+ kind: "id";
27
+ pattern: StringOrRegExp;
28
+ params: IdParams;
29
+ constructor(pattern: StringOrRegExp, params?: IdParams);
30
+ }
31
+ declare class ModuleType {
32
+ kind: "moduleType";
33
+ pattern: PluginModuleType;
34
+ constructor(pattern: PluginModuleType);
35
+ }
36
+ declare class Code {
37
+ kind: "code";
38
+ pattern: StringOrRegExp;
39
+ constructor(expr: StringOrRegExp);
40
+ }
41
+ declare class Include {
42
+ kind: "include";
43
+ expr: FilterExpression;
44
+ constructor(expr: FilterExpression);
45
+ }
46
+ declare class Exclude {
47
+ kind: "exclude";
48
+ expr: FilterExpression;
49
+ constructor(expr: FilterExpression);
50
+ }
51
+ declare function and(...args: FilterExpression[]): And;
52
+ declare function or(...args: FilterExpression[]): Or;
53
+ declare function not(expr: FilterExpression): Not;
54
+ declare function id(pattern: StringOrRegExp, params?: IdParams): Id;
55
+ declare function moduleType(pattern: PluginModuleType): ModuleType;
56
+ declare function code(pattern: StringOrRegExp): Code;
57
+ declare function include(expr: FilterExpression): Include;
58
+ declare function exclude(expr: FilterExpression): Exclude;
59
+ declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
60
+ declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
61
+ declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType): boolean;
62
+
63
+ //#endregion
64
+ //#region src/simple-filters.d.ts
65
+ /**
66
+ * Constructs a RegExp that matches the exact string specified.
67
+ *
68
+ * This is useful for plugin hook filters.
69
+ *
70
+ * @param str the string to match.
71
+ * @param flags flags for the RegExp.
72
+ */
73
+ declare function exactRegex(str: string, flags?: string): RegExp;
74
+ /**
75
+ * Constructs a RegExp that matches a value that has the specified prefix.
76
+ *
77
+ * This is useful for plugin hook filters.
78
+ *
79
+ * @param str the string to match.
80
+ * @param flags flags for the RegExp.
81
+ */
82
+ declare function prefixRegex(str: string, flags?: string): RegExp;
83
+ type WidenString<T> = T extends string ? string : T;
84
+ /**
85
+ * Converts a id filter to match with an id with a query.
86
+ *
87
+ * @param input the id filters to convert.
88
+ */
89
+ declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: T): WidenString<T>;
90
+ declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: readonly T[]): WidenString<T>[];
91
+ declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
92
+
93
+ //#endregion
94
+ export { And, FilterExpression, FilterExpressionKind, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex };
package/dist/index.js ADDED
@@ -0,0 +1,193 @@
1
+ //#region src/utils.ts
2
+ const postfixRE = /[?#].*$/;
3
+ function cleanUrl(url) {
4
+ return url.replace(postfixRE, "");
5
+ }
6
+
7
+ //#endregion
8
+ //#region src/composable-filters.ts
9
+ var And = class {
10
+ kind;
11
+ args;
12
+ constructor(...args) {
13
+ if (args.length === 0) throw new Error("`And` expects at least one operand");
14
+ this.args = args;
15
+ this.kind = "and";
16
+ }
17
+ };
18
+ var Or = class {
19
+ kind;
20
+ args;
21
+ constructor(...args) {
22
+ if (args.length === 0) throw new Error("`Or` expects at least one operand");
23
+ this.args = args;
24
+ this.kind = "or";
25
+ }
26
+ };
27
+ var Not = class {
28
+ kind;
29
+ expr;
30
+ constructor(expr) {
31
+ this.expr = expr;
32
+ this.kind = "not";
33
+ }
34
+ };
35
+ var Id = class {
36
+ kind;
37
+ pattern;
38
+ params;
39
+ constructor(pattern, params) {
40
+ this.pattern = pattern;
41
+ this.kind = "id";
42
+ this.params = params ?? { cleanUrl: false };
43
+ }
44
+ };
45
+ var ModuleType = class {
46
+ kind;
47
+ pattern;
48
+ constructor(pattern) {
49
+ this.pattern = pattern;
50
+ this.kind = "moduleType";
51
+ }
52
+ };
53
+ var Code = class {
54
+ kind;
55
+ pattern;
56
+ constructor(expr) {
57
+ this.pattern = expr;
58
+ this.kind = "code";
59
+ }
60
+ };
61
+ var Include = class {
62
+ kind;
63
+ expr;
64
+ constructor(expr) {
65
+ this.expr = expr;
66
+ this.kind = "include";
67
+ }
68
+ };
69
+ var Exclude = class {
70
+ kind;
71
+ expr;
72
+ constructor(expr) {
73
+ this.expr = expr;
74
+ this.kind = "exclude";
75
+ }
76
+ };
77
+ function and(...args) {
78
+ return new And(...args);
79
+ }
80
+ function or(...args) {
81
+ return new Or(...args);
82
+ }
83
+ function not(expr) {
84
+ return new Not(expr);
85
+ }
86
+ function id(pattern, params) {
87
+ return new Id(pattern, params);
88
+ }
89
+ function moduleType(pattern) {
90
+ return new ModuleType(pattern);
91
+ }
92
+ function code(pattern) {
93
+ return new Code(pattern);
94
+ }
95
+ function include(expr) {
96
+ return new Include(expr);
97
+ }
98
+ function exclude(expr) {
99
+ return new Exclude(expr);
100
+ }
101
+ function interpreter(exprs, code$1, id$1, moduleType$1) {
102
+ let arr = [];
103
+ if (Array.isArray(exprs)) arr = exprs;
104
+ else arr = [exprs];
105
+ return interpreterImpl(arr, code$1, id$1, moduleType$1);
106
+ }
107
+ function interpreterImpl(expr, code$1, id$1, moduleType$1) {
108
+ let hasInclude = false;
109
+ for (const e of expr) switch (e.kind) {
110
+ case "include": {
111
+ hasInclude = true;
112
+ if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return true;
113
+ break;
114
+ }
115
+ case "exclude": {
116
+ if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return false;
117
+ break;
118
+ }
119
+ }
120
+ return !hasInclude;
121
+ }
122
+ function exprInterpreter(expr, code$1, id$1, moduleType$1) {
123
+ switch (expr.kind) {
124
+ case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1));
125
+ case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1));
126
+ case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1);
127
+ case "id": {
128
+ if (id$1 === void 0) throw new Error("`id` is required for `id` expression");
129
+ if (expr.params.cleanUrl) id$1 = cleanUrl(id$1);
130
+ return typeof expr.pattern === "string" ? id$1 === expr.pattern : expr.pattern.test(id$1);
131
+ }
132
+ case "moduleType": {
133
+ if (moduleType$1 === void 0) throw new Error("`moduleType` is required for `moduleType` expression");
134
+ return moduleType$1 === expr.pattern;
135
+ }
136
+ case "code": {
137
+ if (code$1 === void 0) throw new Error("`code` is required for `code` expression");
138
+ return typeof expr.pattern === "string" ? code$1.includes(expr.pattern) : expr.pattern.test(code$1);
139
+ }
140
+ default: throw new Error(`Expression kind ${expr.kind} is not expected.`);
141
+ }
142
+ }
143
+
144
+ //#endregion
145
+ //#region src/simple-filters.ts
146
+ /**
147
+ * Constructs a RegExp that matches the exact string specified.
148
+ *
149
+ * This is useful for plugin hook filters.
150
+ *
151
+ * @param str the string to match.
152
+ * @param flags flags for the RegExp.
153
+ */
154
+ function exactRegex(str, flags) {
155
+ return new RegExp(`^${escapeRegex(str)}$`, flags);
156
+ }
157
+ /**
158
+ * Constructs a RegExp that matches a value that has the specified prefix.
159
+ *
160
+ * This is useful for plugin hook filters.
161
+ *
162
+ * @param str the string to match.
163
+ * @param flags flags for the RegExp.
164
+ */
165
+ function prefixRegex(str, flags) {
166
+ return new RegExp(`^${escapeRegex(str)}`, flags);
167
+ }
168
+ const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
169
+ function escapeRegex(str) {
170
+ return str.replace(escapeRegexRE, "\\$&");
171
+ }
172
+ function makeIdFiltersToMatchWithQuery(input) {
173
+ if (!Array.isArray(input)) return makeIdFilterToMatchWithQuery(
174
+ // Array.isArray cannot narrow the type
175
+ // https://github.com/microsoft/TypeScript/issues/17002
176
+ input
177
+ );
178
+ return input.map((i) => makeIdFilterToMatchWithQuery(i));
179
+ }
180
+ function makeIdFilterToMatchWithQuery(input) {
181
+ if (typeof input === "string") return `${input}{?*,}`;
182
+ return makeRegexIdFilterToMatchWithQuery(input);
183
+ }
184
+ function makeRegexIdFilterToMatchWithQuery(input) {
185
+ return new RegExp(
186
+ // replace `$` with `(?:\?.*)?$` (ignore `\$`)
187
+ input.source.replace(/(?<!\\)\$/g, "(?:\\?.*)?$"),
188
+ input.flags
189
+ );
190
+ }
191
+
192
+ //#endregion
193
+ export { And, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rolldown/pluginutils",
3
- "version": "1.0.0-beta.8-commit.e5c11c6",
3
+ "version": "1.0.0-beta.8-commit.2a5c6a6",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -11,11 +11,13 @@
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
17
  "exports": {
15
18
  ".": {
16
- "import": "./dist/esm/index.js",
17
- "require": "./dist/cjs/index.js",
18
- "types": "./dist/cjs/index.d.ts"
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs"
19
21
  }
20
22
  },
21
23
  "files": [
@@ -24,13 +26,11 @@
24
26
  "devDependencies": {
25
27
  "@types/picomatch": "^4.0.0",
26
28
  "picomatch": "^4.0.2",
29
+ "tsdown": "0.11.1",
27
30
  "vitest": "^3.0.1"
28
31
  },
29
32
  "scripts": {
30
- "build": "node build.mjs",
31
- "build:all": "npm run build:esm && npm run build:cjs",
32
- "build:esm": "tsc -p ./tsconfig.esm.json",
33
- "build:cjs": "tsc -p ./tsconfig.cjs.json",
33
+ "build": "tsdown",
34
34
  "test": "vitest --typecheck"
35
35
  }
36
36
  }
@@ -1,57 +0,0 @@
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 | Include | Exclude;
5
- export type TopLevelFilterExpression = Include | Exclude;
6
- export 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
- declare class Id {
22
- kind: 'id';
23
- pattern: StringOrRegExp;
24
- constructor(pattern: StringOrRegExp);
25
- }
26
- declare class ModuleType {
27
- kind: 'moduleType';
28
- pattern: PluginModuleType;
29
- constructor(pattern: PluginModuleType);
30
- }
31
- declare class Code {
32
- kind: 'code';
33
- pattern: StringOrRegExp;
34
- constructor(expr: StringOrRegExp);
35
- }
36
- declare class Include {
37
- kind: 'include';
38
- expr: FilterExpression;
39
- constructor(expr: FilterExpression);
40
- }
41
- declare class Exclude {
42
- kind: 'exclude';
43
- expr: FilterExpression;
44
- constructor(expr: FilterExpression);
45
- }
46
- export declare function and(...args: FilterExpression[]): And;
47
- export declare function or(...args: FilterExpression[]): Or;
48
- export declare function not(expr: FilterExpression): Not;
49
- export declare function id(pattern: StringOrRegExp): Id;
50
- export declare function moduleType(pattern: PluginModuleType): ModuleType;
51
- export declare function code(pattern: StringOrRegExp): Code;
52
- export declare function include(expr: FilterExpression): Include;
53
- export declare function exclude(expr: FilterExpression): Exclude;
54
- export declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
55
- export declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
56
- export declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType): boolean;
57
- export {};
@@ -1,178 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.And = void 0;
4
- exports.and = and;
5
- exports.or = or;
6
- exports.not = not;
7
- exports.id = id;
8
- exports.moduleType = moduleType;
9
- exports.code = code;
10
- exports.include = include;
11
- exports.exclude = exclude;
12
- exports.interpreter = interpreter;
13
- exports.interpreterImpl = interpreterImpl;
14
- exports.exprInterpreter = exprInterpreter;
15
- class And {
16
- kind;
17
- args;
18
- constructor(...args) {
19
- if (args.length === 0) {
20
- throw new Error('`And` expects at least one operand');
21
- }
22
- this.args = args;
23
- this.kind = 'and';
24
- }
25
- }
26
- exports.And = And;
27
- class Or {
28
- kind;
29
- args;
30
- constructor(...args) {
31
- if (args.length === 0) {
32
- throw new Error('`Or` expects at least one operand');
33
- }
34
- this.args = args;
35
- this.kind = 'or';
36
- }
37
- }
38
- class Not {
39
- kind;
40
- expr;
41
- constructor(expr) {
42
- this.expr = expr;
43
- this.kind = 'not';
44
- }
45
- }
46
- class Id {
47
- kind;
48
- pattern;
49
- constructor(pattern) {
50
- this.pattern = pattern;
51
- this.kind = 'id';
52
- }
53
- }
54
- class ModuleType {
55
- kind;
56
- pattern;
57
- constructor(pattern) {
58
- this.pattern = pattern;
59
- this.kind = 'moduleType';
60
- }
61
- }
62
- class Code {
63
- kind;
64
- pattern;
65
- constructor(expr) {
66
- this.pattern = expr;
67
- this.kind = 'code';
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
- function and(...args) {
87
- return new And(...args);
88
- }
89
- function or(...args) {
90
- return new Or(...args);
91
- }
92
- function not(expr) {
93
- return new Not(expr);
94
- }
95
- function id(pattern) {
96
- return new Id(pattern);
97
- }
98
- function moduleType(pattern) {
99
- return new ModuleType(pattern);
100
- }
101
- function code(pattern) {
102
- return new Code(pattern);
103
- }
104
- function include(expr) {
105
- return new Include(expr);
106
- }
107
- function exclude(expr) {
108
- return new Exclude(expr);
109
- }
110
- function interpreter(exprs, code, id, moduleType) {
111
- let arr = [];
112
- if (Array.isArray(exprs)) {
113
- arr = exprs;
114
- }
115
- else {
116
- arr = [exprs];
117
- }
118
- return interpreterImpl(arr, code, id, moduleType);
119
- }
120
- function interpreterImpl(expr, code, id, moduleType) {
121
- let hasInclude = false;
122
- for (const e of expr) {
123
- switch (e.kind) {
124
- case 'include': {
125
- hasInclude = true;
126
- if (exprInterpreter(e.expr, code, id, moduleType)) {
127
- return true;
128
- }
129
- break;
130
- }
131
- case 'exclude': {
132
- if (exprInterpreter(e.expr, code, id, moduleType)) {
133
- return false;
134
- }
135
- break;
136
- }
137
- }
138
- }
139
- return !hasInclude;
140
- }
141
- function exprInterpreter(expr, code, id, moduleType) {
142
- switch (expr.kind) {
143
- case 'and': {
144
- return expr.args.every((e) => exprInterpreter(e, code, id, moduleType));
145
- }
146
- case 'or': {
147
- return expr.args.some((e) => exprInterpreter(e, code, id, moduleType));
148
- }
149
- case 'not': {
150
- return !exprInterpreter(expr.expr, code, id, moduleType);
151
- }
152
- case 'id': {
153
- if (id === undefined) {
154
- throw new Error('`id` is required for `id` expression');
155
- }
156
- return typeof expr.pattern === 'string'
157
- ? id === expr.pattern
158
- : expr.pattern.test(id);
159
- }
160
- case 'moduleType': {
161
- if (moduleType === undefined) {
162
- throw new Error('`moduleType` is required for `moduleType` expression');
163
- }
164
- return moduleType === expr.pattern;
165
- }
166
- case 'code': {
167
- if (code === undefined) {
168
- throw new Error('`code` is required for `code` expression');
169
- }
170
- return typeof expr.pattern === 'string'
171
- ? code.includes(expr.pattern)
172
- : expr.pattern.test(code);
173
- }
174
- default: {
175
- throw new Error(`Expression kind ${expr.kind} is not expected.`);
176
- }
177
- }
178
- }
@@ -1,2 +0,0 @@
1
- export * from './composable-filters.js';
2
- export * from './simple-filters.js';
package/dist/cjs/index.js DELETED
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./composable-filters.js"), exports);
18
- __exportStar(require("./simple-filters.js"), exports);
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
@@ -1,28 +0,0 @@
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
- export declare function exactRegex(str: string, flags?: string): RegExp;
10
- /**
11
- * Constructs a RegExp that matches a value that has the specified prefix.
12
- *
13
- * This is useful for plugin hook filters.
14
- *
15
- * @param str the string to match.
16
- * @param flags flags for the RegExp.
17
- */
18
- export declare function prefixRegex(str: string, flags?: string): RegExp;
19
- type WidenString<T> = T extends string ? string : T;
20
- /**
21
- * Converts a id filter to match with an id with a query.
22
- *
23
- * @param input the id filters to convert.
24
- */
25
- export declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: T): WidenString<T>;
26
- export declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: readonly T[]): WidenString<T>[];
27
- export declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
28
- export {};
@@ -1,51 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.exactRegex = exactRegex;
4
- exports.prefixRegex = prefixRegex;
5
- exports.makeIdFiltersToMatchWithQuery = makeIdFiltersToMatchWithQuery;
6
- /**
7
- * Constructs a RegExp that matches the exact string specified.
8
- *
9
- * This is useful for plugin hook filters.
10
- *
11
- * @param str the string to match.
12
- * @param flags flags for the RegExp.
13
- */
14
- function exactRegex(str, flags) {
15
- return new RegExp(`^${escapeRegex(str)}$`, flags);
16
- }
17
- /**
18
- * Constructs a RegExp that matches a value that has the specified prefix.
19
- *
20
- * This is useful for plugin hook filters.
21
- *
22
- * @param str the string to match.
23
- * @param flags flags for the RegExp.
24
- */
25
- function prefixRegex(str, flags) {
26
- return new RegExp(`^${escapeRegex(str)}`, flags);
27
- }
28
- const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
29
- function escapeRegex(str) {
30
- return str.replace(escapeRegexRE, '\\$&');
31
- }
32
- function makeIdFiltersToMatchWithQuery(input) {
33
- if (!Array.isArray(input)) {
34
- return makeIdFilterToMatchWithQuery(
35
- // Array.isArray cannot narrow the type
36
- // https://github.com/microsoft/TypeScript/issues/17002
37
- input);
38
- }
39
- return input.map((i) => makeIdFilterToMatchWithQuery(i));
40
- }
41
- function makeIdFilterToMatchWithQuery(input) {
42
- if (typeof input === 'string') {
43
- return `${input}{?*,}`;
44
- }
45
- return makeRegexIdFilterToMatchWithQuery(input);
46
- }
47
- function makeRegexIdFilterToMatchWithQuery(input) {
48
- return new RegExp(
49
- // replace `$` with `(?:\?.*)?$` (ignore `\$`)
50
- input.source.replace(/(?<!\\)\$/g, '(?:\\?.*)?$'), input.flags);
51
- }
@@ -1,57 +0,0 @@
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 | Include | Exclude;
5
- export type TopLevelFilterExpression = Include | Exclude;
6
- export 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
- declare class Id {
22
- kind: 'id';
23
- pattern: StringOrRegExp;
24
- constructor(pattern: StringOrRegExp);
25
- }
26
- declare class ModuleType {
27
- kind: 'moduleType';
28
- pattern: PluginModuleType;
29
- constructor(pattern: PluginModuleType);
30
- }
31
- declare class Code {
32
- kind: 'code';
33
- pattern: StringOrRegExp;
34
- constructor(expr: StringOrRegExp);
35
- }
36
- declare class Include {
37
- kind: 'include';
38
- expr: FilterExpression;
39
- constructor(expr: FilterExpression);
40
- }
41
- declare class Exclude {
42
- kind: 'exclude';
43
- expr: FilterExpression;
44
- constructor(expr: FilterExpression);
45
- }
46
- export declare function and(...args: FilterExpression[]): And;
47
- export declare function or(...args: FilterExpression[]): Or;
48
- export declare function not(expr: FilterExpression): Not;
49
- export declare function id(pattern: StringOrRegExp): Id;
50
- export declare function moduleType(pattern: PluginModuleType): ModuleType;
51
- export declare function code(pattern: StringOrRegExp): Code;
52
- export declare function include(expr: FilterExpression): Include;
53
- export declare function exclude(expr: FilterExpression): Exclude;
54
- export declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
55
- export declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
56
- export declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType): boolean;
57
- export {};
@@ -1,163 +0,0 @@
1
- export class And {
2
- kind;
3
- args;
4
- constructor(...args) {
5
- if (args.length === 0) {
6
- throw new Error('`And` expects at least one operand');
7
- }
8
- this.args = args;
9
- this.kind = 'and';
10
- }
11
- }
12
- class Or {
13
- kind;
14
- args;
15
- constructor(...args) {
16
- if (args.length === 0) {
17
- throw new Error('`Or` expects at least one operand');
18
- }
19
- this.args = args;
20
- this.kind = 'or';
21
- }
22
- }
23
- class Not {
24
- kind;
25
- expr;
26
- constructor(expr) {
27
- this.expr = expr;
28
- this.kind = 'not';
29
- }
30
- }
31
- class Id {
32
- kind;
33
- pattern;
34
- constructor(pattern) {
35
- this.pattern = pattern;
36
- this.kind = 'id';
37
- }
38
- }
39
- class ModuleType {
40
- kind;
41
- pattern;
42
- constructor(pattern) {
43
- this.pattern = pattern;
44
- this.kind = 'moduleType';
45
- }
46
- }
47
- class Code {
48
- kind;
49
- pattern;
50
- constructor(expr) {
51
- this.pattern = expr;
52
- this.kind = 'code';
53
- }
54
- }
55
- class Include {
56
- kind;
57
- expr;
58
- constructor(expr) {
59
- this.expr = expr;
60
- this.kind = 'include';
61
- }
62
- }
63
- class Exclude {
64
- kind;
65
- expr;
66
- constructor(expr) {
67
- this.expr = expr;
68
- this.kind = 'exclude';
69
- }
70
- }
71
- export function and(...args) {
72
- return new And(...args);
73
- }
74
- export function or(...args) {
75
- return new Or(...args);
76
- }
77
- export function not(expr) {
78
- return new Not(expr);
79
- }
80
- export function id(pattern) {
81
- return new Id(pattern);
82
- }
83
- export function moduleType(pattern) {
84
- return new ModuleType(pattern);
85
- }
86
- export function code(pattern) {
87
- return new Code(pattern);
88
- }
89
- export function include(expr) {
90
- return new Include(expr);
91
- }
92
- export function exclude(expr) {
93
- return new Exclude(expr);
94
- }
95
- export function interpreter(exprs, code, id, moduleType) {
96
- let arr = [];
97
- if (Array.isArray(exprs)) {
98
- arr = exprs;
99
- }
100
- else {
101
- arr = [exprs];
102
- }
103
- return interpreterImpl(arr, code, id, moduleType);
104
- }
105
- export function interpreterImpl(expr, code, id, moduleType) {
106
- let hasInclude = false;
107
- for (const e of expr) {
108
- switch (e.kind) {
109
- case 'include': {
110
- hasInclude = true;
111
- if (exprInterpreter(e.expr, code, id, moduleType)) {
112
- return true;
113
- }
114
- break;
115
- }
116
- case 'exclude': {
117
- if (exprInterpreter(e.expr, code, id, moduleType)) {
118
- return false;
119
- }
120
- break;
121
- }
122
- }
123
- }
124
- return !hasInclude;
125
- }
126
- export function exprInterpreter(expr, code, id, moduleType) {
127
- switch (expr.kind) {
128
- case 'and': {
129
- return expr.args.every((e) => exprInterpreter(e, code, id, moduleType));
130
- }
131
- case 'or': {
132
- return expr.args.some((e) => exprInterpreter(e, code, id, moduleType));
133
- }
134
- case 'not': {
135
- return !exprInterpreter(expr.expr, code, id, moduleType);
136
- }
137
- case 'id': {
138
- if (id === undefined) {
139
- throw new Error('`id` is required for `id` expression');
140
- }
141
- return typeof expr.pattern === 'string'
142
- ? id === expr.pattern
143
- : expr.pattern.test(id);
144
- }
145
- case 'moduleType': {
146
- if (moduleType === undefined) {
147
- throw new Error('`moduleType` is required for `moduleType` expression');
148
- }
149
- return moduleType === expr.pattern;
150
- }
151
- case 'code': {
152
- if (code === undefined) {
153
- throw new Error('`code` is required for `code` expression');
154
- }
155
- return typeof expr.pattern === 'string'
156
- ? code.includes(expr.pattern)
157
- : expr.pattern.test(code);
158
- }
159
- default: {
160
- throw new Error(`Expression kind ${expr.kind} is not expected.`);
161
- }
162
- }
163
- }
@@ -1,2 +0,0 @@
1
- export * from './composable-filters.js';
2
- export * from './simple-filters.js';
package/dist/esm/index.js DELETED
@@ -1,2 +0,0 @@
1
- export * from './composable-filters.js';
2
- export * from './simple-filters.js';
@@ -1,28 +0,0 @@
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
- export declare function exactRegex(str: string, flags?: string): RegExp;
10
- /**
11
- * Constructs a RegExp that matches a value that has the specified prefix.
12
- *
13
- * This is useful for plugin hook filters.
14
- *
15
- * @param str the string to match.
16
- * @param flags flags for the RegExp.
17
- */
18
- export declare function prefixRegex(str: string, flags?: string): RegExp;
19
- type WidenString<T> = T extends string ? string : T;
20
- /**
21
- * Converts a id filter to match with an id with a query.
22
- *
23
- * @param input the id filters to convert.
24
- */
25
- export declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: T): WidenString<T>;
26
- export declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: readonly T[]): WidenString<T>[];
27
- export declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
28
- export {};
@@ -1,46 +0,0 @@
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
- export function exactRegex(str, flags) {
10
- return new RegExp(`^${escapeRegex(str)}$`, flags);
11
- }
12
- /**
13
- * Constructs a RegExp that matches a value that has the specified prefix.
14
- *
15
- * This is useful for plugin hook filters.
16
- *
17
- * @param str the string to match.
18
- * @param flags flags for the RegExp.
19
- */
20
- export function prefixRegex(str, flags) {
21
- return new RegExp(`^${escapeRegex(str)}`, flags);
22
- }
23
- const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
24
- function escapeRegex(str) {
25
- return str.replace(escapeRegexRE, '\\$&');
26
- }
27
- export function makeIdFiltersToMatchWithQuery(input) {
28
- if (!Array.isArray(input)) {
29
- return makeIdFilterToMatchWithQuery(
30
- // Array.isArray cannot narrow the type
31
- // https://github.com/microsoft/TypeScript/issues/17002
32
- input);
33
- }
34
- return input.map((i) => makeIdFilterToMatchWithQuery(i));
35
- }
36
- function makeIdFilterToMatchWithQuery(input) {
37
- if (typeof input === 'string') {
38
- return `${input}{?*,}`;
39
- }
40
- return makeRegexIdFilterToMatchWithQuery(input);
41
- }
42
- function makeRegexIdFilterToMatchWithQuery(input) {
43
- return new RegExp(
44
- // replace `$` with `(?:\?.*)?$` (ignore `\$`)
45
- input.source.replace(/(?<!\\)\$/g, '(?:\\?.*)?$'), input.flags);
46
- }