Skip to content

Commit

Permalink
esm: emit experimental warnings in common place
Browse files Browse the repository at this point in the history
PR-URL: nodejs#42314
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
  • Loading branch information
JakobJingleheimer authored and juanarbol committed Apr 4, 2022
1 parent 8bb04f5 commit eea25dc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 35 deletions.
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 @@ -363,7 +363,6 @@ function resolveDirectoryEntry(search) {
}

const encodedSepRegEx = /%2F|%5C/i;
let experimentalSpecifierResolutionWarned = false;
/**
* @param {URL} resolved
* @param {string | URL | undefined} base
Expand All @@ -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
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.

0 comments on commit eea25dc

Please sign in to comment.