4
4
require ( 'internal/modules/cjs/loader' ) ;
5
5
6
6
const {
7
+ ArrayPrototypeJoin,
8
+ ArrayPrototypeMap,
9
+ ArrayPrototypeReduce,
7
10
FunctionPrototypeCall,
11
+ JSONStringify,
8
12
ObjectSetPrototypeOf,
13
+ RegExpPrototypeSymbolReplace,
9
14
SafeWeakMap,
15
+ encodeURIComponent,
16
+ hardenRegExp,
10
17
} = primordials ;
11
18
12
19
const {
@@ -511,7 +518,7 @@ class CustomizedModuleLoader {
511
518
}
512
519
}
513
520
514
- let emittedExperimentalWarning = false ;
521
+ let emittedLoaderFlagWarning = false ;
515
522
/**
516
523
* A loader instance is used as the main entry point for loading ES modules. Currently, this is a singleton; there is
517
524
* only one used for loading the main module and everything in its dependency graph, though separate instances of this
@@ -527,9 +534,24 @@ function createModuleLoader(useCustomLoadersIfPresent = true) {
527
534
! require ( 'internal/modules/esm/utils' ) . isLoaderWorker ( ) ) {
528
535
const userLoaderPaths = getOptionValue ( '--experimental-loader' ) ;
529
536
if ( userLoaderPaths . length > 0 ) {
530
- if ( ! emittedExperimentalWarning ) {
531
- emitExperimentalWarning ( 'Custom ESM Loaders' ) ;
532
- emittedExperimentalWarning = true ;
537
+ if ( ! emittedLoaderFlagWarning ) {
538
+ const readableURIEncode = ( string ) => ArrayPrototypeReduce (
539
+ [
540
+ [ / ' / g, '%27' ] , // We need to URL-encode the single quote as it's the delimiter for the --import flag.
541
+ [ / % 2 2 / g, '"' ] , // We can decode the double quotes to improve readability.
542
+ [ / % 2 F / ig, '/' ] , // We can decode the slashes to improve readability.
543
+ ] ,
544
+ ( str , { 0 : regex , 1 : replacement } ) => RegExpPrototypeSymbolReplace ( hardenRegExp ( regex ) , str , replacement ) ,
545
+ encodeURIComponent ( string ) ) ;
546
+ process . emitWarning (
547
+ '`--experimental-loader` may be removed in the future; instead use `register()`:\n' +
548
+ `--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; ${ ArrayPrototypeJoin (
549
+ ArrayPrototypeMap ( userLoaderPaths , ( loader ) => `register(${ readableURIEncode ( JSONStringify ( loader ) ) } , pathToFileURL("./"))` ) ,
550
+ '; ' ,
551
+ ) } ;'`,
552
+ 'ExperimentalWarning' ,
553
+ ) ;
554
+ emittedLoaderFlagWarning = true ;
533
555
}
534
556
customizations = new CustomizedModuleLoader ( ) ;
535
557
}
0 commit comments