@@ -40,12 +40,23 @@ async function resolve(specifier: string, context: { parentURL?: string }, defau
40
40
return result ;
41
41
}
42
42
43
+ // non-js files have undefined
44
+ // some js files have null
45
+ // {module/commonjs}-typescript are changed to {module,commonjs} because we handle typescript ourselves
46
+ const kSupportedFormats = new Map ( [
47
+ [ 'commonjs' , 'commonjs' ] ,
48
+ [ 'module' , 'module' ] ,
49
+ [ 'commonjs-typescript' , 'commonjs' ] ,
50
+ [ 'module-typescript' , 'module' ] ,
51
+ [ null , null ] ,
52
+ [ undefined , undefined ]
53
+ ] ) ;
54
+
43
55
// Node < 18.6: defaultLoad takes 3 arguments.
44
56
// Node >= 18.6: nextLoad from the chain takes 2 arguments.
45
57
async function load ( moduleUrl : string , context : { format ?: string } , defaultLoad : Function ) {
46
58
// Bail out for wasm, json, etc.
47
- // non-js files have context.format === undefined
48
- if ( context . format !== 'commonjs' && context . format !== 'module' && context . format !== undefined )
59
+ if ( ! kSupportedFormats . has ( context . format ) )
49
60
return defaultLoad ( moduleUrl , context , defaultLoad ) ;
50
61
51
62
// Bail for built-in modules.
@@ -67,7 +78,7 @@ async function load(moduleUrl: string, context: { format?: string }, defaultLoad
67
78
// Output format is required, so we determine it manually when unknown.
68
79
// shortCircuit is required by Node >= 18.6 to designate no more loaders should be called.
69
80
return {
70
- format : context . format || ( fileIsModule ( filename ) ? 'module' : 'commonjs' ) ,
81
+ format : kSupportedFormats . get ( context . format ) || ( fileIsModule ( filename ) ? 'module' : 'commonjs' ) ,
71
82
source : transformed . code ,
72
83
shortCircuit : true ,
73
84
} ;
0 commit comments