Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: emit experimental warnings in common place #42314

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 0 additions & 8 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ function resolveDirectoryEntry(search) {
}

const encodedSepRegEx = /%2F|%5C/i;
let experimentalSpecifierResolutionWarned = false;
/**
* @param {URL} resolved
* @param {string | URL | undefined} base
Expand All @@ -379,13 +378,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.',
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
'ExperimentalWarning');
experimentalSpecifierResolutionWarned = true;
}

let file = resolveExtensionsWithTryExactName(resolved);

// Directory
Expand Down
3 changes: 0 additions & 3 deletions lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() + '/';
Expand Down
32 changes: 32 additions & 0 deletions test/es-module/test-esm-experimental-warnings.mjs
Original file line number Diff line number Diff line change
@@ -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);
}));
}
24 changes: 0 additions & 24 deletions test/es-module/test-esm-specifiers-legacy-flag-warning.mjs

This file was deleted.