@@ -43,6 +43,12 @@ const DEFAULT_PROJECT_PLUGIN_PATHS = ['@theme/plugin.js', '@theme/plugin.cjs', '
4343// Cache instantiated plugins during a single execution
4444const pluginsCache : Map < string , Plugin [ ] > = new Map ( ) ;
4545
46+ export type PluginResolveInfo = {
47+ absolutePath : string ;
48+ rawPath : string ;
49+ isModule : boolean ;
50+ } ;
51+
4652export type ConfigOptions = {
4753 rawConfigDocument ?: Document < RawUniversalConfig > ;
4854 configPath ?: string ;
@@ -80,14 +86,19 @@ export async function resolveConfig({
8086 externalRefResolver : externalRefResolver ?? new BaseResolver ( getResolveConfig ( config ?. resolve ) ) ,
8187 } ) ;
8288
89+ let pluginsOrPaths : ( Plugin | PluginResolveInfo ) [ ] = [ ] ;
8390 let resolvedPlugins : Plugin [ ] ;
91+ let rootConfigDir : string = '' ;
8492 if ( isBrowser ) {
8593 // In browser, we don't support plugins from config file yet
8694 resolvedPlugins = [ defaultPlugin ] ;
8795 } else {
88- const rootConfigDir = path . dirname ( configPath ?? '' ) ;
89- const pluginsOrPaths = collectConfigPlugins ( rootDocument , resolvedRefMap , rootConfigDir ) ;
90- const plugins = await resolvePlugins ( pluginsOrPaths , rootConfigDir ) ;
96+ rootConfigDir = path . dirname ( configPath ?? '' ) ;
97+ pluginsOrPaths = collectConfigPlugins ( rootDocument , resolvedRefMap , rootConfigDir ) ;
98+ const plugins = await resolvePlugins (
99+ pluginsOrPaths . map ( ( p ) => ( isPluginResolveInfo ( p ) ? p . absolutePath : p ) ) ,
100+ rootConfigDir
101+ ) ;
91102 resolvedPlugins = [ ...plugins , defaultPlugin ] ;
92103 }
93104
@@ -106,8 +117,21 @@ export async function resolveConfig({
106117 ) ;
107118 }
108119
120+ const pluginPaths = pluginsOrPaths . length
121+ ? pluginsOrPaths
122+ . map ( ( p ) =>
123+ isPluginResolveInfo ( p ) && p . isModule
124+ ? p . rawPath
125+ : p . absolutePath && path . relative ( rootConfigDir , p . absolutePath )
126+ )
127+ . filter ( isDefined )
128+ : undefined ;
129+
109130 return {
110- resolvedConfig : bundledConfig ,
131+ resolvedConfig : {
132+ ...bundledConfig ,
133+ plugins : pluginPaths ,
134+ } ,
111135 resolvedRefMap,
112136 plugins : resolvedPlugins ,
113137 } ;
@@ -123,28 +147,35 @@ function getDefaultPluginPath(configDir: string): string | undefined {
123147 return ;
124148}
125149
150+ function isPluginResolveInfo ( plugin : Plugin | PluginResolveInfo ) : plugin is PluginResolveInfo {
151+ return 'isModule' in plugin ;
152+ }
153+
126154export const preResolvePluginPath = (
127155 plugin : string | Plugin ,
128156 base : string ,
129157 rootConfigDir : string
130- ) => {
158+ ) : Plugin | PluginResolveInfo => {
131159 if ( ! isString ( plugin ) ) {
132160 return plugin ;
133161 }
134162
135163 const maybeAbsolutePluginPath = path . resolve ( path . dirname ( base ) , plugin ) ;
136164
137165 return fs . existsSync ( maybeAbsolutePluginPath )
138- ? maybeAbsolutePluginPath
139- : // For plugins imported from packages specifically
140- module . createRequire ( import . meta. url ?? __dirname ) . resolve ( plugin , {
141- paths : [
142- // Plugins imported from the node_modules in the project directory
143- rootConfigDir ,
144- // Plugins imported from the node_modules in the package install directory (for example, npx cache directory)
145- import . meta. url ? path . dirname ( url . fileURLToPath ( import . meta. url ) ) : __dirname ,
146- ] ,
147- } ) ;
166+ ? { absolutePath : maybeAbsolutePluginPath , rawPath : plugin , isModule : false }
167+ : {
168+ absolutePath : module . createRequire ( import . meta. url ?? __dirname ) . resolve ( plugin , {
169+ paths : [
170+ // Plugins imported from the node_modules in the project directory
171+ rootConfigDir ,
172+ // Plugins imported from the node_modules in the package install directory (for example, npx cache directory)
173+ import . meta. url ? path . dirname ( url . fileURLToPath ( import . meta. url ) ) : __dirname ,
174+ ] ,
175+ } ) ,
176+ isModule : true ,
177+ rawPath : plugin ,
178+ } ;
148179} ;
149180
150181export async function resolvePlugins (
@@ -162,11 +193,13 @@ export async function resolvePlugins(
162193 try {
163194 const absolutePluginPath = path . isAbsolute ( plugin )
164195 ? plugin
165- : ( preResolvePluginPath (
166- plugin ,
167- path . join ( configDir , CONFIG_FILE_NAME ) ,
168- configDir
169- ) as string ) ;
196+ : (
197+ preResolvePluginPath (
198+ plugin ,
199+ path . join ( configDir , CONFIG_FILE_NAME ) ,
200+ configDir
201+ ) as PluginResolveInfo
202+ ) . absolutePath ;
170203
171204 if ( ! pluginsCache . has ( absolutePluginPath ) ) {
172205 let requiredPlugin : ImportedPlugin | undefined ;
0 commit comments