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

Commit 0634f90

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

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

Diff for: src/loaders.ts

+38-4
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,60 @@ 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 alternativePaths = mappedExtensions[''].map(alternativeExtension => ({
48+
specifier,
49+
specifierExtension: '',
50+
alternativeExtension,
51+
}));
52+
const specifierExtension = path.extname(specifier);
53+
if (!specifierExtension) {
54+
return alternativePaths;
55+
}
56+
const extensionsToTry = mappedExtensions[specifierExtension] ?? [];
57+
specifier = specifier.slice(0, specifier.length - specifierExtension.length);
58+
return [
59+
// Try using subextension first
60+
...alternativePaths,
61+
...extensionsToTry.map(alternativeExtension => ({
62+
specifier,
63+
specifierExtension,
64+
alternativeExtension,
65+
})),
66+
];
67+
}
3968

4069
async function tryExtensions(
4170
specifier: string,
4271
context: Context,
4372
defaultResolve: resolve,
4473
) {
4574
let error;
46-
for (const extension of extensions) {
75+
for (const alternativePath of resolveTsAlternativeSpecifiers(specifier)) {
76+
const {
77+
specifier: alternativeSpecifier,
78+
specifierExtension,
79+
alternativeExtension,
80+
} = alternativePath;
4781
try {
4882
return await resolve(
49-
specifier + extension,
83+
`${alternativeSpecifier}${alternativeExtension}`,
5084
context,
5185
defaultResolve,
5286
true,
5387
);
5488
} catch (_error: any) {
5589
if (error === undefined) {
5690
const { message } = _error;
57-
_error.message = _error.message.replace(`${extension}'`, "'");
91+
_error.message = _error.message.replace(`${alternativeExtension}'`, `${specifierExtension}'`);
5892
_error.stack = _error.stack.replace(message, _error.message);
5993
error = _error;
6094
}

Diff for: tests/specs/typescript/cts.ts

+2-2
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 () => {

Diff for: tests/specs/typescript/mts.ts

+1-1
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 () => {

Diff for: tests/specs/typescript/ts.ts

+1-1
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)