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

Commit

Permalink
fix(typescript): fallback to checking cjs/mjs on missing cts/mts (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored May 16, 2022
1 parent 02cca31 commit 1c7b38d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export const resolve: resolve = async function (
}

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

if (tsExtensionsPattern.test(specifier)) {
Expand Down
9 changes: 9 additions & 0 deletions tests/specs/javascript/cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
expect(nodeProcess.stderr).toMatch('Cannot find module \'node:');
}
});

test('TypeScript Import', async () => {
const nodeProcess = await node.import(importPath, { typescript: true });
if (semver.satisfies(node.version, nodeSupportsNodePrefixRequire)) {
expect(nodeProcess.stdout).toBe(`${output}\n{"default":1234}`);
} else {
expect(nodeProcess.stderr).toMatch('Cannot find module \'node:');
}
});
});

describe('extensionless - should not work', ({ test }) => {
Expand Down
5 changes: 5 additions & 0 deletions tests/specs/javascript/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stdout).toBe(`${output}\n{"default":1234}`);
});

test('TypeScript Import', async () => {
const nodeProcess = await node.import(importPath, { typescript: true });
expect(nodeProcess.stdout).toBe(`${output}\n{"default":1234}`);
});
});

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

0 comments on commit 1c7b38d

Please sign in to comment.