Skip to content

Commit c1f74b1

Browse files
esm: emit experimental warnings in common place
1 parent 457567f commit c1f74b1

File tree

5 files changed

+44
-35
lines changed

5 files changed

+44
-35
lines changed

lib/internal/modules/esm/loader.js

Lines changed: 13 additions & 0 deletions
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,
@@ -107,6 +108,18 @@ class ESMLoader {
107108
*/
108109
translators = translators;
109110

111+
constructor() {
112+
if (getOptionValue('--experimental-loader')) {
113+
emitExperimentalWarning('Custom ESM Loaders');
114+
}
115+
if (getOptionValue('--experimental-network-imports')) {
116+
emitExperimentalWarning('Network Imports');
117+
}
118+
if (getOptionValue('--experimental-specifier-resolution') === 'node') {
119+
emitExperimentalWarning('Specifier Resolution');
120+
}
121+
}
122+
110123
static pluckHooks({
111124
globalPreload,
112125
resolve,

lib/internal/modules/esm/resolve.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ function resolveDirectoryEntry(search) {
362362
}
363363

364364
const encodedSepRegEx = /%2F|%5C/i;
365-
let experimentalSpecifierResolutionWarned = false;
366365
/**
367366
* @param {URL} resolved
368367
* @param {string | URL | undefined} base
@@ -377,13 +376,6 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
377376

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

389381
// Directory

lib/internal/process/esm_loader.js

Lines changed: 0 additions & 3 deletions
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() + '/';
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import { fileURL } from '../common/fixtures.mjs';
3+
import { ok, 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 child = spawn(execPath, [
16+
arg,
17+
'--input-type=module',
18+
'--eval',
19+
`const foo = 'a'`,
20+
]);
21+
22+
let stderr = '';
23+
child.stderr.setEncoding('utf8');
24+
child.stderr.on('data', (data) => { stderr += data });
25+
child.on('close', mustCall((code, signal) => {
26+
strictEqual(code, 0);
27+
strictEqual(signal, null);
28+
ok(stderr.includes('ExperimentalWarning:'));
29+
ok(stderr.includes(experiment), new Error(`Expected warning to mention ${experiment}`));
30+
}));
31+
}

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

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)