Skip to content

Commit 45d517d

Browse files
committed
readableURIEncode
1 parent 27c14d7 commit 45d517d

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/internal/modules/esm/loader.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ require('internal/modules/cjs/loader');
66
const {
77
ArrayPrototypeJoin,
88
ArrayPrototypeMap,
9-
encodeURI,
9+
ArrayPrototypeReduce,
1010
FunctionPrototypeCall,
1111
JSONStringify,
1212
ObjectSetPrototypeOf,
13+
RegExpPrototypeSymbolReplace,
1314
SafeWeakMap,
15+
encodeURIComponent,
16+
hardenRegExp,
1417
} = primordials;
1518

1619
const {
@@ -520,10 +523,18 @@ function createModuleLoader(useCustomLoadersIfPresent = true) {
520523
const userLoaderPaths = getOptionValue('--experimental-loader');
521524
if (userLoaderPaths.length > 0) {
522525
if (!emittedLoaderFlagWarning) {
526+
const readableURIEncode = (string) => ArrayPrototypeReduce(
527+
[
528+
[/'/g, '%27'], // We need to URL-encode the single quote as it's the delimiter for the --import flag.
529+
[/%22/g, '"'], // We can decode the double quotes to improve readability.
530+
[/%2F/ig, '/'], // We can decode the slashes to improve readability.
531+
],
532+
(str, { 0: regex, 1: replacement }) => RegExpPrototypeSymbolReplace(hardenRegExp(regex), str, replacement),
533+
encodeURIComponent(string));
523534
process.emitWarning(
524535
'`--experimental-loader` may be removed in the future; instead use `register()`:\n' +
525536
`--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; ${ArrayPrototypeJoin(
526-
ArrayPrototypeMap(userLoaderPaths, (loader) => `register(${JSONStringify(encodeURI(loader))}, pathToFileURL("./"))`),
537+
ArrayPrototypeMap(userLoaderPaths, (loader) => `register(${readableURIEncode(JSONStringify(loader))}, pathToFileURL("./"))`),
527538
'; ',
528539
)};'`,
529540
'ExperimentalWarning',

0 commit comments

Comments
 (0)