@rolldown/pluginutils 1.0.0-beta.8-commit.56abf23 → 1.0.0-beta.8-commit.f97bbba

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.
@@ -0,0 +1,57 @@
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 {};
@@ -0,0 +1,178 @@
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
+ }
@@ -0,0 +1,2 @@
1
+ export * from './composable-filters.js';
2
+ export * from './simple-filters.js';
@@ -0,0 +1,18 @@
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);
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,28 @@
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 {};
@@ -0,0 +1,51 @@
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
+ }
@@ -0,0 +1,57 @@
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 {};
@@ -0,0 +1,163 @@
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
+ }
@@ -0,0 +1,2 @@
1
+ export * from './composable-filters.js';
2
+ export * from './simple-filters.js';
@@ -0,0 +1,2 @@
1
+ export * from './composable-filters.js';
2
+ export * from './simple-filters.js';
@@ -0,0 +1,28 @@
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 {};
@@ -0,0 +1,46 @@
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rolldown/pluginutils",
3
- "version": "1.0.0-beta.8-commit.56abf23",
3
+ "version": "1.0.0-beta.8-commit.f97bbba",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {