Skip to content

Commit

Permalink
New: Support .mjs extension (closes gulpjs#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 3, 2020
1 parent a19a44a commit 5aaf332
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Map file types to modules which provide a [require.extensions] loader.
'.litcoffee': ['coffeescript/register', 'coffee-script/register', 'coffeescript', 'coffee-script'],
'.liticed': 'iced-coffee-script/register',
'.ls': ['livescript', 'LiveScript'],
'.mjs': '/absolute/path/to/interpret/mjs-stub.js',
'.node': null,
'.toml': {
module: 'toml-require',
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -193,6 +196,7 @@ var jsVariantExtensions = [
'.litcoffee',
'.liticed',
'.ls',
'.mjs',
'.ts',
'.tsx',
'.wisp',
Expand Down
1 change: 1 addition & 0 deletions mjs-stub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require.extensions['.mjs'] = null;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"main": "index.js",
"files": [
"LICENSE",
"index.js"
"index.js",
"mjs-stub.js"
],
"scripts": {
"lint": "eslint .",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/mjs/0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
9 changes: 9 additions & 0 deletions test/fixtures/mjs/0/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1
}
}
};
1 change: 1 addition & 0 deletions test/fixtures/mjs/1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
9 changes: 9 additions & 0 deletions test/fixtures/mjs/1/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1
}
}
};
69 changes: 68 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 about 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();
});

});

0 comments on commit 5aaf332

Please sign in to comment.