11import * as fs from 'node:fs'
2- import { fileURLToPath } from 'node:url'
2+ import { createRequire } from 'node:module'
3+ import { fileURLToPath , pathToFileURL } from 'node:url'
34
45import { getFilePathDirectory , normalizeFilePath } from './paths.ts'
56
@@ -90,7 +91,7 @@ function getResolvedInjectedPackage(packageName: string): ResolvedInjectedPackag
9091 let existing = resolvedInjectedPackages . get ( packageName )
9192 if ( existing ) return existing
9293
93- let packageJsonUrl = import . meta . resolve ( ` ${ packageName } /package.json` )
94+ let packageJsonUrl = resolveInjectedPackageJsonUrl ( packageName )
9495 let packageJsonPath = normalizeFilePath ( fs . realpathSync ( fileURLToPath ( packageJsonUrl ) ) )
9596
9697 let resolvedInjectedPackage = {
@@ -102,6 +103,18 @@ function getResolvedInjectedPackage(packageName: string): ResolvedInjectedPackag
102103 return resolvedInjectedPackage
103104}
104105
106+ export function resolveInjectedPackageJsonUrl (
107+ packageName : string ,
108+ resolve : ( ( specifier : string ) => string ) | undefined = import . meta. resolve ,
109+ ) : string {
110+ if ( typeof resolve === 'function' ) {
111+ return resolve ( `${ packageName } /package.json` )
112+ }
113+
114+ let require = createRequire ( import . meta. url )
115+ return pathToFileURL ( require . resolve ( `${ packageName } /package.json` ) ) . href
116+ }
117+
105118function getInjectedPackageRoutePattern ( packageName : string ) : string {
106119 return `${ injectedPackagesBasePath } /${ packageName } /*path`
107120}
0 commit comments