Skip to content

Commit eea25dc

Browse files
JakobJingleheimerjuanarbol
authored andcommitted
esm: emit experimental warnings in common place
PR-URL: #42314 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent 8bb04f5 commit eea25dc

File tree

5 files changed

+56
-35
lines changed

5 files changed

+56
-35
lines changed

lib/internal/modules/esm/loader.js

+24
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131
ERR_UNKNOWN_MODULE_FORMAT
3232
} = require('internal/errors').codes;
3333
const { pathToFileURL, isURLInstance, URL } = require('internal/url');
34+
const { emitExperimentalWarning } = require('internal/util');
3435
const {
3536
isAnyArrayBuffer,
3637
isArrayBufferView,
@@ -53,6 +54,13 @@ const {
5354
fetchModule,
5455
} = require('internal/modules/esm/fetch_module');
5556

57+
58+
/**
59+
* Prevent the specifier resolution warning from being printed twice
60+
*/
61+
let emittedSpecifierResolutionWarning = false;
62+
63+
5664
/**
5765
* An ESMLoader instance is used as the main entry point for loading ES modules.
5866
* Currently, this is a singleton -- there is only one used for loading
@@ -107,6 +115,22 @@ class ESMLoader {
107115
*/
108116
translators = translators;
109117

118+
constructor() {
119+
if (getOptionValue('--experimental-loader')) {
120+
emitExperimentalWarning('Custom ESM Loaders');
121+
}
122+
if (getOptionValue('--experimental-network-imports')) {
123+
emitExperimentalWarning('Network Imports');
124+
}
125+
if (getOptionValue('--experimental-specifier-resolution') === 'node' && !emittedSpecifierResolutionWarning) {
126+
process.emitWarning(
127+
'The Node.js specifier resolution flag is experimental. It could change or be removed at any time.',
128+
'ExperimentalWarning'
129+
);
130+
emittedSpecifierResolutionWarning = true;
131+
}
132+
}
133+
110134
static pluckHooks({
111135
globalPreload,
112136
resolve,

lib/internal/modules/esm/resolve.js

-8
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ function resolveDirectoryEntry(search) {
363363
}
364364

365365
const encodedSepRegEx = /%2F|%5C/i;
366-
let experimentalSpecifierResolutionWarned = false;
367366
/**
368367
* @param {URL} resolved
369368
* @param {string | URL | undefined} base
@@ -378,13 +377,6 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
378377

379378
let path = fileURLToPath(resolved);
380379
if (getOptionValue('--experimental-specifier-resolution') === 'node') {
381-
if (!experimentalSpecifierResolutionWarned) {
382-
process.emitWarning(
383-
'The Node.js specifier resolution flag is experimental. It could change or be removed at any time.',
384-
'ExperimentalWarning');
385-
experimentalSpecifierResolutionWarned = true;
386-
}
387-
388380
let file = resolveExtensionsWithTryExactName(resolved);
389381

390382
// Directory

lib/internal/process/esm_loader.js

-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ async function initializeLoader() {
5555

5656
if (!customLoaders.length) return;
5757

58-
const { emitExperimentalWarning } = require('internal/util');
59-
emitExperimentalWarning('--experimental-loader');
60-
6158
let cwd;
6259
try {
6360
cwd = process.cwd() + '/';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import { fileURL } from '../common/fixtures.mjs';
3+
import { match, strictEqual } from 'assert';
4+
import { spawn } from 'child_process';
5+
import { execPath } from 'process';
6+
7+
// Verify experimental warnings are printed
8+
for (
9+
const [experiment, arg] of [
10+
[/Custom ESM Loaders/, `--experimental-loader=${fileURL('es-module-loaders', 'hooks-custom.mjs')}`],
11+
[/Network Imports/, '--experimental-network-imports'],
12+
[/specifier resolution/, '--experimental-specifier-resolution=node'],
13+
]
14+
) {
15+
const input = `import ${JSON.stringify(fileURL('es-module-loaders', 'module-named-exports.mjs'))}`;
16+
const child = spawn(execPath, [
17+
arg,
18+
'--input-type=module',
19+
'--eval',
20+
input,
21+
]);
22+
23+
let stderr = '';
24+
child.stderr.setEncoding('utf8');
25+
child.stderr.on('data', (data) => { stderr += data; });
26+
child.on('close', mustCall((code, signal) => {
27+
strictEqual(code, 0);
28+
strictEqual(signal, null);
29+
match(stderr, /ExperimentalWarning/);
30+
match(stderr, experiment);
31+
}));
32+
}

test/es-module/test-esm-specifiers-legacy-flag-warning.mjs

-24
This file was deleted.

0 commit comments

Comments
 (0)