Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 90fdc58

Browse files
fix: fallback directory resolution to module extensions (#50)
1 parent 54e4e40 commit 90fdc58

File tree

11 files changed

+138
-1
lines changed

11 files changed

+138
-1
lines changed

src/loaders.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,18 @@ async function tryDirectory(
6969
context: Context,
7070
defaultResolve: resolve,
7171
) {
72-
const appendIndex = specifier.endsWith('/') ? 'index' : '/index';
72+
const isExplicitDirectory = specifier.endsWith('/');
73+
const appendIndex = isExplicitDirectory ? 'index' : '/index';
7374

7475
try {
7576
return await tryExtensions(specifier + appendIndex, context, defaultResolve);
7677
} catch (error: any) {
78+
if (!isExplicitDirectory) {
79+
try {
80+
return await tryExtensions(specifier, context, defaultResolve);
81+
} catch {}
82+
}
83+
7784
const { message } = error;
7885
error.message = error.message.replace(`${appendIndex.replace('/', path.sep)}'`, "'");
7986
error.stack = error.stack.replace(message, error.message);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

tests/specs/javascript/esm.ts

+24
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,30 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
131131
expect(nodeProcess.stdout).toMatch('{"default":1234}');
132132
});
133133
});
134+
135+
describe('empty directory should fallback to file', ({ test }) => {
136+
const importPath = './lib/esm-ext-js/index';
137+
138+
test('Load', async () => {
139+
const nodeProcess = await node.load(importPath);
140+
assertResults(nodeProcess);
141+
});
142+
143+
test('Import', async () => {
144+
const nodeProcess = await node.import(importPath);
145+
assertResults(nodeProcess);
146+
expect(nodeProcess.stdout).toMatch('{"default":1234}');
147+
});
148+
});
149+
150+
describe('empty but explicit directory should not fallback to file', ({ test }) => {
151+
const importPath = './lib/esm-ext-js/index/';
152+
153+
test('Import', async () => {
154+
const nodeProcess = await node.import(importPath);
155+
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
156+
});
157+
});
134158
});
135159
});
136160
});

tests/specs/json.ts

+24
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,29 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
4747
expect(nodeProcess.stdout).toMatch('{"default":{"loaded":"json"},"loaded":"json"}');
4848
});
4949
});
50+
51+
describe('empty directory should fallback to file', ({ test }) => {
52+
const importPath = './lib/json/index';
53+
54+
test('Load', async () => {
55+
const nodeProcess = await node.load(importPath);
56+
expect(nodeProcess.exitCode).toBe(0);
57+
expect(nodeProcess.stdout).toBe('');
58+
});
59+
60+
test('Import', async () => {
61+
const nodeProcess = await node.import(importPath);
62+
expect(nodeProcess.stdout).toMatch('{"default":{"loaded":"json"},"loaded":"json"}');
63+
});
64+
});
65+
66+
describe('empty but explicit directory should not fallback to file', ({ test }) => {
67+
const importPath = './lib/json/index/';
68+
69+
test('Import', async () => {
70+
const nodeProcess = await node.import(importPath);
71+
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
72+
});
73+
});
5074
});
5175
});

tests/specs/typescript/jsx.ts

+24
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,29 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
6363
expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
6464
});
6565
});
66+
67+
describe('empty directory should fallback to file', ({ test }) => {
68+
const importPath = './lib/ts-ext-jsx/index';
69+
70+
test('Load', async () => {
71+
const nodeProcess = await node.load(importPath);
72+
assertResults(nodeProcess.stdout);
73+
});
74+
75+
test('Import', async () => {
76+
const nodeProcess = await node.import(importPath);
77+
assertResults(nodeProcess.stdout);
78+
expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
79+
});
80+
});
81+
82+
describe('empty but explicit directory should not fallback to file', ({ test }) => {
83+
const importPath = './lib/ts-ext-jsx/index/';
84+
85+
test('Import', async () => {
86+
const nodeProcess = await node.import(importPath);
87+
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
88+
});
89+
});
6690
});
6791
});

tests/specs/typescript/ts.ts

+24
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,29 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
103103
expect(nodeProcess.stdout).toMatch('{"default":1234}');
104104
});
105105
});
106+
107+
describe('empty directory should fallback to file', ({ test }) => {
108+
const importPath = './lib/ts-ext-ts/index';
109+
110+
test('Load', async () => {
111+
const nodeProcess = await node.load(importPath);
112+
assertResults(nodeProcess.stdout);
113+
});
114+
115+
test('Import', async () => {
116+
const nodeProcess = await node.import(importPath);
117+
assertResults(nodeProcess.stdout);
118+
expect(nodeProcess.stdout).toMatch('{"default":1234}');
119+
});
120+
});
121+
122+
describe('empty but explicit directory should not fallback to file', ({ test }) => {
123+
const importPath = './lib/ts-ext-ts/index/';
124+
125+
test('Import', async () => {
126+
const nodeProcess = await node.import(importPath);
127+
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
128+
});
129+
});
106130
});
107131
});

tests/specs/typescript/tsx.ts

+24
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,29 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
6363
expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
6464
});
6565
});
66+
67+
describe('empty directory should fallback to file', ({ test }) => {
68+
const importPath = './lib/ts-ext-tsx/index';
69+
70+
test('Load', async () => {
71+
const nodeProcess = await node.load(importPath);
72+
assertResults(nodeProcess.stdout);
73+
});
74+
75+
test('Import', async () => {
76+
const nodeProcess = await node.import(importPath);
77+
assertResults(nodeProcess.stdout);
78+
expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
79+
});
80+
});
81+
82+
describe('empty but explicit directory should not fallback to file', ({ test }) => {
83+
const importPath = './lib/ts-ext-tsx/index/';
84+
85+
test('Import', async () => {
86+
const nodeProcess = await node.import(importPath);
87+
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
88+
});
89+
});
6690
});
6791
});

0 commit comments

Comments
 (0)