市场夺宝奇兵
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.1 KiB

  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 | Query;
  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 QueryFilterObject {
  23. [key: string]: StringOrRegExp | boolean;
  24. }
  25. interface IdParams {
  26. cleanUrl?: boolean;
  27. }
  28. declare class Id {
  29. kind: "id";
  30. pattern: StringOrRegExp;
  31. params: IdParams;
  32. constructor(pattern: StringOrRegExp, params?: IdParams);
  33. }
  34. declare class ModuleType {
  35. kind: "moduleType";
  36. pattern: PluginModuleType;
  37. constructor(pattern: PluginModuleType);
  38. }
  39. declare class Code {
  40. kind: "code";
  41. pattern: StringOrRegExp;
  42. constructor(expr: StringOrRegExp);
  43. }
  44. declare class Query {
  45. kind: "query";
  46. key: string;
  47. pattern: StringOrRegExp | boolean;
  48. constructor(key: string, pattern: StringOrRegExp | boolean);
  49. }
  50. declare class Include {
  51. kind: "include";
  52. expr: FilterExpression;
  53. constructor(expr: FilterExpression);
  54. }
  55. declare class Exclude {
  56. kind: "exclude";
  57. expr: FilterExpression;
  58. constructor(expr: FilterExpression);
  59. }
  60. declare function and(...args: FilterExpression[]): And;
  61. declare function or(...args: FilterExpression[]): Or;
  62. declare function not(expr: FilterExpression): Not;
  63. declare function id(pattern: StringOrRegExp, params?: IdParams): Id;
  64. declare function moduleType(pattern: PluginModuleType): ModuleType;
  65. declare function code(pattern: StringOrRegExp): Code;
  66. declare function query(key: string, pattern: StringOrRegExp | boolean): Query;
  67. declare function include(expr: FilterExpression): Include;
  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;
  78. declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], 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;
  84. //#endregion
  85. //#region src/simple-filters.d.ts
  86. /**
  87. * Constructs a RegExp that matches the exact string specified.
  88. *
  89. * This is useful for plugin hook filters.
  90. *
  91. * @param str the string to match.
  92. * @param flags flags for the RegExp.
  93. *
  94. * @example
  95. * ```ts
  96. * import { exactRegex } from '@rolldown/pluginutils';
  97. * const plugin = {
  98. * name: 'plugin',
  99. * resolveId: {
  100. * filter: { id: exactRegex('foo') },
  101. * handler(id) {} // will only be called for `foo`
  102. * }
  103. * }
  104. * ```
  105. */
  106. declare function exactRegex(str: string, flags?: string): RegExp;
  107. /**
  108. * Constructs a RegExp that matches a value that has the specified prefix.
  109. *
  110. * This is useful for plugin hook filters.
  111. *
  112. * @param str the string to match.
  113. * @param flags flags for the RegExp.
  114. *
  115. * @example
  116. * ```ts
  117. * import { prefixRegex } from '@rolldown/pluginutils';
  118. * const plugin = {
  119. * name: 'plugin',
  120. * resolveId: {
  121. * filter: { id: prefixRegex('foo') },
  122. * handler(id) {} // will only be called for IDs starting with `foo`
  123. * }
  124. * }
  125. * ```
  126. */
  127. declare function prefixRegex(str: string, flags?: string): RegExp;
  128. type WidenString<T> = T extends string ? string : T;
  129. /**
  130. * Converts a id filter to match with an id with a query.
  131. *
  132. * @param input the id filters to convert.
  133. *
  134. * @example
  135. * ```ts
  136. * import { makeIdFiltersToMatchWithQuery } from '@rolldown/pluginutils';
  137. * const plugin = {
  138. * name: 'plugin',
  139. * transform: {
  140. * filter: { id: makeIdFiltersToMatchWithQuery(['**' + '/*.js', /\.ts$/]) },
  141. * // The handler will be called for IDs like:
  142. * // - foo.js
  143. * // - foo.js?foo
  144. * // - foo.txt?foo.js
  145. * // - foo.ts
  146. * // - foo.ts?foo
  147. * // - foo.txt?foo.ts
  148. * handler(code, id) {}
  149. * }
  150. * }
  151. * ```
  152. */
  153. declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: T): WidenString<T>;
  154. declare function makeIdFiltersToMatchWithQuery<T extends string | RegExp>(input: readonly T[]): WidenString<T>[];
  155. declare function makeIdFiltersToMatchWithQuery(input: string | RegExp | readonly (string | RegExp)[]): string | RegExp | (string | RegExp)[];
  156. //#endregion
  157. export { FilterExpression, FilterExpressionKind, QueryFilterObject, TopLevelFilterExpression, and, code, exactRegex, exclude, exprInterpreter, id, include, interpreter, interpreterImpl, makeIdFiltersToMatchWithQuery, moduleType, not, or, prefixRegex, queries, query };