1- import { URL , pathToFileURL , fileURLToPath } from 'url'
1+ import { URL , fileURLToPath , pathToFileURL } from 'url'
22import fs from 'fs'
33import { dirname } from 'path'
4- import { transformSync , build } from 'esbuild'
5- import fetch from 'node-fetch'
4+ import { build , transformSync } from 'esbuild'
65
76const isWindows = process . platform === 'win32'
87
@@ -144,7 +143,7 @@ export async function load(url, context, defaultLoad) {
144143 if ( httpRegex . test ( url ) ) {
145144 return {
146145 format : 'module' ,
147- source : await ( await fetch ( url ) ) . text ( ) ,
146+ source : await fetchNetworkModule ( url ) ,
148147 }
149148 }
150149
@@ -190,7 +189,7 @@ export async function transformSource(source, context, defaultTransformSource) {
190189 if ( httpRegex . test ( url ) ) {
191190 return {
192191 format : 'module' ,
193- source : await ( await fetch ( url ) ) . text ( ) ,
192+ source : await fetchNetworkModule ( url ) ,
194193 }
195194 }
196195
@@ -212,9 +211,25 @@ export async function transformSource(source, context, defaultTransformSource) {
212211export async function getSource ( url , context , defaultGetSource ) {
213212 if ( httpRegex . test ( url ) ) {
214213 return {
215- source : await ( await fetch ( url ) ) . text ( ) ,
214+ source : await fetchNetworkModule ( url ) ,
216215 }
217216 }
218217
219218 return defaultGetSource ( url , context , defaultGetSource )
220219}
220+
221+ export const networkModuleCache = new Map ( )
222+
223+ function fetchNetworkModule ( url ) {
224+ if ( ! networkModuleCache . has ( url ) ) {
225+ const promise = ( async ( ) => {
226+ const _fetch = ( typeof fetch != 'undefined' )
227+ ? fetch
228+ : ( await import ( 'node-fetch' ) ) . default
229+
230+ return await _fetch ( url ) . then ( r => r . text ( ) )
231+ } ) ( )
232+ networkModuleCache . set ( url , promise )
233+ }
234+ return networkModuleCache . get ( url )
235+ }
0 commit comments