@@ -27,7 +27,7 @@ export interface ExternalModulesExecutorOptions {
27
27
}
28
28
29
29
interface ModuleInformation {
30
- type : 'data' | 'builtin' | 'vite' | 'wasm' | 'module' | 'commonjs'
30
+ type : 'data' | 'builtin' | 'vite' | 'wasm' | 'module' | 'commonjs' | 'network'
31
31
url : string
32
32
path : string
33
33
}
@@ -41,6 +41,8 @@ export class ExternalModulesExecutor {
41
41
private fs : FileMap
42
42
private resolvers : ( ( id : string , parent : string ) => string | undefined ) [ ] = [ ]
43
43
44
+ #networkSupported: boolean | null = null
45
+
44
46
constructor ( private options : ExternalModulesExecutorOptions ) {
45
47
this . context = options . context
46
48
@@ -62,6 +64,20 @@ export class ExternalModulesExecutor {
62
64
this . resolvers = [ this . vite . resolve ]
63
65
}
64
66
67
+ async import ( identifier : string ) {
68
+ const module = await this . createModule ( identifier )
69
+ await this . esm . evaluateModule ( module )
70
+ return module . namespace
71
+ }
72
+
73
+ require ( identifier : string ) {
74
+ return this . cjs . require ( identifier )
75
+ }
76
+
77
+ createRequire ( identifier : string ) {
78
+ return this . cjs . createRequire ( identifier )
79
+ }
80
+
65
81
// dynamic import can be used in both ESM and CJS, so we have it in the executor
66
82
public importModuleDynamically = async ( specifier : string , referencer : VMModule ) => {
67
83
const module = await this . resolveModule ( specifier , referencer . identifier )
@@ -161,6 +177,9 @@ export class ExternalModulesExecutor {
161
177
if ( extension === '.node' || isNodeBuiltin ( identifier ) )
162
178
return { type : 'builtin' , url : identifier , path : identifier }
163
179
180
+ if ( this . isNetworkSupported && ( identifier . startsWith ( 'http:' ) || identifier . startsWith ( 'https:' ) ) )
181
+ return { type : 'network' , url : identifier , path : identifier }
182
+
164
183
const isFileUrl = identifier . startsWith ( 'file://' )
165
184
const pathUrl = isFileUrl ? fileURLToPath ( identifier . split ( '?' ) [ 0 ] ) : identifier
166
185
const fileUrl = isFileUrl ? identifier : pathToFileURL ( pathUrl ) . toString ( )
@@ -209,31 +228,32 @@ export class ExternalModulesExecutor {
209
228
case 'vite' :
210
229
return await this . vite . createViteModule ( url )
211
230
case 'wasm' :
212
- return await this . esm . createWebAssemblyModule ( url , this . fs . readBuffer ( path ) )
231
+ return await this . esm . createWebAssemblyModule ( url , ( ) => this . fs . readBuffer ( path ) )
213
232
case 'module' :
214
- return await this . esm . createEsModule ( url , this . fs . readFile ( path ) )
233
+ return await this . esm . createEsModule ( url , ( ) => this . fs . readFile ( path ) )
215
234
case 'commonjs' : {
216
235
const exports = this . require ( path )
217
236
return this . wrapCommonJsSynteticModule ( identifier , exports )
218
237
}
238
+ case 'network' : {
239
+ return this . esm . createNetworkModule ( url )
240
+ }
219
241
default : {
220
242
const _deadend : never = type
221
243
return _deadend
222
244
}
223
245
}
224
246
}
225
247
226
- async import ( identifier : string ) {
227
- const module = await this . createModule ( identifier )
228
- await this . esm . evaluateModule ( module )
229
- return module . namespace
230
- }
231
-
232
- require ( identifier : string ) {
233
- return this . cjs . require ( identifier )
234
- }
235
-
236
- createRequire ( identifier : string ) {
237
- return this . cjs . createRequire ( identifier )
248
+ private get isNetworkSupported ( ) {
249
+ if ( this . #networkSupported == null ) {
250
+ if ( process . execArgv . includes ( '--experimental-network-imports' ) )
251
+ this . #networkSupported = true
252
+ else if ( process . env . NODE_OPTIONS ?. includes ( '--experimental-network-imports' ) )
253
+ this . #networkSupported = true
254
+ else
255
+ this . #networkSupported = false
256
+ }
257
+ return this . #networkSupported
238
258
}
239
259
}
0 commit comments