@rolldown/pluginutils 1.0.0-beta.8-commit.d95f99e → 1.0.0-beta.8-commit.bf53a10

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 CHANGED
@@ -5,6 +5,13 @@ const postfixRE = /[?#].*$/;
5
5
  function cleanUrl(url) {
6
6
  return url.replace(postfixRE, "");
7
7
  }
8
+ function extractQueryWithoutFragment(url) {
9
+ const questionMarkIndex = url.indexOf("?");
10
+ if (questionMarkIndex === -1) return "";
11
+ const fragmentIndex = url.indexOf("#", questionMarkIndex);
12
+ if (fragmentIndex === -1) return url.substring(questionMarkIndex);
13
+ else return url.substring(questionMarkIndex, fragmentIndex);
14
+ }
8
15
 
9
16
  //#endregion
10
17
  //#region src/composable-filters.ts
@@ -60,6 +67,16 @@ var Code = class {
60
67
  this.kind = "code";
61
68
  }
62
69
  };
70
+ var Query = class {
71
+ kind;
72
+ key;
73
+ pattern;
74
+ constructor(key, pattern) {
75
+ this.pattern = pattern;
76
+ this.key = key;
77
+ this.kind = "query";
78
+ }
79
+ };
63
80
  var Include = class {
64
81
  kind;
65
82
  expr;
@@ -94,24 +111,41 @@ function moduleType(pattern) {
94
111
  function code(pattern) {
95
112
  return new Code(pattern);
96
113
  }
114
+ function query(key, pattern) {
115
+ return new Query(key, pattern);
116
+ }
97
117
  function include(expr) {
98
118
  return new Include(expr);
99
119
  }
100
120
  function exclude(expr) {
101
121
  return new Exclude(expr);
102
122
  }
123
+ /**
124
+ * convert a queryObject to FilterExpression like
125
+ * ```js
126
+ * and(query(k1, v1), query(k2, v2))
127
+ * ```
128
+ * @param queryFilterObject The query filter object needs to be matched.
129
+ * @returns a `And` FilterExpression
130
+ */
131
+ function queries(queryFilter) {
132
+ let arr = Object.entries(queryFilter).map(([key, value]) => {
133
+ return new Query(key, value);
134
+ });
135
+ return and(...arr);
136
+ }
103
137
  function interpreter(exprs, code$1, id$1, moduleType$1) {
104
138
  let arr = [];
105
139
  if (Array.isArray(exprs)) arr = exprs;
106
140
  else arr = [exprs];
107
141
  return interpreterImpl(arr, code$1, id$1, moduleType$1);
108
142
  }
109
- function interpreterImpl(expr, code$1, id$1, moduleType$1) {
143
+ function interpreterImpl(expr, code$1, id$1, moduleType$1, ctx = {}) {
110
144
  let hasInclude = false;
111
145
  for (const e of expr) switch (e.kind) {
112
146
  case "include": {
113
147
  hasInclude = true;
114
- if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return true;
148
+ if (exprInterpreter(e.expr, code$1, id$1, moduleType$1, ctx)) return true;
115
149
  break;
116
150
  }
117
151
  case "exclude": {
@@ -121,11 +155,11 @@ function interpreterImpl(expr, code$1, id$1, moduleType$1) {
121
155
  }
122
156
  return !hasInclude;
123
157
  }
124
- function exprInterpreter(expr, code$1, id$1, moduleType$1) {
158
+ function exprInterpreter(expr, code$1, id$1, moduleType$1, ctx = {}) {
125
159
  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);
160
+ case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx));
161
+ case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx));
162
+ case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1, ctx);
129
163
  case "id": {
130
164
  if (id$1 === void 0) throw new Error("`id` is required for `id` expression");
131
165
  if (expr.params.cleanUrl) id$1 = cleanUrl(id$1);
@@ -139,7 +173,19 @@ function exprInterpreter(expr, code$1, id$1, moduleType$1) {
139
173
  if (code$1 === void 0) throw new Error("`code` is required for `code` expression");
140
174
  return typeof expr.pattern === "string" ? code$1.includes(expr.pattern) : expr.pattern.test(code$1);
141
175
  }
142
- default: throw new Error(`Expression kind ${expr.kind} is not expected.`);
176
+ case "query": {
177
+ if (id$1 === void 0) throw new Error("`id` is required for `Query` expression");
178
+ if (!ctx.urlSearchParamsCache) {
179
+ let queryString = extractQueryWithoutFragment(id$1);
180
+ ctx.urlSearchParamsCache = new URLSearchParams(queryString);
181
+ }
182
+ let urlParams = ctx.urlSearchParamsCache;
183
+ if (typeof expr.pattern === "boolean") if (expr.pattern) return urlParams.has(expr.key);
184
+ else return !urlParams.has(expr.key);
185
+ else if (typeof expr.pattern === "string") return urlParams.get(expr.key) === expr.pattern;
186
+ else return expr.pattern.test(urlParams.get(expr.key) ?? "");
187
+ }
188
+ default: throw new Error(`Expression ${JSON.stringify(expr)} is not expected.`);
143
189
  }
144
190
  }
145
191
 
@@ -192,7 +238,6 @@ function makeRegexIdFilterToMatchWithQuery(input) {
192
238
  }
193
239
 
194
240
  //#endregion
195
- exports.And = And
196
241
  exports.and = and
197
242
  exports.code = code
198
243
  exports.exactRegex = exactRegex
@@ -206,4 +251,6 @@ exports.makeIdFiltersToMatchWithQuery = makeIdFiltersToMatchWithQuery
206
251
  exports.moduleType = moduleType
207
252
  exports.not = not
208
253
  exports.or = or
209
- exports.prefixRegex = prefixRegex
254
+ exports.prefixRegex = prefixRegex
255
+ exports.queries = queries
256
+ exports.query = query
package/dist/index.d.cts CHANGED
@@ -2,7 +2,7 @@
2
2
  type StringOrRegExp = string | RegExp;
3
3
  type PluginModuleType = "js" | "jsx" | "ts" | "tsx" | "json" | "text" | "base64" | "dataurl" | "binary" | "empty" | (string & {});
4
4
  type FilterExpressionKind = FilterExpression["kind"];
5
- type FilterExpression = And | Or | Not | Id | ModuleType | Code | Include | Exclude;
5
+ type FilterExpression = And | Or | Not | Id | ModuleType | Code | Query;
6
6
  type TopLevelFilterExpression = Include | Exclude;
7
7
  declare class And {
8
8
  kind: "and";
@@ -19,6 +19,9 @@ declare class Not {
19
19
  expr: FilterExpression;
20
20
  constructor(expr: FilterExpression);
21
21
  }
22
+ interface QueryFilterObject {
23
+ [key: string]: StringOrRegExp | boolean;
24
+ }
22
25
  interface IdParams {
23
26
  cleanUrl?: boolean;
24
27
  }
@@ -38,6 +41,12 @@ declare class Code {
38
41
  pattern: StringOrRegExp;
39
42
  constructor(expr: StringOrRegExp);
40
43
  }
44
+ declare class Query {
45
+ kind: "query";
46
+ key: string;
47
+ pattern: StringOrRegExp | boolean;
48
+ constructor(key: string, pattern: StringOrRegExp | boolean);
49
+ }
41
50
  declare class Include {
42
51
  kind: "include";
43
52
  expr: FilterExpression;
@@ -54,11 +63,24 @@ declare function not(expr: FilterExpression): Not;
54
63
  declare function id(pattern: StringOrRegExp, params?: IdParams): Id;
55
64
  declare function moduleType(pattern: PluginModuleType): ModuleType;
56
65
  declare function code(pattern: StringOrRegExp): Code;
66
+ declare function query(key: string, pattern: StringOrRegExp | boolean): Query;
57
67
  declare function include(expr: FilterExpression): Include;
58
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;
59
78
  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;
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;
62
84
 
63
85
  //#endregion
64
86
  //#region src/simple-filters.d.ts
@@ -91,4 +113,4 @@ declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input:
91
113
  declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
92
114
 
93
115
  //#endregion
94
- export { And, FilterExpression, FilterExpressionKind, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex };
116
+ export { FilterExpression, FilterExpressionKind, QueryFilterObject, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, queries, query };
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  type StringOrRegExp = string | RegExp;
3
3
  type PluginModuleType = "js" | "jsx" | "ts" | "tsx" | "json" | "text" | "base64" | "dataurl" | "binary" | "empty" | (string & {});
4
4
  type FilterExpressionKind = FilterExpression["kind"];
5
- type FilterExpression = And | Or | Not | Id | ModuleType | Code | Include | Exclude;
5
+ type FilterExpression = And | Or | Not | Id | ModuleType | Code | Query;
6
6
  type TopLevelFilterExpression = Include | Exclude;
7
7
  declare class And {
8
8
  kind: "and";
@@ -19,6 +19,9 @@ declare class Not {
19
19
  expr: FilterExpression;
20
20
  constructor(expr: FilterExpression);
21
21
  }
22
+ interface QueryFilterObject {
23
+ [key: string]: StringOrRegExp | boolean;
24
+ }
22
25
  interface IdParams {
23
26
  cleanUrl?: boolean;
24
27
  }
@@ -38,6 +41,12 @@ declare class Code {
38
41
  pattern: StringOrRegExp;
39
42
  constructor(expr: StringOrRegExp);
40
43
  }
44
+ declare class Query {
45
+ kind: "query";
46
+ key: string;
47
+ pattern: StringOrRegExp | boolean;
48
+ constructor(key: string, pattern: StringOrRegExp | boolean);
49
+ }
41
50
  declare class Include {
42
51
  kind: "include";
43
52
  expr: FilterExpression;
@@ -54,11 +63,24 @@ declare function not(expr: FilterExpression): Not;
54
63
  declare function id(pattern: StringOrRegExp, params?: IdParams): Id;
55
64
  declare function moduleType(pattern: PluginModuleType): ModuleType;
56
65
  declare function code(pattern: StringOrRegExp): Code;
66
+ declare function query(key: string, pattern: StringOrRegExp | boolean): Query;
57
67
  declare function include(expr: FilterExpression): Include;
58
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;
59
78
  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;
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;
62
84
 
63
85
  //#endregion
64
86
  //#region src/simple-filters.d.ts
@@ -91,4 +113,4 @@ declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input:
91
113
  declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
92
114
 
93
115
  //#endregion
94
- export { And, FilterExpression, FilterExpressionKind, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex };
116
+ export { FilterExpression, FilterExpressionKind, QueryFilterObject, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, queries, query };
package/dist/index.js CHANGED
@@ -3,6 +3,13 @@ const postfixRE = /[?#].*$/;
3
3
  function cleanUrl(url) {
4
4
  return url.replace(postfixRE, "");
5
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
+ }
6
13
 
7
14
  //#endregion
8
15
  //#region src/composable-filters.ts
@@ -58,6 +65,16 @@ var Code = class {
58
65
  this.kind = "code";
59
66
  }
60
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
+ };
61
78
  var Include = class {
62
79
  kind;
63
80
  expr;
@@ -92,24 +109,41 @@ function moduleType(pattern) {
92
109
  function code(pattern) {
93
110
  return new Code(pattern);
94
111
  }
112
+ function query(key, pattern) {
113
+ return new Query(key, pattern);
114
+ }
95
115
  function include(expr) {
96
116
  return new Include(expr);
97
117
  }
98
118
  function exclude(expr) {
99
119
  return new Exclude(expr);
100
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
+ let arr = Object.entries(queryFilter).map(([key, value]) => {
131
+ return new Query(key, value);
132
+ });
133
+ return and(...arr);
134
+ }
101
135
  function interpreter(exprs, code$1, id$1, moduleType$1) {
102
136
  let arr = [];
103
137
  if (Array.isArray(exprs)) arr = exprs;
104
138
  else arr = [exprs];
105
139
  return interpreterImpl(arr, code$1, id$1, moduleType$1);
106
140
  }
107
- function interpreterImpl(expr, code$1, id$1, moduleType$1) {
141
+ function interpreterImpl(expr, code$1, id$1, moduleType$1, ctx = {}) {
108
142
  let hasInclude = false;
109
143
  for (const e of expr) switch (e.kind) {
110
144
  case "include": {
111
145
  hasInclude = true;
112
- if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return true;
146
+ if (exprInterpreter(e.expr, code$1, id$1, moduleType$1, ctx)) return true;
113
147
  break;
114
148
  }
115
149
  case "exclude": {
@@ -119,11 +153,11 @@ function interpreterImpl(expr, code$1, id$1, moduleType$1) {
119
153
  }
120
154
  return !hasInclude;
121
155
  }
122
- function exprInterpreter(expr, code$1, id$1, moduleType$1) {
156
+ function exprInterpreter(expr, code$1, id$1, moduleType$1, ctx = {}) {
123
157
  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);
158
+ case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx));
159
+ case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx));
160
+ case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1, ctx);
127
161
  case "id": {
128
162
  if (id$1 === void 0) throw new Error("`id` is required for `id` expression");
129
163
  if (expr.params.cleanUrl) id$1 = cleanUrl(id$1);
@@ -137,7 +171,19 @@ function exprInterpreter(expr, code$1, id$1, moduleType$1) {
137
171
  if (code$1 === void 0) throw new Error("`code` is required for `code` expression");
138
172
  return typeof expr.pattern === "string" ? code$1.includes(expr.pattern) : expr.pattern.test(code$1);
139
173
  }
140
- default: throw new Error(`Expression kind ${expr.kind} is not expected.`);
174
+ case "query": {
175
+ if (id$1 === void 0) throw new Error("`id` is required for `Query` expression");
176
+ if (!ctx.urlSearchParamsCache) {
177
+ let queryString = extractQueryWithoutFragment(id$1);
178
+ ctx.urlSearchParamsCache = new URLSearchParams(queryString);
179
+ }
180
+ let urlParams = ctx.urlSearchParamsCache;
181
+ if (typeof expr.pattern === "boolean") if (expr.pattern) return urlParams.has(expr.key);
182
+ else return !urlParams.has(expr.key);
183
+ else if (typeof expr.pattern === "string") return urlParams.get(expr.key) === expr.pattern;
184
+ else return expr.pattern.test(urlParams.get(expr.key) ?? "");
185
+ }
186
+ default: throw new Error(`Expression ${JSON.stringify(expr)} is not expected.`);
141
187
  }
142
188
  }
143
189
 
@@ -190,4 +236,4 @@ function makeRegexIdFilterToMatchWithQuery(input) {
190
236
  }
191
237
 
192
238
  //#endregion
193
- export { And, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex };
239
+ export { and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, queries, query };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rolldown/pluginutils",
3
- "version": "1.0.0-beta.8-commit.d95f99e",
3
+ "version": "1.0.0-beta.8-commit.bf53a10",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {