Skip to content

Commit eb86fd1

Browse files
committed
feature: @putout/engine-loader: async-loader: require.resolve
1 parent 3141d2a commit eb86fd1

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

packages/engine-loader/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ yarn-error.log
44

55
coverage
66
.idea
7+

packages/engine-loader/lib/load/async-loader.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ async function cleverLoad(names, load = simpleImport) {
4747
if (!e)
4848
return reporter;
4949

50+
if (e.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
51+
const fullName = require.resolve(name);
52+
53+
[e, reporter] = await tryToCatch(load, fullName);
54+
55+
if (!e)
56+
return reporter;
57+
}
58+
5059
if (e.code === 'ERR_MODULE_NOT_FOUND')
5160
continue;
5261

packages/engine-loader/lib/load/async-loader.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,23 @@ test('putout: engine-loader: async-loader: PUTOUT_LOAD_DIR', async (t) => {
103103
t.equal(result, expected);
104104
t.end();
105105
});
106+
107+
test('putout: engine-loader: async-loader: PUTOUT_LOAD_DIR: node_modules', async (t) => {
108+
process.env.PUTOUT_LOAD_DIR = join(__dirname, 'fixture');
109+
110+
const {createAsyncLoader} = reRequire('./async-loader');
111+
const loadAsync = createAsyncLoader('plugin');
112+
113+
const {report} = await loadAsync('world');
114+
115+
stopAll();
116+
reRequire('./async-loader');
117+
118+
delete process.env.PUTOUT_LOAD_DIR;
119+
120+
const result = report();
121+
const expected = 'hello';
122+
123+
t.equal(result, expected);
124+
t.end();
125+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.report = () => 'hello';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": "index.js"
3+
}

0 commit comments

Comments
 (0)