@@ -27,7 +27,7 @@ export interface ExternalModulesExecutorOptions {
2727}
2828
2929interface ModuleInformation {
30- type : 'data' | 'builtin' | 'vite' | 'wasm' | 'module' | 'commonjs'
30+ type : 'data' | 'builtin' | 'vite' | 'wasm' | 'module' | 'commonjs' | 'network'
3131 url : string
3232 path : string
3333}
@@ -41,6 +41,8 @@ export class ExternalModulesExecutor {
4141 private fs : FileMap
4242 private resolvers : ( ( id : string , parent : string ) => string | undefined ) [ ] = [ ]
4343
44+ #networkSupported: boolean | null = null
45+
4446 constructor ( private options : ExternalModulesExecutorOptions ) {
4547 this . context = options . context
4648
@@ -62,6 +64,20 @@ export class ExternalModulesExecutor {
6264 this . resolvers = [ this . vite . resolve ]
6365 }
6466
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+
6581 // dynamic import can be used in both ESM and CJS, so we have it in the executor
6682 public importModuleDynamically = async ( specifier : string , referencer : VMModule ) => {
6783 const module = await this . resolveModule ( specifier , referencer . identifier )
@@ -161,6 +177,9 @@ export class ExternalModulesExecutor {
161177 if ( extension === '.node' || isNodeBuiltin ( identifier ) )
162178 return { type : 'builtin' , url : identifier , path : identifier }
163179
180+ if ( this . isNetworkSupported && ( identifier . startsWith ( 'http:' ) || identifier . startsWith ( 'https:' ) ) )
181+ return { type : 'network' , url : identifier , path : identifier }
182+
164183 const isFileUrl = identifier . startsWith ( 'file://' )
165184 const pathUrl = isFileUrl ? fileURLToPath ( identifier . split ( '?' ) [ 0 ] ) : identifier
166185 const fileUrl = isFileUrl ? identifier : pathToFileURL ( pathUrl ) . toString ( )
@@ -209,31 +228,32 @@ export class ExternalModulesExecutor {
209228 case 'vite' :
210229 return await this . vite . createViteModule ( url )
211230 case 'wasm' :
212- return await this . esm . createWebAssemblyModule ( url , this . fs . readBuffer ( path ) )
231+ return await this . esm . createWebAssemblyModule ( url , ( ) => this . fs . readBuffer ( path ) )
213232 case 'module' :
214- return await this . esm . createEsModule ( url , this . fs . readFile ( path ) )
233+ return await this . esm . createEsModule ( url , ( ) => this . fs . readFile ( path ) )
215234 case 'commonjs' : {
216235 const exports = this . require ( path )
217236 return this . wrapCommonJsSynteticModule ( identifier , exports )
218237 }
238+ case 'network' : {
239+ return this . esm . createNetworkModule ( url )
240+ }
219241 default : {
220242 const _deadend : never = type
221243 return _deadend
222244 }
223245 }
224246 }
225247
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
238258 }
239259}
0 commit comments