From 6398be7b28a2115f34db2ed23d810132bc46d1cc Mon Sep 17 00:00:00 2001 From: "Sharon (Sean) Rolel" Date: Mon, 29 Jun 2020 07:14:32 -0400 Subject: [PATCH 1/2] treat modules with no extension as .js --- lib/internal/modules/esm/get_format.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 616b2cf52309ea..950bf0ad9b995a 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -50,7 +50,7 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) { } else if (parsed.protocol === 'file:') { const ext = extname(parsed.pathname); let format; - if (ext === '.js') { + if (!ext || ext === '.js') { format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs'; } else { format = extensionFormatMap[ext]; From e3b12330615f7d0aeeab827937681d6a7db2eee7 Mon Sep 17 00:00:00 2001 From: "Sharon (Sean) Rolel" Date: Thu, 2 Jul 2020 18:36:59 -0400 Subject: [PATCH 2/2] update tests --- test/es-module/test-esm-no-extension.js | 34 +++++++++++++++++++ ...nsion.js => test-esm-unknown-extension.js} | 6 ++-- 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 test/es-module/test-esm-no-extension.js rename test/es-module/{test-esm-unknown-or-no-extension.js => test-esm-unknown-extension.js} (83%) diff --git a/test/es-module/test-esm-no-extension.js b/test/es-module/test-esm-no-extension.js new file mode 100644 index 00000000000000..12c004893c7f15 --- /dev/null +++ b/test/es-module/test-esm-no-extension.js @@ -0,0 +1,34 @@ +'use strict'; + +const common = require('../common'); +const fixtures = require('../common/fixtures'); +const { spawn } = require('child_process'); +const assert = require('assert'); + +// In a "type": "module" package scope, files with no extensions should +// not throw; both when used as a main entry point and also when +// referenced via `import`. + +[ + '/es-modules/package-type-module/noext-esm', + '/es-modules/package-type-module/imports-noext.mjs', +].forEach((fixturePath) => { + const entry = fixtures.path(fixturePath); + const child = spawn(process.execPath, [entry]); + let stdout = ''; + let stderr = ''; + child.stderr.setEncoding('utf8'); + child.stdout.setEncoding('utf8'); + child.stdout.on('data', (data) => { + stdout += data; + }); + child.stderr.on('data', (data) => { + stderr += data; + }); + child.on('close', common.mustCall((code, signal) => { + assert.strictEqual(code, 0); + assert.strictEqual(signal, null); + assert.strictEqual(stdout, 'executed\n'); + assert.ok(stderr.indexOf('ERR_UNKNOWN_FILE_EXTENSION') === -1); + })); +}); diff --git a/test/es-module/test-esm-unknown-or-no-extension.js b/test/es-module/test-esm-unknown-extension.js similarity index 83% rename from test/es-module/test-esm-unknown-or-no-extension.js rename to test/es-module/test-esm-unknown-extension.js index 3b1802a4dcedbd..efcf636369d608 100644 --- a/test/es-module/test-esm-unknown-or-no-extension.js +++ b/test/es-module/test-esm-unknown-extension.js @@ -5,13 +5,11 @@ const fixtures = require('../common/fixtures'); const { spawn } = require('child_process'); const assert = require('assert'); -// In a "type": "module" package scope, files with unknown extensions or no -// extensions should throw; both when used as a main entry point and also when +// In a "type": "module" package scope, files with unknown extensions +// should throw; both when used as a main entry point and also when // referenced via `import`. [ - '/es-modules/package-type-module/noext-esm', - '/es-modules/package-type-module/imports-noext.mjs', '/es-modules/package-type-module/extension.unknown', '/es-modules/package-type-module/imports-unknownext.mjs', ].forEach((fixturePath) => {