@rolldown/pluginutils 1.0.0-beta.55 → 1.0.0-beta.57

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.
@@ -1,7 +1,7 @@
1
1
  type StringOrRegExp = string | RegExp;
2
2
  type PluginModuleType = 'js' | 'jsx' | 'ts' | 'tsx' | 'json' | 'text' | 'base64' | 'dataurl' | 'binary' | 'empty' | (string & {});
3
3
  export type FilterExpressionKind = FilterExpression['kind'];
4
- export type FilterExpression = And | Or | Not | Id | ModuleType | Code | Query;
4
+ export type FilterExpression = And | Or | Not | Id | ImporterId | ModuleType | Code | Query;
5
5
  export type TopLevelFilterExpression = Include | Exclude;
6
6
  declare class And {
7
7
  kind: 'and';
@@ -30,6 +30,12 @@ declare class Id {
30
30
  params: IdParams;
31
31
  constructor(pattern: StringOrRegExp, params?: IdParams);
32
32
  }
33
+ declare class ImporterId {
34
+ kind: 'importerId';
35
+ pattern: StringOrRegExp;
36
+ params: IdParams;
37
+ constructor(pattern: StringOrRegExp, params?: IdParams);
38
+ }
33
39
  declare class ModuleType {
34
40
  kind: 'moduleType';
35
41
  pattern: PluginModuleType;
@@ -60,6 +66,7 @@ export declare function and(...args: FilterExpression[]): And;
60
66
  export declare function or(...args: FilterExpression[]): Or;
61
67
  export declare function not(expr: FilterExpression): Not;
62
68
  export declare function id(pattern: StringOrRegExp, params?: IdParams): Id;
69
+ export declare function importerId(pattern: StringOrRegExp, params?: IdParams): ImporterId;
63
70
  export declare function moduleType(pattern: PluginModuleType): ModuleType;
64
71
  export declare function code(pattern: StringOrRegExp): Code;
65
72
  export declare function query(key: string, pattern: StringOrRegExp | boolean): Query;
@@ -74,10 +81,10 @@ export declare function exclude(expr: FilterExpression): Exclude;
74
81
  * @returns a `And` FilterExpression
75
82
  */
76
83
  export declare function queries(queryFilter: QueryFilterObject): And;
77
- export declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean;
84
+ export declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, importerId?: string): boolean;
78
85
  interface InterpreterCtx {
79
86
  urlSearchParamsCache?: URLSearchParams;
80
87
  }
81
- export declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean;
82
- export declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean;
88
+ export declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, importerId?: string, ctx?: InterpreterCtx): boolean;
89
+ export declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType, importerId?: string, ctx?: InterpreterCtx): boolean;
83
90
  export {};
@@ -1,4 +1,4 @@
1
- import { cleanUrl, extractQueryWithoutFragment } from './utils.js';
1
+ import { cleanUrl, extractQueryWithoutFragment } from "../utils.js";
2
2
  class And {
3
3
  kind;
4
4
  args;
@@ -41,6 +41,18 @@ class Id {
41
41
  };
42
42
  }
43
43
  }
44
+ class ImporterId {
45
+ kind;
46
+ pattern;
47
+ params;
48
+ constructor(pattern, params) {
49
+ this.pattern = pattern;
50
+ this.kind = 'importerId';
51
+ this.params = params ?? {
52
+ cleanUrl: false,
53
+ };
54
+ }
55
+ }
44
56
  class ModuleType {
45
57
  kind;
46
58
  pattern;
@@ -95,6 +107,9 @@ export function not(expr) {
95
107
  export function id(pattern, params) {
96
108
  return new Id(pattern, params);
97
109
  }
110
+ export function importerId(pattern, params) {
111
+ return new ImporterId(pattern, params);
112
+ }
98
113
  export function moduleType(pattern) {
99
114
  return new ModuleType(pattern);
100
115
  }
@@ -130,7 +145,7 @@ export function queries(queryFilter) {
130
145
  });
131
146
  return and(...arr);
132
147
  }
133
- export function interpreter(exprs, code, id, moduleType) {
148
+ export function interpreter(exprs, code, id, moduleType, importerId) {
134
149
  let arr = [];
135
150
  if (Array.isArray(exprs)) {
136
151
  arr = exprs;
@@ -138,21 +153,21 @@ export function interpreter(exprs, code, id, moduleType) {
138
153
  else {
139
154
  arr = [exprs];
140
155
  }
141
- return interpreterImpl(arr, code, id, moduleType);
156
+ return interpreterImpl(arr, code, id, moduleType, importerId);
142
157
  }
143
- export function interpreterImpl(expr, code, id, moduleType, ctx = {}) {
158
+ export function interpreterImpl(expr, code, id, moduleType, importerId, ctx = {}) {
144
159
  let hasInclude = false;
145
160
  for (const e of expr) {
146
161
  switch (e.kind) {
147
162
  case 'include': {
148
163
  hasInclude = true;
149
- if (exprInterpreter(e.expr, code, id, moduleType, ctx)) {
164
+ if (exprInterpreter(e.expr, code, id, moduleType, importerId, ctx)) {
150
165
  return true;
151
166
  }
152
167
  break;
153
168
  }
154
169
  case 'exclude': {
155
- if (exprInterpreter(e.expr, code, id, moduleType)) {
170
+ if (exprInterpreter(e.expr, code, id, moduleType, importerId, ctx)) {
156
171
  return false;
157
172
  }
158
173
  break;
@@ -161,27 +176,40 @@ export function interpreterImpl(expr, code, id, moduleType, ctx = {}) {
161
176
  }
162
177
  return !hasInclude;
163
178
  }
164
- export function exprInterpreter(expr, code, id, moduleType, ctx = {}) {
179
+ export function exprInterpreter(expr, code, id, moduleType, importerId, ctx = {}) {
165
180
  switch (expr.kind) {
166
181
  case 'and': {
167
- return expr.args.every((e) => exprInterpreter(e, code, id, moduleType, ctx));
182
+ return expr.args.every((e) => exprInterpreter(e, code, id, moduleType, importerId, ctx));
168
183
  }
169
184
  case 'or': {
170
- return expr.args.some((e) => exprInterpreter(e, code, id, moduleType, ctx));
185
+ return expr.args.some((e) => exprInterpreter(e, code, id, moduleType, importerId, ctx));
171
186
  }
172
187
  case 'not': {
173
- return !exprInterpreter(expr.expr, code, id, moduleType, ctx);
188
+ return !exprInterpreter(expr.expr, code, id, moduleType, importerId, ctx);
174
189
  }
175
190
  case 'id': {
176
191
  if (id === undefined) {
177
192
  throw new Error('`id` is required for `id` expression');
178
193
  }
194
+ let idToMatch = id;
195
+ if (expr.params.cleanUrl) {
196
+ idToMatch = cleanUrl(idToMatch);
197
+ }
198
+ return typeof expr.pattern === 'string'
199
+ ? idToMatch === expr.pattern
200
+ : expr.pattern.test(idToMatch);
201
+ }
202
+ case 'importerId': {
203
+ if (importerId === undefined) {
204
+ return false; // Entry files have no importer, so no match
205
+ }
206
+ let importerIdToMatch = importerId;
179
207
  if (expr.params.cleanUrl) {
180
- id = cleanUrl(id);
208
+ importerIdToMatch = cleanUrl(importerIdToMatch);
181
209
  }
182
210
  return typeof expr.pattern === 'string'
183
- ? id === expr.pattern
184
- : expr.pattern.test(id);
211
+ ? importerIdToMatch === expr.pattern
212
+ : expr.pattern.test(importerIdToMatch);
185
213
  }
186
214
  case 'moduleType': {
187
215
  if (moduleType === undefined) {
@@ -0,0 +1,3 @@
1
+ export * from './composable-filters.ts';
2
+ export * from './filter-vite-plugins.ts';
3
+ export * from './simple-filters.ts';
@@ -0,0 +1,3 @@
1
+ export * from "./composable-filters.js";
2
+ export * from "./filter-vite-plugins.js";
3
+ export * from "./simple-filters.js";
package/dist/index.d.ts CHANGED
@@ -1,3 +1 @@
1
- export * from './composable-filters.js';
2
- export * from './filter-vite-plugins.js';
3
- export * from './simple-filters.js';
1
+ export * from './filter/index.ts';
package/dist/index.js CHANGED
@@ -1,3 +1 @@
1
- export * from './composable-filters.js';
2
- export * from './filter-vite-plugins.js';
3
- export * from './simple-filters.js';
1
+ export * from "./filter/index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rolldown/pluginutils",
3
- "version": "1.0.0-beta.55",
3
+ "version": "1.0.0-beta.57",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {
@@ -16,6 +16,7 @@
16
16
  "types": "./dist/index.d.ts",
17
17
  "exports": {
18
18
  ".": "./dist/index.js",
19
+ "./filter": "./dist/filter/index.js",
19
20
  "./package.json": "./package.json"
20
21
  },
21
22
  "files": [