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

Commit 73f899d

Browse files
committed
try to load typescript fallback for specifier with extensions.
1 parent d50ddac commit 73f899d

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

src/loaders.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,52 @@ type resolve = (
3535
recursiveCall?: boolean,
3636
) => MaybePromise<Resolved>;
3737

38-
const extensions = ['.js', '.json', '.ts', '.tsx', '.jsx'] as const;
38+
const mappedExtensions: Record<string, string[]> = {
39+
'.js': ['.ts', '.tsx'],
40+
'.jsx': ['.ts', '.tsx'],
41+
'.cjs': ['.cts'],
42+
'.mjs': ['.mts'],
43+
'': ['.ts', '.js', '.json', '.tsx', '.jsx'],
44+
};
45+
46+
function resolveTsAlternativeSpecifiers(specifier: string) {
47+
const specifierExtension = path.extname(specifier);
48+
let extensionsToTry = mappedExtensions[specifierExtension] ?? [];
49+
if (specifierExtension) {
50+
specifier = specifier.slice(0, specifier.length - specifierExtension.length);
51+
extensionsToTry = [
52+
// Try subextension first
53+
...mappedExtensions[''].map(extension => `${specifierExtension}${extension}`),
54+
...extensionsToTry,
55+
];
56+
}
57+
return extensionsToTry.map(alternativeExtension => ({
58+
specifier,
59+
specifierExtension,
60+
altenativeSpecifier: `${specifier}${alternativeExtension}`,
61+
alternativeExtension,
62+
}));
63+
}
3964

4065
async function tryExtensions(
4166
specifier: string,
4267
context: Context,
4368
defaultResolve: resolve,
4469
) {
4570
let error;
46-
for (const extension of extensions) {
71+
for (const alternativePath of resolveTsAlternativeSpecifiers(specifier)) {
72+
const { specifierExtension, altenativeSpecifier, alternativeExtension } = alternativePath;
4773
try {
4874
return await resolve(
49-
specifier + extension,
75+
altenativeSpecifier,
5076
context,
5177
defaultResolve,
5278
true,
5379
);
5480
} catch (_error: any) {
5581
if (error === undefined) {
5682
const { message } = _error;
57-
_error.message = _error.message.replace(`${extension}'`, "'");
83+
_error.message = _error.message.replace(`${alternativeExtension}'`, `${specifierExtension}'`);
5884
_error.stack = _error.stack.replace(message, _error.message);
5985
error = _error;
6086
}

tests/specs/typescript/cts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
2828
describe('full path via .cjs', ({ test }) => {
2929
const importPath = './lib/ts-ext-cts/index.cjs';
3030

31-
test('Load - should not work', async () => {
31+
test('Load', async () => {
3232
const nodeProcess = await node.load(importPath);
33-
expect(nodeProcess.stderr).toMatch('Cannot find module');
33+
expect(nodeProcess.stderr).toMatch('SyntaxError: Unexpected token \':\'');
3434
});
3535

3636
test('Import', async () => {

tests/specs/typescript/mts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
4141

4242
test('Load - should not work', async () => {
4343
const nodeProcess = await node.load(importPath);
44-
expect(nodeProcess.stderr).toMatch('Cannot find module');
44+
assertResults(nodeProcess.stdout);
4545
});
4646

4747
test('Import', async () => {

tests/specs/typescript/ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
4949

5050
test('Load - should not work', async () => {
5151
const nodeProcess = await node.load(importPath);
52-
expect(nodeProcess.stderr).toMatch('Cannot find module');
52+
assertResults(nodeProcess.stdout, 'ts-ext-ts/index.ts');
5353
});
5454

5555
test('Import', async () => {

0 commit comments

Comments
 (0)