From 8ec39537d9130483165a40cd9d0025e4abf6c1f7 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 13 Aug 2023 16:37:32 +0200 Subject: [PATCH] esm: fix `globalPreload` warning PR-URL: https://github.com/nodejs/node/pull/49069 Fixes: https://github.com/nodejs/node/issues/49026 Reviewed-By: Chemi Atlow Reviewed-By: Jacob Smith --- lib/internal/modules/esm/hooks.js | 10 +++------- test/es-module/test-esm-loader-hooks.mjs | 14 +++++++++++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/internal/modules/esm/hooks.js b/lib/internal/modules/esm/hooks.js index e4ee2db18a0899..abba6b51a05939 100644 --- a/lib/internal/modules/esm/hooks.js +++ b/lib/internal/modules/esm/hooks.js @@ -85,7 +85,6 @@ let importMetaInitializer; // [2] `validate...()`s throw the wrong error -let globalPreloadWarned = false; class Hooks { #chains = { /** @@ -161,12 +160,9 @@ class Hooks { } = pluckHooks(exports); if (globalPreload && !initialize) { - if (globalPreloadWarned === false) { - globalPreloadWarned = true; - emitExperimentalWarning( - '`globalPreload` will be removed in a future version. Please use `initialize` instead.', - ); - } + emitExperimentalWarning( + '`globalPreload` is planned for removal in favor of `initialize`. `globalPreload`', + ); ArrayPrototypePush(this.#chains.globalPreload, { __proto__: null, fn: globalPreload, url }); } if (resolve) { diff --git a/test/es-module/test-esm-loader-hooks.mjs b/test/es-module/test-esm-loader-hooks.mjs index 4684f813a16dce..633e4d07b3744c 100644 --- a/test/es-module/test-esm-loader-hooks.mjs +++ b/test/es-module/test-esm-loader-hooks.mjs @@ -372,10 +372,22 @@ describe('Loader hooks', { concurrency: true }, () => { const { stderr } = await spawnPromisified(execPath, [ '--experimental-loader', 'data:text/javascript,export function globalPreload(){}', + '--experimental-loader', + 'data:text/javascript,export function globalPreload(){return""}', + fixtures.path('empty.js'), + ]); + + assert.strictEqual(stderr.match(/`globalPreload` is an experimental feature/g).length, 1); + }); + + it('should not emit deprecation warning when initialize is supplied', async () => { + const { stderr } = await spawnPromisified(execPath, [ + '--experimental-loader', + 'data:text/javascript,export function globalPreload(){}export function initialize(){}', fixtures.path('empty.js'), ]); - assert.match(stderr, /`globalPreload` will be removed/); + assert.doesNotMatch(stderr, /`globalPreload` is an experimental feature/); }); it('should handle globalPreload returning undefined', async () => {