File tree 4 files changed +45
-5
lines changed
angular/cli/src/utilities
4 files changed +45
-5
lines changed Original file line number Diff line number Diff line change 6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
+ /**
10
+ * Lazily compiled dynamic import loader function.
11
+ */
12
+ let load : ( < T > ( modulePath : string | URL ) => Promise < T > ) | undefined ;
13
+
9
14
/**
10
15
* This uses a dynamic import to load a module which may be ESM.
11
16
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
19
24
* @returns A Promise that resolves to the dynamically imported module.
20
25
*/
21
26
export function loadEsmModule < T > ( modulePath : string | URL ) : Promise < T > {
22
- return new Function ( 'modulePath' , `return import(modulePath);` ) ( modulePath ) as Promise < T > ;
27
+ load ??= new Function ( 'modulePath' , `return import(modulePath);` ) as Exclude <
28
+ typeof load ,
29
+ undefined
30
+ > ;
31
+
32
+ return load ( modulePath ) ;
23
33
}
Original file line number Diff line number Diff line change @@ -213,6 +213,11 @@ export class WorkspaceNodeModulesArchitectHost implements ArchitectHost<NodeModu
213
213
}
214
214
}
215
215
216
+ /**
217
+ * Lazily compiled dynamic import loader function.
218
+ */
219
+ let load : ( < T > ( modulePath : string | URL ) => Promise < T > ) | undefined ;
220
+
216
221
/**
217
222
* This uses a dynamic import to load a module which may be ESM.
218
223
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
@@ -225,8 +230,13 @@ export class WorkspaceNodeModulesArchitectHost implements ArchitectHost<NodeModu
225
230
* @param modulePath The path of the module to load.
226
231
* @returns A Promise that resolves to the dynamically imported module.
227
232
*/
228
- function loadEsmModule < T > ( modulePath : string | URL ) : Promise < T > {
229
- return new Function ( 'modulePath' , `return import(modulePath);` ) ( modulePath ) as Promise < T > ;
233
+ export function loadEsmModule < T > ( modulePath : string | URL ) : Promise < T > {
234
+ load ??= new Function ( 'modulePath' , `return import(modulePath);` ) as Exclude <
235
+ typeof load ,
236
+ undefined
237
+ > ;
238
+
239
+ return load ( modulePath ) ;
230
240
}
231
241
232
242
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Original file line number Diff line number Diff line change 6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
+ /**
10
+ * Lazily compiled dynamic import loader function.
11
+ */
12
+ let load : ( < T > ( modulePath : string | URL ) => Promise < T > ) | undefined ;
13
+
9
14
/**
10
15
* This uses a dynamic import to load a module which may be ESM.
11
16
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
19
24
* @returns A Promise that resolves to the dynamically imported module.
20
25
*/
21
26
export function loadEsmModule < T > ( modulePath : string | URL ) : Promise < T > {
22
- return new Function ( 'modulePath' , `return import(modulePath);` ) ( modulePath ) as Promise < T > ;
27
+ load ??= new Function ( 'modulePath' , `return import(modulePath);` ) as Exclude <
28
+ typeof load ,
29
+ undefined
30
+ > ;
31
+
32
+ return load ( modulePath ) ;
23
33
}
Original file line number Diff line number Diff line change @@ -489,6 +489,11 @@ if (require.main === module) {
489
489
} ) ;
490
490
}
491
491
492
+ /**
493
+ * Lazily compiled dynamic import loader function.
494
+ */
495
+ let load : ( < T > (modulePath: string | URL) => Promise < T > ) | undefined;
496
+
492
497
/**
493
498
* This uses a dynamic import to load a module which may be ESM.
494
499
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
@@ -502,5 +507,10 @@ if (require.main === module) {
502
507
* @returns A Promise that resolves to the dynamically imported module.
503
508
*/
504
509
export function loadEsmModule< T > (modulePath: string | URL): Promise< T > {
505
- return new Function ( 'modulePath' , `return import(modulePath);` ) ( modulePath ) as Promise < T > ;
510
+ load ??= new Function ( 'modulePath' , `return import(modulePath);` ) as Exclude <
511
+ typeof load ,
512
+ undefined
513
+ > ;
514
+
515
+ return load ( modulePath ) ;
506
516
}
You can’t perform that action at this time.
0 commit comments