From 5e7d26ec0035607bab9abf1247f5666764c1497a Mon Sep 17 00:00:00 2001 From: FrankQiu Date: Thu, 21 Oct 2021 22:43:56 +0800 Subject: [PATCH 1/6] module: refactor get_format --- lib/internal/modules/esm/get_format.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 7c07e5b1f7ce17..2b6f1858b6ba78 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -60,20 +60,15 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), { if (ext === '.js') { format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs'; } else { - format = extensionFormatMap[ext]; + format = extensionFormatMap[ext] || legacyExtensionFormatMap[ext] || null; } - if (!format) { - if (experimentalSpecifierResolution === 'node') { - process.emitWarning( - 'The Node.js specifier resolution in ESM is experimental.', - 'ExperimentalWarning'); - format = legacyExtensionFormatMap[ext]; - } else { - throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)); - } - } - - return format || null; + if (!extensionFormatMap[ext] && experimentalSpecifierResolution === 'node') + process.emitWarning( + 'The Node.js specifier resolution in ESM is experimental.', + 'ExperimentalWarning'); + else + throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)); + return format; }, 'node:'() { return 'builtin'; }, }); From c52be618e6be0fb91032b53a0feffed6220e7abc Mon Sep 17 00:00:00 2001 From: FrankQiu Date: Thu, 21 Oct 2021 22:56:22 +0800 Subject: [PATCH 2/6] module: fix bug --- lib/internal/modules/esm/get_format.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 2b6f1858b6ba78..344deb61ac1d4b 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -62,12 +62,15 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), { } else { format = extensionFormatMap[ext] || legacyExtensionFormatMap[ext] || null; } - if (!extensionFormatMap[ext] && experimentalSpecifierResolution === 'node') - process.emitWarning( - 'The Node.js specifier resolution in ESM is experimental.', - 'ExperimentalWarning'); - else - throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)); + if (!extensionFormatMap[ext]) { + if (experimentalSpecifierResolution === 'node') + process.emitWarning( + 'The Node.js specifier resolution in ESM is experimental.', + 'ExperimentalWarning'); + else + throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)); + } + return format; }, 'node:'() { return 'builtin'; }, From 1d612d49ca127f2e1fd6179174aef83a3039f85d Mon Sep 17 00:00:00 2001 From: FrankQiu Date: Wed, 27 Oct 2021 22:38:33 +0800 Subject: [PATCH 3/6] esm: add a condition to if --- lib/internal/modules/esm/get_format.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 344deb61ac1d4b..3da0cf48aa168c 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -55,20 +55,26 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), { }, 'file:'(parsed, url) { const ext = extname(parsed.pathname); - let format; + let format = null; if (ext === '.js') { format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs'; } else { - format = extensionFormatMap[ext] || legacyExtensionFormatMap[ext] || null; + format = extensionFormatMap[ext]; } - if (!extensionFormatMap[ext]) { - if (experimentalSpecifierResolution === 'node') + if (!format) { + if ( + experimentalSpecifierResolution === 'node' && + legacyExtensionFormatMap[ext] + ) { process.emitWarning( 'The Node.js specifier resolution in ESM is experimental.', - 'ExperimentalWarning'); - else + 'ExperimentalWarning' + ); + format = legacyExtensionFormatMap[ext]; + } else { throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)); + } } return format; From d781db949dd894ec80489d1790d603c75208ae6f Mon Sep 17 00:00:00 2001 From: FrankQiu Date: Tue, 2 Nov 2021 14:52:50 +0800 Subject: [PATCH 4/6] module: add some tests --- test/es-module/test-esm-get-format.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 test/es-module/test-esm-get-format.js diff --git a/test/es-module/test-esm-get-format.js b/test/es-module/test-esm-get-format.js new file mode 100644 index 00000000000000..cb5f71bf99f445 --- /dev/null +++ b/test/es-module/test-esm-get-format.js @@ -0,0 +1,13 @@ +require('../common'); +const assert = require('assert'); +const { defaultGetFormat} = require('internal/modules/esm/get_format'); + +{ + const url = new URL('file://example.com/foo/bar.js') + assert.strictEqual(defaultGetFormat(url), 'commonjs') +} + +{ + const url = new URL('file://example.com/foo/bar.whatever') + assert.throws(() => defaultGetFormat(url), {name: 'TypeError', message: /Unknown file extension whatever/}) +} From e54325a39c74cf53fc972a850dafbe88028fcdcd Mon Sep 17 00:00:00 2001 From: FrankQiu Date: Wed, 3 Nov 2021 13:39:22 +0800 Subject: [PATCH 5/6] module: fix lint err --- test/es-module/test-esm-get-format.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/es-module/test-esm-get-format.js b/test/es-module/test-esm-get-format.js index cb5f71bf99f445..e711b57615c3a4 100644 --- a/test/es-module/test-esm-get-format.js +++ b/test/es-module/test-esm-get-format.js @@ -1,13 +1,14 @@ +'use strict'; require('../common'); const assert = require('assert'); -const { defaultGetFormat} = require('internal/modules/esm/get_format'); +const { defaultGetFormat } = require('internal/modules/esm/get_format'); { - const url = new URL('file://example.com/foo/bar.js') - assert.strictEqual(defaultGetFormat(url), 'commonjs') + const url = new URL('file://example.com/foo/bar.js'); + assert.strictEqual(defaultGetFormat(url), 'commonjs'); } { - const url = new URL('file://example.com/foo/bar.whatever') - assert.throws(() => defaultGetFormat(url), {name: 'TypeError', message: /Unknown file extension whatever/}) + const url = new URL('file://example.com/foo/bar.whatever'); + assert.throws(() => defaultGetFormat(url), { name: 'TypeError', message: /Unknown file extension whatever/ }); } From 8d330e26714be59ce5ad767db2543a5f96eb5a41 Mon Sep 17 00:00:00 2001 From: FrankQiu Date: Mon, 29 Nov 2021 23:20:51 +0800 Subject: [PATCH 6/6] module: fix lint err --- lib/internal/modules/esm/get_format.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 3da0cf48aa168c..0683113f0106db 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -69,8 +69,7 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), { ) { process.emitWarning( 'The Node.js specifier resolution in ESM is experimental.', - 'ExperimentalWarning' - ); + 'ExperimentalWarning'); format = legacyExtensionFormatMap[ext]; } else { throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url));