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

Commit 1c7b38d

Browse files
fix(typescript): fallback to checking cjs/mjs on missing cts/mts (#5)
1 parent 02cca31 commit 1c7b38d

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/loaders.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ export const resolve: resolve = async function (
5252
}
5353

5454
/**
55-
* Typescript 4.6.0 behavior seems to be that if `.mjs` is specified,
56-
* it converts it to mts without testing if it exists, and without
57-
* consideration for whether a file with .mjs exists
55+
* Typescript gives .mts or .cts priority over actual .mjs or .cjs extensions
5856
*/
5957
if (
6058
/\.[cm]js$/.test(specifier)
6159
&& tsExtensionsPattern.test(context.parentURL!)
6260
) {
63-
specifier = `${specifier.slice(0, -2)}ts`;
61+
try {
62+
return await resolve(`${specifier.slice(0, -2)}ts`, context, defaultResolve);
63+
} catch {}
6464
}
6565

6666
if (tsExtensionsPattern.test(specifier)) {

tests/specs/javascript/cjs.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
3030
expect(nodeProcess.stderr).toMatch('Cannot find module \'node:');
3131
}
3232
});
33+
34+
test('TypeScript Import', async () => {
35+
const nodeProcess = await node.import(importPath, { typescript: true });
36+
if (semver.satisfies(node.version, nodeSupportsNodePrefixRequire)) {
37+
expect(nodeProcess.stdout).toBe(`${output}\n{"default":1234}`);
38+
} else {
39+
expect(nodeProcess.stderr).toMatch('Cannot find module \'node:');
40+
}
41+
});
3342
});
3443

3544
describe('extensionless - should not work', ({ test }) => {

tests/specs/javascript/esm.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
1919
const nodeProcess = await node.import(importPath);
2020
expect(nodeProcess.stdout).toBe(`${output}\n{"default":1234}`);
2121
});
22+
23+
test('TypeScript Import', async () => {
24+
const nodeProcess = await node.import(importPath, { typescript: true });
25+
expect(nodeProcess.stdout).toBe(`${output}\n{"default":1234}`);
26+
});
2227
});
2328

2429
describe('extensionless - should not work', ({ test }) => {

0 commit comments

Comments
 (0)