Files
SnowLuma/.pnpm-store/v11/files/56/5d5f43f4f3d9e3bbcfbcdf6b14d3bb39c6b4261db6ea37adfa6ec70fbc7629b20f75352938425b6a94b45b09f39f31c7b9cde56aad2dd42d453a17b04e9ce9
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

71 lines
1.9 KiB
Plaintext

/**
* Constructs a RegExp that matches the exact string specified.
*
* This is useful for plugin hook filters.
*
* @param str the string to match.
* @param flags flags for the RegExp.
*
* @example
* ```ts
* import { exactRegex } from '@rolldown/pluginutils';
* const plugin = {
* name: 'plugin',
* resolveId: {
* filter: { id: exactRegex('foo') },
* handler(id) {} // will only be called for `foo`
* }
* }
* ```
*/
export function exactRegex(str, flags) {
return new RegExp(`^${escapeRegex(str)}$`, flags);
}
/**
* Constructs a RegExp that matches a value that has the specified prefix.
*
* This is useful for plugin hook filters.
*
* @param str the string to match.
* @param flags flags for the RegExp.
*
* @example
* ```ts
* import { prefixRegex } from '@rolldown/pluginutils';
* const plugin = {
* name: 'plugin',
* resolveId: {
* filter: { id: prefixRegex('foo') },
* handler(id) {} // will only be called for IDs starting with `foo`
* }
* }
* ```
*/
export function prefixRegex(str, flags) {
return new RegExp(`^${escapeRegex(str)}`, flags);
}
const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
function escapeRegex(str) {
return str.replace(escapeRegexRE, '\\$&');
}
export function makeIdFiltersToMatchWithQuery(input) {
if (!Array.isArray(input)) {
return makeIdFilterToMatchWithQuery(
// Array.isArray cannot narrow the type
// https://github.com/microsoft/TypeScript/issues/17002
input);
}
return input.map((i) => makeIdFilterToMatchWithQuery(i));
}
function makeIdFilterToMatchWithQuery(input) {
if (typeof input === 'string') {
return `${input}{?*,}`;
}
return makeRegexIdFilterToMatchWithQuery(input);
}
function makeRegexIdFilterToMatchWithQuery(input) {
return new RegExp(
// replace `$` with `(?:\?.*)?$` (ignore `\$`)
input.source.replace(/(?<!\\)\$/g, '(?:\\?.*)?$'), input.flags);
}