@@ -29,16 +29,20 @@ import { PluginContext, ResolveResult } from "../../types/context";
2929
3030export interface CreateUnpluginModuleResolutionFunctionsOptions {
3131 /**
32- * A prefix to apply to all resolved module IDs. This can be used to create virtual modules by prefixing them with a specific string (e.g., `\0`) .
32+ * An indicator of whether to prefix virtual module IDs with a specific string . This is useful for ensuring that virtual modules are only processed by the plugin and not by other plugins or the bundler itself .
3333 *
3434 * @remarks
35- * If set to `false`, no prefix will be applied, and resolved module IDs will be returned as-is. By default, this is set to `\0powerlines:`, which is a common convention for virtual modules in Rollup and Vite plugins.
35+ * - If set to `true`, virtual module IDs will be prefixed with the string `\0powerlines:`.
36+ * - If set to `false`, no prefix will be added to virtual module IDs.
3637 *
37- * @defaultValue "\\0powerlines:"
38+ * @defaultValue true
3839 */
39- prefix ?: string | boolean ;
40+ prefix ?: boolean ;
4041}
4142
43+ const VIRTUAL_MODULE_PREFIX = "\\0powerlines-virtual:" ;
44+ const VIRTUAL_MODULE_PREFIX_REGEX = / ^ \\ 0 p o w e r l i n e s - v i r t u a l : / ;
45+
4246/**
4347 * Creates the module resolution hook functions for a Powerlines unplugin plugin instance.
4448 *
@@ -49,6 +53,7 @@ export interface CreateUnpluginModuleResolutionFunctionsOptions {
4953 * @see https://rollupjs.org/plugin-development/#load
5054 *
5155 * @param context - The plugin context.
56+ * @param options - Options for creating the module resolution functions.
5257 * @returns The module resolution hooks (`resolveId` and `load`).
5358 */
5459export function createUnpluginModuleResolutionFunctions <
@@ -59,13 +64,6 @@ export function createUnpluginModuleResolutionFunctions<
5964) : Pick < UnpluginOptions , "resolveId" | "load" > {
6065 const ctx = context as unknown as UNSAFE_PluginContext ;
6166
62- let prefix = "" ;
63- if ( isSetString ( options . prefix ) ) {
64- prefix = options . prefix ;
65- } else if ( options . prefix !== false ) {
66- prefix = "\0powerlines:" ;
67- }
68-
6967 return {
7068 async resolveId (
7169 this : UnpluginBuildContext & UnpluginContext ,
@@ -91,7 +89,10 @@ export function createUnpluginModuleResolutionFunctions<
9189 } else if ( isSetObject ( result ) ) {
9290 return {
9391 ...result ,
94- id : result . virtual ? `${ prefix } ${ result . id } ` : result . id
92+ id :
93+ result . virtual && options . prefix !== false
94+ ? `${ VIRTUAL_MODULE_PREFIX } ${ result . id } `
95+ : result . id
9596 } ;
9697 }
9798
@@ -111,15 +112,21 @@ export function createUnpluginModuleResolutionFunctions<
111112 } else if ( isSetObject ( result ) ) {
112113 return {
113114 ...result ,
114- id : result . virtual ? `${ prefix } ${ result . id } ` : result . id
115+ id :
116+ result . virtual && options . prefix !== false
117+ ? `${ VIRTUAL_MODULE_PREFIX } ${ result . id } `
118+ : result . id
115119 } ;
116120 }
117121
118122 result = await ctx . resolve ( id , importer , { isFile : true , ...opts } ) ;
119123 if ( isSetObject ( result ) ) {
120124 return {
121125 ...result ,
122- id : result . virtual ? `${ prefix } ${ result . id } ` : result . id
126+ id :
127+ result . virtual && options . prefix !== false
128+ ? `${ VIRTUAL_MODULE_PREFIX } ${ result . id } `
129+ : result . id
123130 } ;
124131 }
125132
@@ -139,35 +146,30 @@ export function createUnpluginModuleResolutionFunctions<
139146 } else if ( isSetObject ( result ) ) {
140147 return {
141148 ...result ,
142- id : result . virtual ? `${ prefix } ${ result . id } ` : result . id
149+ id :
150+ result . virtual && options . prefix !== false
151+ ? `${ VIRTUAL_MODULE_PREFIX } ${ result . id } `
152+ : result . id
143153 } ;
144154 }
145155
146156 return null ;
147157 } ,
148158 load : {
149- filter : prefix
150- ? {
151- id : {
152- include : [
153- new RegExp ( `^ ${ prefix . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g , "\\$&" ) } ` )
154- ]
159+ filter :
160+ options . prefix !== false
161+ ? {
162+ id : {
163+ include : [ VIRTUAL_MODULE_PREFIX_REGEX ]
164+ }
155165 }
156- }
157- : undefined ,
166+ : undefined ,
158167 async handler (
159168 this : UnpluginBuildContext & UnpluginContext ,
160169 id : string
161170 ) : Promise < LoadResult | null | undefined > {
162- const moduleId = prefix
163- ? id . replace (
164- new RegExp (
165- `^${ prefix . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) } ` ,
166- "g"
167- ) ,
168- ""
169- )
170- : id ;
171+ const moduleId =
172+ options . prefix !== false ? id . replace ( VIRTUAL_MODULE_PREFIX , "" ) : id ;
171173
172174 let result = await ctx . $$internal . callHook (
173175 "load" ,
0 commit comments