GeezerVison is showing github.com — esm: update loaders warning · nodejs/node@12cb700
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js +26-4Lines changed: 26 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 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 { | @@ -486,7 +493,7 @@ class CustomizedModuleLoader { | 486 | 493 | } | 487 | 494 | } | 488 | 495 |
| 489 | | -let emittedExperimentalWarning = false; | | 496 | +let emittedLoaderFlagWarning = false; | 490 | 497 | /** | 491 | 498 | * A loader instance is used as the main entry point for loading ES modules. Currently, this is a singleton; there is | 492 | 499 | * only one used for loading the main module and everything in its dependency graph, though separate instances of this | @@ -502,9 +509,24 @@ function createModuleLoader(useCustomLoadersIfPresent = true) { | 502 | 509 | !require('internal/modules/esm/utils').isLoaderWorker()) { | 503 | 510 | const userLoaderPaths = getOptionValue('--experimental-loader'); | 504 | 511 | if (userLoaderPaths.length > 0) { | 505 | | - if (!emittedExperimentalWarning) { | 506 | | - emitExperimentalWarning('Custom ESM Loaders'); | 507 | | - emittedExperimentalWarning = true; | | 512 | + if (!emittedLoaderFlagWarning) { | | 513 | + const readableURIEncode = (string) => ArrayPrototypeReduce( | | 514 | + [ | | 515 | + [/'/g, '%27'], // We need to URL-encode the single quote as it's the delimiter for the --import flag. | | 516 | + [/%22/g, '"'], // We can decode the double quotes to improve readability. | | 517 | + [/%2F/ig, '/'], // We can decode the slashes to improve readability. | | 518 | + ], | | 519 | + (str, { 0: regex, 1: replacement }) => RegExpPrototypeSymbolReplace(hardenRegExp(regex), str, replacement), | | 520 | + encodeURIComponent(string)); | | 521 | + process.emitWarning( | | 522 | + '`--experimental-loader` may be removed in the future; instead use `register()`:\n' + | | 523 | + `--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; ${ArrayPrototypeJoin( | | 524 | + ArrayPrototypeMap(userLoaderPaths, (loader) => `register(${readableURIEncode(JSONStringify(loader))}, pathToFileURL("./"))`), | | 525 | + '; ', | | 526 | + )};'`, | | 527 | + 'ExperimentalWarning', | | 528 | + ); | | 529 | + emittedLoaderFlagWarning = true; | 508 | 530 | } | 509 | 531 | customizations = new CustomizedModuleLoader(); | 510 | 532 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-experimental-warnings.mjs +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,15 +24,19 @@ describe('ESM: warn for obsolete hooks provided', { concurrency: true }, () => { | 24 | 24 |
| 25 | 25 | describe('experimental warnings for enabled experimental feature', () => { | 26 | 26 | for ( | 27 | | - const [experiment, arg] of [ | 28 | | - [/Custom ESM Loaders/, `--experimental-loader=${fileURL('es-module-loaders', 'hooks-custom.mjs')}`], | | 27 | + const [experiment, ...args] of [ | | 28 | + [ | | 29 | + /`--experimental-loader` may be removed in the future/, | | 30 | + '--experimental-loader', | | 31 | + fileURL('es-module-loaders', 'hooks-custom.mjs'), | | 32 | + ], | 29 | 33 | [/Network Imports/, '--experimental-network-imports'], | 30 | 34 | [/specifier resolution/, '--experimental-specifier-resolution=node'], | 31 | 35 | ] | 32 | 36 | ) { | 33 | 37 | it(`should print for ${experiment.toString().replaceAll('/', '')}`, async () => { | 34 | 38 | const { code, signal, stderr } = await spawnPromisified(execPath, [ | 35 | | - arg, | | 39 | + ...args, | 36 | 40 | '--input-type=module', | 37 | 41 | '--eval', | 38 | 42 | `import ${JSON.stringify(fileURL('es-module-loaders', 'module-named-exports.mjs'))}`, | |
|