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 {
@@ -498,7 +505,7 @@ class CustomizedModuleLoader {
498
505
}
499
506
}
500
507
501
- let emittedExperimentalWarning = false ;
508
+ let emittedLoaderFlagWarning = false ;
502
509
/**
503
510
* A loader instance is used as the main entry point for loading ES modules. Currently, this is a singleton; there is
504
511
* only one used for loading the main module and everything in its dependency graph, though separate instances of this
@@ -514,9 +521,24 @@ function createModuleLoader(useCustomLoadersIfPresent = true) {
514
521
! require ( 'internal/modules/esm/utils' ) . isLoaderWorker ( ) ) {
515
522
const userLoaderPaths = getOptionValue ( '--experimental-loader' ) ;
516
523
if ( userLoaderPaths . length > 0 ) {
517
- if ( ! emittedExperimentalWarning ) {
518
- emitExperimentalWarning ( 'Custom ESM Loaders' ) ;
519
- emittedExperimentalWarning = true ;
524
+ if ( ! emittedLoaderFlagWarning ) {
525
+ const readableURIEncode = ( string ) => ArrayPrototypeReduce (
526
+ [
527
+ [ / ' / g, '%27' ] , // We need to URL-encode the single quote as it's the delimiter for the --import flag.
528
+ [ / % 2 2 / g, '"' ] , // We can decode the double quotes to improve readability.
529
+ [ / % 2 F / ig, '/' ] , // We can decode the slashes to improve readability.
530
+ ] ,
531
+ ( str , { 0 : regex , 1 : replacement } ) => RegExpPrototypeSymbolReplace ( hardenRegExp ( regex ) , str , replacement ) ,
532
+ encodeURIComponent ( string ) ) ;
533
+ process . emitWarning (
534
+ '`--experimental-loader` may be removed in the future; instead use `register()`:\n' +
535
+ `--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; ${ ArrayPrototypeJoin (
536
+ ArrayPrototypeMap ( userLoaderPaths , ( loader ) => `register(${ readableURIEncode ( JSONStringify ( loader ) ) } , pathToFileURL("./"))` ) ,
537
+ '; ' ,
538
+ ) } ;'`,
539
+ 'ExperimentalWarning' ,
540
+ ) ;
541
+ emittedLoaderFlagWarning = true ;
520
542
}
521
543
customizations = new CustomizedModuleLoader ( ) ;
522
544
}
0 commit comments