@rolldown/pluginutils
Advanced tools
| type StringOrRegExp = string | RegExp; | ||
| type PluginModuleType = 'js' | 'jsx' | 'ts' | 'tsx' | 'json' | 'text' | 'base64' | 'dataurl' | 'binary' | 'empty' | (string & {}); | ||
| export type FilterExpressionKind = FilterExpression['kind']; | ||
| export type FilterExpression = And | Or | Not | Id | ModuleType | Code | Query; | ||
| export type FilterExpression = And | Or | Not | Id | ImporterId | ModuleType | Code | Query; | ||
| export type TopLevelFilterExpression = Include | Exclude; | ||
@@ -33,2 +33,8 @@ declare class And { | ||
| } | ||
| declare class ImporterId { | ||
| kind: 'importerId'; | ||
| pattern: StringOrRegExp; | ||
| params: IdParams; | ||
| constructor(pattern: StringOrRegExp, params?: IdParams); | ||
| } | ||
| declare class ModuleType { | ||
@@ -64,2 +70,3 @@ kind: 'moduleType'; | ||
| export declare function id(pattern: StringOrRegExp, params?: IdParams): Id; | ||
| export declare function importerId(pattern: StringOrRegExp, params?: IdParams): ImporterId; | ||
| export declare function moduleType(pattern: PluginModuleType): ModuleType; | ||
@@ -79,8 +86,8 @@ export declare function code(pattern: StringOrRegExp): Code; | ||
| export declare function queries(queryFilter: QueryFilterObject): And; | ||
| export declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType): boolean; | ||
| export declare function interpreter(exprs: TopLevelFilterExpression | TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, importerId?: string): boolean; | ||
| interface InterpreterCtx { | ||
| urlSearchParamsCache?: URLSearchParams; | ||
| } | ||
| export declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean; | ||
| export declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType, ctx?: InterpreterCtx): boolean; | ||
| export declare function interpreterImpl(expr: TopLevelFilterExpression[], code?: string, id?: string, moduleType?: PluginModuleType, importerId?: string, ctx?: InterpreterCtx): boolean; | ||
| export declare function exprInterpreter(expr: FilterExpression, code?: string, id?: string, moduleType?: PluginModuleType, importerId?: string, ctx?: InterpreterCtx): boolean; | ||
| export {}; |
@@ -44,2 +44,14 @@ import { cleanUrl, extractQueryWithoutFragment } from './utils.js'; | ||
| } | ||
| class ImporterId { | ||
| kind; | ||
| pattern; | ||
| params; | ||
| constructor(pattern, params) { | ||
| this.pattern = pattern; | ||
| this.kind = 'importerId'; | ||
| this.params = params ?? { | ||
| cleanUrl: false, | ||
| }; | ||
| } | ||
| } | ||
| class ModuleType { | ||
@@ -99,2 +111,5 @@ kind; | ||
| } | ||
| export function importerId(pattern, params) { | ||
| return new ImporterId(pattern, params); | ||
| } | ||
| export function moduleType(pattern) { | ||
@@ -135,3 +150,3 @@ return new ModuleType(pattern); | ||
| } | ||
| export function interpreter(exprs, code, id, moduleType) { | ||
| export function interpreter(exprs, code, id, moduleType, importerId) { | ||
| let arr = []; | ||
@@ -144,5 +159,5 @@ if (Array.isArray(exprs)) { | ||
| } | ||
| return interpreterImpl(arr, code, id, moduleType); | ||
| return interpreterImpl(arr, code, id, moduleType, importerId); | ||
| } | ||
| export function interpreterImpl(expr, code, id, moduleType, ctx = {}) { | ||
| export function interpreterImpl(expr, code, id, moduleType, importerId, ctx = {}) { | ||
| let hasInclude = false; | ||
@@ -153,3 +168,3 @@ for (const e of expr) { | ||
| hasInclude = true; | ||
| if (exprInterpreter(e.expr, code, id, moduleType, ctx)) { | ||
| if (exprInterpreter(e.expr, code, id, moduleType, importerId, ctx)) { | ||
| return true; | ||
@@ -160,3 +175,3 @@ } | ||
| case 'exclude': { | ||
| if (exprInterpreter(e.expr, code, id, moduleType)) { | ||
| if (exprInterpreter(e.expr, code, id, moduleType, importerId, ctx)) { | ||
| return false; | ||
@@ -170,12 +185,12 @@ } | ||
| } | ||
| export function exprInterpreter(expr, code, id, moduleType, ctx = {}) { | ||
| export function exprInterpreter(expr, code, id, moduleType, importerId, ctx = {}) { | ||
| switch (expr.kind) { | ||
| case 'and': { | ||
| return expr.args.every((e) => exprInterpreter(e, code, id, moduleType, ctx)); | ||
| return expr.args.every((e) => exprInterpreter(e, code, id, moduleType, importerId, ctx)); | ||
| } | ||
| case 'or': { | ||
| return expr.args.some((e) => exprInterpreter(e, code, id, moduleType, ctx)); | ||
| return expr.args.some((e) => exprInterpreter(e, code, id, moduleType, importerId, ctx)); | ||
| } | ||
| case 'not': { | ||
| return !exprInterpreter(expr.expr, code, id, moduleType, ctx); | ||
| return !exprInterpreter(expr.expr, code, id, moduleType, importerId, ctx); | ||
| } | ||
@@ -186,9 +201,22 @@ case 'id': { | ||
| } | ||
| let idToMatch = id; | ||
| if (expr.params.cleanUrl) { | ||
| id = cleanUrl(id); | ||
| idToMatch = cleanUrl(idToMatch); | ||
| } | ||
| return typeof expr.pattern === 'string' | ||
| ? id === expr.pattern | ||
| : expr.pattern.test(id); | ||
| ? idToMatch === expr.pattern | ||
| : expr.pattern.test(idToMatch); | ||
| } | ||
| case 'importerId': { | ||
| if (importerId === undefined) { | ||
| return false; // Entry files have no importer, so no match | ||
| } | ||
| let importerIdToMatch = importerId; | ||
| if (expr.params.cleanUrl) { | ||
| importerIdToMatch = cleanUrl(importerIdToMatch); | ||
| } | ||
| return typeof expr.pattern === 'string' | ||
| ? importerIdToMatch === expr.pattern | ||
| : expr.pattern.test(importerIdToMatch); | ||
| } | ||
| case 'moduleType': { | ||
@@ -195,0 +223,0 @@ if (moduleType === undefined) { |
+1
-1
| { | ||
| "name": "@rolldown/pluginutils", | ||
| "version": "1.0.0-beta.55", | ||
| "version": "1.0.0-beta.56", | ||
| "license": "MIT", | ||
@@ -5,0 +5,0 @@ "type": "module", |
23060
6.14%615
6.03%