From b5da2f4dad721edd8fc979e1f75b2812d62567e7 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 ca20a2253b93e0..b7e1afac31060d 100644 --- a/lib/internal/modules/esm/hooks.js +++ b/lib/internal/modules/esm/hooks.js @@ -86,7 +86,6 @@ let importMetaInitializer; // [2] `validate...()`s throw the wrong error -let globalPreloadWarned = false; class Hooks { #chains = { /** @@ -162,12 +161,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 9358d49c9c1e0c..a230c417edc75a 100644 --- a/test/es-module/test-esm-loader-hooks.mjs +++ b/test/es-module/test-esm-loader-hooks.mjs @@ -424,10 +424,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 () => {