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

Commit

Permalink
fix: fallback directory resolution to module extensions (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 29, 2022
1 parent 54e4e40 commit 90fdc58
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ async function tryDirectory(
context: Context,
defaultResolve: resolve,
) {
const appendIndex = specifier.endsWith('/') ? 'index' : '/index';
const isExplicitDirectory = specifier.endsWith('/');
const appendIndex = isExplicitDirectory ? 'index' : '/index';

try {
return await tryExtensions(specifier + appendIndex, context, defaultResolve);
} catch (error: any) {
if (!isExplicitDirectory) {
try {
return await tryExtensions(specifier, context, defaultResolve);
} catch {}
}

const { message } = error;
error.message = error.message.replace(`${appendIndex.replace('/', path.sep)}'`, "'");
error.stack = error.stack.replace(message, error.message);
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/package-module/lib/esm-ext-js/index/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 2 additions & 0 deletions tests/fixtures/package-module/lib/json/index/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 2 additions & 0 deletions tests/fixtures/package-module/lib/ts-ext-jsx/index/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 2 additions & 0 deletions tests/fixtures/package-module/lib/ts-ext-ts/index/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 2 additions & 0 deletions tests/fixtures/package-module/lib/ts-ext-tsx/index/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
24 changes: 24 additions & 0 deletions tests/specs/javascript/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});
});

describe('empty directory should fallback to file', ({ test }) => {
const importPath = './lib/esm-ext-js/index';

test('Load', async () => {
const nodeProcess = await node.load(importPath);
assertResults(nodeProcess);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath);
assertResults(nodeProcess);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});
});

describe('empty but explicit directory should not fallback to file', ({ test }) => {
const importPath = './lib/esm-ext-js/index/';

test('Import', async () => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
});
});
});
});
});
24 changes: 24 additions & 0 deletions tests/specs/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,29 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
expect(nodeProcess.stdout).toMatch('{"default":{"loaded":"json"},"loaded":"json"}');
});
});

describe('empty directory should fallback to file', ({ test }) => {
const importPath = './lib/json/index';

test('Load', async () => {
const nodeProcess = await node.load(importPath);
expect(nodeProcess.exitCode).toBe(0);
expect(nodeProcess.stdout).toBe('');
});

test('Import', async () => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stdout).toMatch('{"default":{"loaded":"json"},"loaded":"json"}');
});
});

describe('empty but explicit directory should not fallback to file', ({ test }) => {
const importPath = './lib/json/index/';

test('Import', async () => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
});
});
});
});
24 changes: 24 additions & 0 deletions tests/specs/typescript/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,29 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
});
});

describe('empty directory should fallback to file', ({ test }) => {
const importPath = './lib/ts-ext-jsx/index';

test('Load', async () => {
const nodeProcess = await node.load(importPath);
assertResults(nodeProcess.stdout);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath);
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
});
});

describe('empty but explicit directory should not fallback to file', ({ test }) => {
const importPath = './lib/ts-ext-jsx/index/';

test('Import', async () => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
});
});
});
});
24 changes: 24 additions & 0 deletions tests/specs/typescript/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,29 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});
});

describe('empty directory should fallback to file', ({ test }) => {
const importPath = './lib/ts-ext-ts/index';

test('Load', async () => {
const nodeProcess = await node.load(importPath);
assertResults(nodeProcess.stdout);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath);
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});
});

describe('empty but explicit directory should not fallback to file', ({ test }) => {
const importPath = './lib/ts-ext-ts/index/';

test('Import', async () => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
});
});
});
});
24 changes: 24 additions & 0 deletions tests/specs/typescript/tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,29 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
});
});

describe('empty directory should fallback to file', ({ test }) => {
const importPath = './lib/ts-ext-tsx/index';

test('Load', async () => {
const nodeProcess = await node.load(importPath);
assertResults(nodeProcess.stdout);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath);
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":["div",null,"hello world"]}');
});
});

describe('empty but explicit directory should not fallback to file', ({ test }) => {
const importPath = './lib/ts-ext-tsx/index/';

test('Import', async () => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stderr).toMatch('ERR_MODULE_NOT_FOUND');
});
});
});
});

0 comments on commit 90fdc58

Please sign in to comment.