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

Commit 85e27f5

Browse files
fix(typescript): allow subextensions (#21)
1 parent ab24e0c commit 85e27f5

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/loaders.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ type resolve = (
3333
specifier: string,
3434
context: Context,
3535
defaultResolve: resolve,
36+
recursiveCall?: boolean,
3637
) => MaybePromise<Resolved>;
3738

38-
const hasExtensionPattern = /\.\w+$/;
39-
4039
const extensions = ['.js', '.json', '.ts', '.tsx', '.jsx'] as const;
4140

4241
async function tryExtensions(
@@ -51,6 +50,7 @@ async function tryExtensions(
5150
specifier + extension,
5251
context,
5352
defaultResolve,
53+
true,
5454
);
5555
} catch (_error: any) {
5656
if (error === undefined) {
@@ -86,6 +86,7 @@ export const resolve: resolve = async function (
8686
specifier,
8787
context,
8888
defaultResolve,
89+
resursiveCall,
8990
) {
9091
// Added in v12.20.0
9192
// https://nodejs.org/api/esm.html#esm_node_imports
@@ -106,7 +107,7 @@ export const resolve: resolve = async function (
106107

107108
if (tsPath) {
108109
try {
109-
return await resolve(tsPath, context, defaultResolve);
110+
return await resolve(tsPath, context, defaultResolve, true);
110111
} catch (error) {
111112
if ((error as any).code !== 'ERR_MODULE_NOT_FOUND') {
112113
throw error;
@@ -119,15 +120,15 @@ export const resolve: resolve = async function (
119120
try {
120121
resolved = await defaultResolve(specifier, context, defaultResolve);
121122
} catch (error) {
122-
if (error instanceof Error) {
123+
if (
124+
(error instanceof Error)
125+
&& !resursiveCall
126+
) {
123127
if ((error as any).code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
124128
return await tryDirectory(specifier, context, defaultResolve);
125129
}
126130

127-
if (
128-
(error as any).code === 'ERR_MODULE_NOT_FOUND'
129-
&& !hasExtensionPattern.test(specifier)
130-
) {
131+
if ((error as any).code === 'ERR_MODULE_NOT_FOUND') {
131132
return await tryExtensions(specifier, context, defaultResolve);
132133
}
133134
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import fs from 'node:fs';
2+
3+
console.log(
4+
'loaded ts-ext-ts/index.tsx.ts',
5+
JSON.stringify({
6+
nodePrefix: Boolean(fs),
7+
hasDynamicImport: Boolean(import('fs')),
8+
...(() => {
9+
let nameInError;
10+
try {
11+
nameInError();
12+
} catch (error) {
13+
return {
14+
nameInError: error.message.includes('nameInError'),
15+
sourceMap: error.stack.includes(':11:5'),
16+
};
17+
}
18+
})(),
19+
}),
20+
);
21+
22+
function valueNumber(value: number) {
23+
return value;
24+
}
25+
26+
export default valueNumber(1234);

tests/specs/typescript/ts.ts

+15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
4747
});
4848
});
4949

50+
describe('extensionless with subextension', ({ test }) => {
51+
const importPath = './lib/ts-ext-ts/index.tsx';
52+
const outputSubextension = 'loaded ts-ext-ts/index.tsx.ts {"nodePrefix":true,"hasDynamicImport":true,"nameInError":true,"sourceMap":true}';
53+
54+
test('Load', async () => {
55+
const nodeProcess = await node.load(importPath);
56+
expect(nodeProcess.stdout).toBe(outputSubextension);
57+
});
58+
59+
test('Import', async () => {
60+
const nodeProcess = await node.import(importPath);
61+
expect(nodeProcess.stdout).toBe(`${outputSubextension}\n{"default":1234}`);
62+
});
63+
});
64+
5065
describe('directory', ({ test }) => {
5166
const importPath = './lib/ts-ext-ts';
5267

0 commit comments

Comments
 (0)