@rolldown/pluginutils 1.0.0-beta.8-commit.66f4623 → 1.0.0-beta.8-commit.985af6d

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
@@ -60,6 +60,16 @@ var Code = class {
60
60
  this.kind = "code";
61
61
  }
62
62
  };
63
+ var Query = class {
64
+ kind;
65
+ key;
66
+ pattern;
67
+ constructor(key, pattern) {
68
+ this.pattern = pattern;
69
+ this.key = key;
70
+ this.kind = "query";
71
+ }
72
+ };
63
73
  var Include = class {
64
74
  kind;
65
75
  expr;
@@ -94,24 +104,46 @@ function moduleType(pattern) {
94
104
  function code(pattern) {
95
105
  return new Code(pattern);
96
106
  }
107
+ function query(key, pattern) {
108
+ return new Query(key, pattern);
109
+ }
97
110
  function include(expr) {
98
111
  return new Include(expr);
99
112
  }
100
113
  function exclude(expr) {
101
114
  return new Exclude(expr);
102
115
  }
116
+ /**
117
+ * convert a queryObject to FilterExpression like
118
+ * ```js
119
+ * and(query(k1, v1), query(k2, v2))
120
+ * ```
121
+ * @param queryFilterObject The query filter object needs to be matched.
122
+ * @returns a `And` FilterExpression
123
+ *
124
+ * There are three kinds of conditions are supported:
125
+ * 1. `boolean`: if the value is `true`, the key must exist and be truthy. if the value is `false`, the key must not exist or be falsy.
126
+ * 2. `string`: the key must exist and be equal to the value.
127
+ * 3. `RegExp`: the key must exist and match the value.
128
+ */
129
+ function queryObjectToFilterExpr(queryFilter) {
130
+ let arr = Object.entries(queryFilter).map(([key, value]) => {
131
+ return new Query(key, value);
132
+ });
133
+ return and(...arr);
134
+ }
103
135
  function interpreter(exprs, code$1, id$1, moduleType$1) {
104
136
  let arr = [];
105
137
  if (Array.isArray(exprs)) arr = exprs;
106
138
  else arr = [exprs];
107
139
  return interpreterImpl(arr, code$1, id$1, moduleType$1);
108
140
  }
109
- function interpreterImpl(expr, code$1, id$1, moduleType$1) {
141
+ function interpreterImpl(expr, code$1, id$1, moduleType$1, ctx = {}) {
110
142
  let hasInclude = false;
111
143
  for (const e of expr) switch (e.kind) {
112
144
  case "include": {
113
145
  hasInclude = true;
114
- 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;
115
147
  break;
116
148
  }
117
149
  case "exclude": {
@@ -121,11 +153,11 @@ function interpreterImpl(expr, code$1, id$1, moduleType$1) {
121
153
  }
122
154
  return !hasInclude;
123
155
  }
124
- function exprInterpreter(expr, code$1, id$1, moduleType$1) {
156
+ function exprInterpreter(expr, code$1, id$1, moduleType$1, ctx = {}) {
125
157
  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);
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);
129
161
  case "id": {
130
162
  if (id$1 === void 0) throw new Error("`id` is required for `id` expression");
131
163
  if (expr.params.cleanUrl) id$1 = cleanUrl(id$1);
@@ -139,7 +171,19 @@ function exprInterpreter(expr, code$1, id$1, moduleType$1) {
139
171
  if (code$1 === void 0) throw new Error("`code` is required for `code` expression");
140
172
  return typeof expr.pattern === "string" ? code$1.includes(expr.pattern) : expr.pattern.test(code$1);
141
173
  }
142
- 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 = ""] = id$1.split("?", 2);
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 kind ${expr} is not expected.`);
143
187
  }
144
188
  }
145
189
 
@@ -192,7 +236,6 @@ function makeRegexIdFilterToMatchWithQuery(input) {
192
236
  }
193
237
 
194
238
  //#endregion
195
- exports.And = And
196
239
  exports.and = and
197
240
  exports.code = code
198
241
  exports.exactRegex = exactRegex
@@ -206,4 +249,6 @@ exports.makeIdFiltersToMatchWithQuery = makeIdFiltersToMatchWithQuery
206
249
  exports.moduleType = moduleType
207
250
  exports.not = not
208
251
  exports.or = or
209
- exports.prefixRegex = prefixRegex
252
+ exports.prefixRegex = prefixRegex
253
+ exports.query = query
254
+ exports.queryObjectToFilterExpr = queryObjectToFilterExpr
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,29 @@ 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
+ * There are three kinds of conditions are supported:
78
+ * 1. `boolean`: if the value is `true`, the key must exist and be truthy. if the value is `false`, the key must not exist or be falsy.
79
+ * 2. `string`: the key must exist and be equal to the value.
80
+ * 3. `RegExp`: the key must exist and match the value.
81
+ */
82
+ declare function queryObjectToFilterExpr(queryFilter: QueryFilterObject): And;
59
83
  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;
84
+ interface InterpreterCtx {
85
+ urlSearchParamsCache?: URLSearchParams;
86
+ }
87
+ declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean;
88
+ declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean;
62
89
 
63
90
  //#endregion
64
91
  //#region src/simple-filters.d.ts
@@ -91,4 +118,4 @@ declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input:
91
118
  declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
92
119
 
93
120
  //#endregion
94
- export { And, FilterExpression, FilterExpressionKind, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex };
121
+ export { FilterExpression, FilterExpressionKind, QueryFilterObject, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, query, queryObjectToFilterExpr };
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,29 @@ 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
+ * There are three kinds of conditions are supported:
78
+ * 1. `boolean`: if the value is `true`, the key must exist and be truthy. if the value is `false`, the key must not exist or be falsy.
79
+ * 2. `string`: the key must exist and be equal to the value.
80
+ * 3. `RegExp`: the key must exist and match the value.
81
+ */
82
+ declare function queryObjectToFilterExpr(queryFilter: QueryFilterObject): And;
59
83
  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;
84
+ interface InterpreterCtx {
85
+ urlSearchParamsCache?: URLSearchParams;
86
+ }
87
+ declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean;
88
+ declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean;
62
89
 
63
90
  //#endregion
64
91
  //#region src/simple-filters.d.ts
@@ -91,4 +118,4 @@ declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input:
91
118
  declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
92
119
 
93
120
  //#endregion
94
- export { And, FilterExpression, FilterExpressionKind, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex };
121
+ export { FilterExpression, FilterExpressionKind, QueryFilterObject, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, query, queryObjectToFilterExpr };
package/dist/index.js CHANGED
@@ -58,6 +58,16 @@ var Code = class {
58
58
  this.kind = "code";
59
59
  }
60
60
  };
61
+ var Query = class {
62
+ kind;
63
+ key;
64
+ pattern;
65
+ constructor(key, pattern) {
66
+ this.pattern = pattern;
67
+ this.key = key;
68
+ this.kind = "query";
69
+ }
70
+ };
61
71
  var Include = class {
62
72
  kind;
63
73
  expr;
@@ -92,24 +102,46 @@ function moduleType(pattern) {
92
102
  function code(pattern) {
93
103
  return new Code(pattern);
94
104
  }
105
+ function query(key, pattern) {
106
+ return new Query(key, pattern);
107
+ }
95
108
  function include(expr) {
96
109
  return new Include(expr);
97
110
  }
98
111
  function exclude(expr) {
99
112
  return new Exclude(expr);
100
113
  }
114
+ /**
115
+ * convert a queryObject to FilterExpression like
116
+ * ```js
117
+ * and(query(k1, v1), query(k2, v2))
118
+ * ```
119
+ * @param queryFilterObject The query filter object needs to be matched.
120
+ * @returns a `And` FilterExpression
121
+ *
122
+ * There are three kinds of conditions are supported:
123
+ * 1. `boolean`: if the value is `true`, the key must exist and be truthy. if the value is `false`, the key must not exist or be falsy.
124
+ * 2. `string`: the key must exist and be equal to the value.
125
+ * 3. `RegExp`: the key must exist and match the value.
126
+ */
127
+ function queryObjectToFilterExpr(queryFilter) {
128
+ let arr = Object.entries(queryFilter).map(([key, value]) => {
129
+ return new Query(key, value);
130
+ });
131
+ return and(...arr);
132
+ }
101
133
  function interpreter(exprs, code$1, id$1, moduleType$1) {
102
134
  let arr = [];
103
135
  if (Array.isArray(exprs)) arr = exprs;
104
136
  else arr = [exprs];
105
137
  return interpreterImpl(arr, code$1, id$1, moduleType$1);
106
138
  }
107
- function interpreterImpl(expr, code$1, id$1, moduleType$1) {
139
+ function interpreterImpl(expr, code$1, id$1, moduleType$1, ctx = {}) {
108
140
  let hasInclude = false;
109
141
  for (const e of expr) switch (e.kind) {
110
142
  case "include": {
111
143
  hasInclude = true;
112
- if (exprInterpreter(e.expr, code$1, id$1, moduleType$1)) return true;
144
+ if (exprInterpreter(e.expr, code$1, id$1, moduleType$1, ctx)) return true;
113
145
  break;
114
146
  }
115
147
  case "exclude": {
@@ -119,11 +151,11 @@ function interpreterImpl(expr, code$1, id$1, moduleType$1) {
119
151
  }
120
152
  return !hasInclude;
121
153
  }
122
- function exprInterpreter(expr, code$1, id$1, moduleType$1) {
154
+ function exprInterpreter(expr, code$1, id$1, moduleType$1, ctx = {}) {
123
155
  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);
156
+ case "and": return expr.args.every((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx));
157
+ case "or": return expr.args.some((e) => exprInterpreter(e, code$1, id$1, moduleType$1, ctx));
158
+ case "not": return !exprInterpreter(expr.expr, code$1, id$1, moduleType$1, ctx);
127
159
  case "id": {
128
160
  if (id$1 === void 0) throw new Error("`id` is required for `id` expression");
129
161
  if (expr.params.cleanUrl) id$1 = cleanUrl(id$1);
@@ -137,7 +169,19 @@ function exprInterpreter(expr, code$1, id$1, moduleType$1) {
137
169
  if (code$1 === void 0) throw new Error("`code` is required for `code` expression");
138
170
  return typeof expr.pattern === "string" ? code$1.includes(expr.pattern) : expr.pattern.test(code$1);
139
171
  }
140
- default: throw new Error(`Expression kind ${expr.kind} is not expected.`);
172
+ case "query": {
173
+ if (id$1 === void 0) throw new Error("`id` is required for `Query` expression");
174
+ if (!ctx.urlSearchParamsCache) {
175
+ let [_, queryString = ""] = id$1.split("?", 2);
176
+ ctx.urlSearchParamsCache = new URLSearchParams(queryString);
177
+ }
178
+ let urlParams = ctx.urlSearchParamsCache;
179
+ if (typeof expr.pattern === "boolean") if (expr.pattern) return urlParams.has(expr.key);
180
+ else return !urlParams.has(expr.key);
181
+ else if (typeof expr.pattern === "string") return urlParams.get(expr.key) === expr.pattern;
182
+ else return expr.pattern.test(urlParams.get(expr.key) ?? "");
183
+ }
184
+ default: throw new Error(`Expression kind ${expr} is not expected.`);
141
185
  }
142
186
  }
143
187
 
@@ -190,4 +234,4 @@ function makeRegexIdFilterToMatchWithQuery(input) {
190
234
  }
191
235
 
192
236
  //#endregion
193
- export { And, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex };
237
+ export { and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, query, queryObjectToFilterExpr };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rolldown/pluginutils",
3
- "version": "1.0.0-beta.8-commit.66f4623",
3
+ "version": "1.0.0-beta.8-commit.985af6d",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {