@@ -11,7 +11,7 @@ import { extname, isAbsolute, join } from 'node:path'
11
11
import { fileURLToPath , URL , pathToFileURL } from 'node:url'
12
12
13
13
import debugFactory from 'debug'
14
- import { EnforceExtension , ResolverFactory } from 'oxc-resolver'
14
+ import { EnforceExtension , ResolverFactory , type NapiResolveOptions } from 'oxc-resolver'
15
15
import ts from 'typescript'
16
16
17
17
// @ts -expect-error
@@ -35,22 +35,6 @@ const TSCONFIG_PATH = (function () {
35
35
return pathFromEnv
36
36
} ) ( )
37
37
38
- const resolver = new ResolverFactory ( {
39
- tsconfig : {
40
- configFile : TSCONFIG_PATH ,
41
- references : 'auto' ,
42
- } ,
43
- conditionNames : [ 'node' , 'import' ] ,
44
- enforceExtension : EnforceExtension . Auto ,
45
- extensions : [ '.js' , '.mjs' , '.cjs' , '.ts' , '.tsx' , '.mts' , '.cts' , '.json' , '.wasm' , '.node' ] ,
46
- extensionAlias : {
47
- '.js' : [ '.ts' , '.tsx' , '.js' ] ,
48
- '.mjs' : [ '.mts' , '.mjs' ] ,
49
- '.cjs' : [ '.cts' , '.cjs' ] ,
50
- } ,
51
- moduleType : true ,
52
- } )
53
-
54
38
async function getModuleType ( path : string ) : Promise < 'module' | 'commonjs' | undefined > {
55
39
const pkgJsonReadContent = await readPackageJSON ( path )
56
40
return pkgJsonReadContent ?. type
@@ -152,9 +136,37 @@ const EXTENSION_MODULE_MAP = {
152
136
'.node' : 'commonjs' ,
153
137
} as const
154
138
139
+ let conditions : string [ ] | undefined = undefined
140
+
141
+ const resolverOptions : NapiResolveOptions = {
142
+ tsconfig : {
143
+ configFile : TSCONFIG_PATH ,
144
+ references : 'auto' ,
145
+ } ,
146
+ conditionNames : [ 'node' , 'import' ] ,
147
+ enforceExtension : EnforceExtension . Auto ,
148
+ extensions : [ '.js' , '.mjs' , '.cjs' , '.ts' , '.tsx' , '.mts' , '.cts' , '.json' , '.wasm' , '.node' ] ,
149
+ extensionAlias : {
150
+ '.js' : [ '.ts' , '.tsx' , '.js' ] ,
151
+ '.mjs' : [ '.mts' , '.mjs' ] ,
152
+ '.cjs' : [ '.cts' , '.cjs' ] ,
153
+ } ,
154
+ moduleType : true ,
155
+ }
156
+
157
+ let resolver = new ResolverFactory ( resolverOptions )
158
+
155
159
export const resolve : ResolveHook = async ( specifier , context , nextResolve ) => {
156
160
debug ( 'resolve' , specifier , JSON . stringify ( context ) )
157
161
162
+ if ( ! conditions ) {
163
+ conditions = context . conditions
164
+ resolver = resolver . cloneWithOptions ( {
165
+ ...resolverOptions ,
166
+ conditionNames : conditions ,
167
+ } )
168
+ }
169
+
158
170
if ( specifier . startsWith ( 'node:' ) || specifier . startsWith ( 'nodejs:' ) ) {
159
171
debug ( 'skip resolve: internal format' , specifier )
160
172
0 commit comments