feat: Allow finds to use regex (#2452)

This commit is contained in:
Nuckyz 2024-05-14 23:57:43 -03:00 committed by GitHub
parent 4da8b9aad7
commit f74da73086
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 13 deletions

View file

@ -29,14 +29,19 @@ export default function definePlugin<P extends PluginDef>(p: P & Record<string,
export type ReplaceFn = (match: string, ...groups: string[]) => string;
export interface PatchReplacement {
/** The match for the patch replacement. If you use a string it will be implicitly converted to a RegExp */
match: string | RegExp;
/** The replacement string or function which returns the string for the patch replacement */
replace: string | ReplaceFn;
/** A function which returns whether this patch replacement should be applied */
predicate?(): boolean;
}
export interface Patch {
plugin: string;
find: string;
/** A string or RegExp which is only include/matched in the module code you wish to patch. Prefer only using a RegExp if a simple string test is not enough */
find: string | RegExp;
/** The replacement(s) for the module being patched */
replacement: PatchReplacement | PatchReplacement[];
/** Whether this patch should apply to multiple modules */
all?: boolean;
@ -44,6 +49,7 @@ export interface Patch {
noWarn?: boolean;
/** Only apply this set of replacements if all of them succeed. Use this if your replacements depend on each other */
group?: boolean;
/** A function which returns whether this patch should be applied */
predicate?(): boolean;
}