From bfc3e7f01f8838d901bdc44883e95c0dbd2a6cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Fri, 29 Nov 2019 16:30:25 -0500 Subject: [PATCH 1/3] lib: add warning on dynamic import es modules --- lib/internal/process/esm_loader.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/internal/process/esm_loader.js b/lib/internal/process/esm_loader.js index 19099d9cbd58fd..3b9fe98c301381 100644 --- a/lib/internal/process/esm_loader.js +++ b/lib/internal/process/esm_loader.js @@ -24,6 +24,9 @@ exports.initializeImportMetaObject = function(wrap, meta) { exports.importModuleDynamicallyCallback = async function(wrap, specifier) { assert(calledInitialize === true || !userLoader); + process.emitWarning( + 'The ESM module loader is experimental.', + 'ExperimentalWarning', undefined); const { callbackMap } = internalBinding('module_wrap'); if (callbackMap.has(wrap)) { const { importModuleDynamically } = callbackMap.get(wrap); From 21813e384d10523a02345a053321fafa3b5e53b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Sun, 1 Dec 2019 00:14:39 -0500 Subject: [PATCH 2/3] fixup --- test/es-module/test-esm-dynamic-import.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/es-module/test-esm-dynamic-import.js b/test/es-module/test-esm-dynamic-import.js index 8a92ac41edbdfd..edc3b5cab4039f 100644 --- a/test/es-module/test-esm-dynamic-import.js +++ b/test/es-module/test-esm-dynamic-import.js @@ -42,6 +42,14 @@ function expectFsNamespace(result) { // For direct use of import expressions inside of CJS or ES modules, including // via eval, all kinds of specifiers should work without issue. (function testScriptOrModuleImport() { + // Warning should be emitted 9 times, bellow we're importing 9 ES modules. + const IMPORTED_DYNAMICALLY = 9; + process.on('warning', common.mustCall(({ name, code, message }) => { + assert.strictEqual(code, undefined); + assert.strictEqual(message, 'The ESM module loader is experimental.'); + assert.strictEqual(name, 'ExperimentalWarning'); + }, IMPORTED_DYNAMICALLY)); + // Importing another file, both direct & via eval // expectOkNamespace(import(relativePath)); expectOkNamespace(eval(`import("${relativePath}")`)); From 79e5c6b0ca134c5b5c89900008c6f618ce3b5ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Mon, 2 Dec 2019 18:14:44 -0500 Subject: [PATCH 3/3] fixup --- lib/internal/process/esm_loader.js | 9 ++++++--- test/es-module/test-esm-dynamic-import.js | 9 ++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/internal/process/esm_loader.js b/lib/internal/process/esm_loader.js index 3b9fe98c301381..404be77338bfe1 100644 --- a/lib/internal/process/esm_loader.js +++ b/lib/internal/process/esm_loader.js @@ -24,9 +24,12 @@ exports.initializeImportMetaObject = function(wrap, meta) { exports.importModuleDynamicallyCallback = async function(wrap, specifier) { assert(calledInitialize === true || !userLoader); - process.emitWarning( - 'The ESM module loader is experimental.', - 'ExperimentalWarning', undefined); + if (!calledInitialize) { + process.emitWarning( + 'The ESM module loader is experimental.', + 'ExperimentalWarning', undefined); + calledInitialize = true; + } const { callbackMap } = internalBinding('module_wrap'); if (callbackMap.has(wrap)) { const { importModuleDynamically } = callbackMap.get(wrap); diff --git a/test/es-module/test-esm-dynamic-import.js b/test/es-module/test-esm-dynamic-import.js index edc3b5cab4039f..1fc22f1d98934a 100644 --- a/test/es-module/test-esm-dynamic-import.js +++ b/test/es-module/test-esm-dynamic-import.js @@ -42,13 +42,8 @@ function expectFsNamespace(result) { // For direct use of import expressions inside of CJS or ES modules, including // via eval, all kinds of specifiers should work without issue. (function testScriptOrModuleImport() { - // Warning should be emitted 9 times, bellow we're importing 9 ES modules. - const IMPORTED_DYNAMICALLY = 9; - process.on('warning', common.mustCall(({ name, code, message }) => { - assert.strictEqual(code, undefined); - assert.strictEqual(message, 'The ESM module loader is experimental.'); - assert.strictEqual(name, 'ExperimentalWarning'); - }, IMPORTED_DYNAMICALLY)); + common.expectWarning('ExperimentalWarning', + 'The ESM module loader is experimental.'); // Importing another file, both direct & via eval // expectOkNamespace(import(relativePath));