@@ -2,6 +2,7 @@ import EnhancedResolve from 'enhanced-resolve'
22import { createJiti , type Jiti } from 'jiti'
33import fs from 'node:fs'
44import fsPromises from 'node:fs/promises'
5+ import type Module from 'node:module'
56import path , { dirname } from 'node:path'
67import { pathToFileURL } from 'node:url'
78import {
@@ -87,6 +88,13 @@ export async function loadModule(id: string, base: string, onDependency: (path:
8788 throw new Error ( `Could not resolve '${ id } ' from '${ base } '` )
8889 }
8990
91+ if ( typeof resolvedPath !== 'string' ) {
92+ return {
93+ base,
94+ module : resolvedPath ,
95+ }
96+ }
97+
9098 let module = await importModule ( pathToFileURL ( resolvedPath ) . href )
9199 return {
92100 base : dirname ( resolvedPath ) ,
@@ -98,6 +106,14 @@ export async function loadModule(id: string, base: string, onDependency: (path:
98106 if ( ! resolvedPath ) {
99107 throw new Error ( `Could not resolve '${ id } ' from '${ base } '` )
100108 }
109+
110+ if ( typeof resolvedPath !== 'string' ) {
111+ return {
112+ base,
113+ module : resolvedPath ,
114+ }
115+ }
116+
101117 let [ module , moduleDependencies ] = await Promise . all ( [
102118 importModule ( pathToFileURL ( resolvedPath ) . href + '?id=' + Date . now ( ) ) ,
103119 getModuleDependencies ( resolvedPath ) ,
@@ -173,7 +189,13 @@ const jsResolver = EnhancedResolve.ResolverFactory.createResolver({
173189 conditionNames : import . meta. url ? [ 'node' , 'import' ] : [ 'node' , 'require' ] ,
174190} )
175191
176- function resolveJsId ( id : string , base : string ) : Promise < string | false | undefined > {
192+ function resolveJsId ( id : string , base : string ) : Promise < string | false | undefined | Module > {
193+ if ( typeof globalThis . __tw_resolve === 'function' ) {
194+ let resolved = globalThis . __tw_resolve ( id , base )
195+ if ( resolved ) {
196+ return Promise . resolve ( resolved )
197+ }
198+ }
177199 return runResolver ( jsResolver , id , base )
178200}
179201
0 commit comments