Skip to content

Commit f49cbee

Browse files
authored
Merge pull request #58 from BoolJS/develop
Allow importing ESM modules
2 parents a62fa0c + dcbac8f commit f49cbee

8 files changed

Lines changed: 909 additions & 457 deletions

File tree

.mocharc.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
colors: true
2+
diff: true
3+
timeout: 10000
4+
slow: 1000
5+
spec:
6+
- test/**/*.js
7+
package: package.json
8+
recursive: true
9+
exit: true

.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Mocha Tests",
11+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
12+
"internalConsoleOptions": "openOnSessionStart",
13+
"skipFiles": [
14+
"<node_internals>/**"
15+
]
16+
}
17+
]
18+
}

lib/api/configurations/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports.readFiles = async function (instance, route, files) {
1717
'The file name format is invalid');
1818
} else {
1919
instance.getComponents().configuration
20-
.set(info.name, await new Reader().readStatic(info.ext, filename));
20+
.set(info.name, await Reader.readStatic(info.ext, filename));
2121
}
2222
}
2323

lib/readers/defaults/javascript.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,35 @@ const { Error } = require('@booljs/api');
1010
*/
1111

1212
module.exports = async function (route) {
13-
const imported = require(route);
13+
try {
14+
const imported = require(route);
1415

15-
if (imported !== undefined) {
16-
return imported;
17-
}
16+
if (imported !== undefined) {
17+
return imported;
18+
}
19+
20+
throw new Error(0, 'EMODNOTFOUND', 'The required module wasn\'t found');
21+
} catch (error) {
22+
if (error.code === 'ERR_REQUIRE_ESM') {
23+
return importEsmModule(route);
24+
}
1825

19-
throw new Error(0, 'EMODNOTFOUND', 'The required module wasn\'t found');
26+
throw error;
27+
}
2028
};
29+
30+
/**
31+
* @private
32+
* @description ESM reader for bool.js
33+
* @param {String route} - The file's route
34+
* @returns {Promise<Object>}
35+
*/
36+
async function importEsmModule (route) {
37+
const imported = await import(route);
38+
39+
if (imported.default) {
40+
return imported.default;
41+
}
42+
43+
return imported;
44+
}

lib/readers/defaults/yaml.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const YAML = require('yaml');
4+
5+
/**
6+
* @private
7+
* @description JSON reader for bool.js
8+
* @param {String} route - The file's route
9+
* @returns {Promise}
10+
*/
11+
module.exports = async function (route) {
12+
return YAML.parse(route);
13+
};

lib/readers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
module.exports = class Readers {
1010
static staticReaders = {
11+
'.yaml': require('./defaults/yaml'),
1112
'.json': require('./defaults/json'),
1213
'.cson': require('./defaults/cson')
1314
};

0 commit comments

Comments
 (0)