esm: emit experimental warnings in common place

PR-URL: https://github.com/nodejs/node/pull/42314
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
This commit is contained in:
Jacob Smith authored and Juan José Arboleda committed 2022-04-06 12:20:12 -05:00
1 parent 2f682091cd
commit 902776e674
5 files changed
+56 -35

No files matched your search

+24
View File
@@ -31,6 +31,7 @@ const {
ERR_UNKNOWN_MODULE_FORMAT
} = require('internal/errors').codes;
const { pathToFileURL, isURLInstance, URL } = require('internal/url');
const { emitExperimentalWarning } = require('internal/util');
const {
isAnyArrayBuffer,
isArrayBufferView,
@@ -53,6 +54,13 @@ const {
fetchModule,
} = require('internal/modules/esm/fetch_module');
/**
* Prevent the specifier resolution warning from being printed twice
*/
let emittedSpecifierResolutionWarning = false;
/**
* An ESMLoader instance is used as the main entry point for loading ES modules.
* Currently, this is a singleton -- there is only one used for loading
@@ -107,6 +115,22 @@ class ESMLoader {
*/
translators = translators;
constructor() {
if (getOptionValue('--experimental-loader')) {
emitExperimentalWarning('Custom ESM Loaders');
}
if (getOptionValue('--experimental-network-imports')) {
emitExperimentalWarning('Network Imports');
}
if (getOptionValue('--experimental-specifier-resolution') === 'node' && !emittedSpecifierResolutionWarning) {
process.emitWarning(
'The Node.js specifier resolution flag is experimental. It could change or be removed at any time.',
'ExperimentalWarning'
);
emittedSpecifierResolutionWarning = true;
}
}
static pluckHooks({
globalPreload,
resolve,
-8
View File
@@ -363,7 +363,6 @@ function resolveDirectoryEntry(search) {
}
const encodedSepRegEx = /%2F|%5C/i;
let experimentalSpecifierResolutionWarned = false;
/**
* @param {URL} resolved
* @param {string | URL | undefined} base
@@ -378,13 +377,6 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
let path = fileURLToPath(resolved);
if (getOptionValue('--experimental-specifier-resolution') === 'node') {
if (!experimentalSpecifierResolutionWarned) {
process.emitWarning(
'The Node.js specifier resolution flag is experimental. It could change or be removed at any time.',
'ExperimentalWarning');
experimentalSpecifierResolutionWarned = true;
}
let file = resolveExtensionsWithTryExactName(resolved);
// Directory
-3
View File
@@ -55,9 +55,6 @@ async function initializeLoader() {
if (!customLoaders.length) return;
const { emitExperimentalWarning } = require('internal/util');
emitExperimentalWarning('--experimental-loader');
let cwd;
try {
cwd = process.cwd() + '/';
@@ -0,0 +1,32 @@
import { mustCall } from '../common/index.mjs';
import { fileURL } from '../common/fixtures.mjs';
import { match, strictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
// Verify experimental warnings are printed
for (
const [experiment, arg] of [
[/Custom ESM Loaders/, `--experimental-loader=${fileURL('es-module-loaders', 'hooks-custom.mjs')}`],
[/Network Imports/, '--experimental-network-imports'],
[/specifier resolution/, '--experimental-specifier-resolution=node'],
]
) {
const input = `import ${JSON.stringify(fileURL('es-module-loaders', 'module-named-exports.mjs'))}`;
const child = spawn(execPath, [
arg,
'--input-type=module',
'--eval',
input,
]);
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => { stderr += data; });
child.on('close', mustCall((code, signal) => {
strictEqual(code, 0);
strictEqual(signal, null);
match(stderr, /ExperimentalWarning/);
match(stderr, experiment);
}));
}
@@ -1,24 +0,0 @@
import { mustCall } from '../common/index.mjs';
import { fileURL } from '../common/fixtures.mjs';
import { match, strictEqual } from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
// Verify experimental warning is printed
const child = spawn(execPath, [
'--experimental-specifier-resolution=node',
'--input-type=module',
'--eval',
`import ${JSON.stringify(fileURL('es-module-specifiers', 'package-type-module'))}`,
]);
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
});
child.on('close', mustCall((code, signal) => {
strictEqual(code, 0);
strictEqual(signal, null);
match(stderr, /ExperimentalWarning: The Node\.js specifier resolution flag is experimental/);
}));