From afe7c29c9fecfbe75b3bbc420382d797aa35d699 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Tue, 2 Jun 2020 22:20:32 -0700 Subject: [PATCH] New: Support .mjs extension (closes #65) --- index.js | 3 ++ mjs-stub.js | 1 + package.json | 3 +- test/fixtures/mjs/0/package.json | 1 + test/fixtures/mjs/0/test.mjs | 9 +++++ test/fixtures/mjs/1/package.json | 1 + test/fixtures/mjs/1/test.mjs | 9 +++++ test/index.js | 69 +++++++++++++++++++++++++++++++- 8 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 mjs-stub.js create mode 100644 test/fixtures/mjs/0/package.json create mode 100644 test/fixtures/mjs/0/test.mjs create mode 100644 test/fixtures/mjs/1/package.json create mode 100644 test/fixtures/mjs/1/test.mjs diff --git a/index.js b/index.js index 40de0dc..18b1276 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,8 @@ var path = require('path'); var endsInBabelJs = /\.babel\.[jt]s(x)$/; +var mjsStub = path.join(__dirname, 'mjs-stub'); + function ignoreNonBabelAndNodeModules(file) { return !endsInBabelJs.test(file) && path.relative(process.cwd(), file).split(path.sep).indexOf('node_modules') >= 0; @@ -130,6 +132,7 @@ var extensions = { '.litcoffee': ['coffeescript/register', 'coffee-script/register', 'coffeescript', 'coffee-script'], '.liticed': 'iced-coffee-script/register', '.ls': ['livescript', 'LiveScript'], + '.mjs': mjsStub, '.node': null, '.toml': { module: 'toml-require', diff --git a/mjs-stub.js b/mjs-stub.js new file mode 100644 index 0000000..6a1af95 --- /dev/null +++ b/mjs-stub.js @@ -0,0 +1 @@ +require.extensions['.mjs'] = null; diff --git a/package.json b/package.json index 3ca9f32..a19af20 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "main": "index.js", "files": [ "LICENSE", - "index.js" + "index.js", + "mjs-stub.js" ], "scripts": { "lint": "eslint .", diff --git a/test/fixtures/mjs/0/package.json b/test/fixtures/mjs/0/package.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/mjs/0/package.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/mjs/0/test.mjs b/test/fixtures/mjs/0/test.mjs new file mode 100644 index 0000000..4389305 --- /dev/null +++ b/test/fixtures/mjs/0/test.mjs @@ -0,0 +1,9 @@ +export default { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1 + } + } +}; diff --git a/test/fixtures/mjs/1/package.json b/test/fixtures/mjs/1/package.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/fixtures/mjs/1/package.json @@ -0,0 +1 @@ +{} diff --git a/test/fixtures/mjs/1/test.mjs b/test/fixtures/mjs/1/test.mjs new file mode 100644 index 0000000..09ad00b --- /dev/null +++ b/test/fixtures/mjs/1/test.mjs @@ -0,0 +1,9 @@ +module.exports = { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1 + } + } +}; diff --git a/test/index.js b/test/index.js index d16f571..9308011 100644 --- a/test/index.js +++ b/test/index.js @@ -84,6 +84,11 @@ describe('interpret.extensions', function() { return attempts; } + // We will handle the .mjs tests separately + if (ext === '.mjs') { + return attempts; + } + modules.forEach(function(mod, idx) { if (mod && typeof mod !== 'string') { mod = mod.module; @@ -220,9 +225,71 @@ describe('interpret.extensions', function() { }; expect(require(fixture)).toEqual(expected); } - done(); }); }); + it('does not error with the .mjs extension', function(done) { + var ext = '.mjs'; + var fixture = './fixtures/' + ext.slice(1) + '/0/test' + ext; + + var result = rechoir.prepare(extensions, fixture); + + expect(Array.isArray(result)).toEqual(true); + expect(result[0].moduleName).toEqual(path.join(__dirname, '../mjs-stub')); + done(); + }); + + it('the module can be loaded with import(), ignoring the loader', function() { + if (nodeVersion.major < 12) { + this.skip(); + } + + var ext = '.mjs'; + var fixture = './fixtures/' + ext.slice(1) + '/0/test' + ext; + + rechoir.prepare(extensions, fixture); + + var expected = { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1, + }, + }, + }; + + // This avoid SyntaxError when parsing on old node versions + var imprt = new Function('a', 'return import(a)'); + + return imprt(fixture) + .then(function(result) { + expect(result.default).toEqual(expected); + }); + }); + + it('stubs .mjs extension with null on old node that do not care it', function(done) { + if (nodeVersion.major > 10) { + this.skip(); + } + + var ext = '.mjs'; + var fixture = './fixtures/' + ext.slice(1) + '/1/test' + ext; + + rechoir.prepare(extensions, fixture); + + var expected = { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1, + }, + }, + }; + expect(require(fixture)).toEqual(expected); + done(); + }); + });