Skip to content

Commit

Permalink
fix(cjs): implicit resolution to correctly try extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Jun 28, 2024
1 parent d120d14 commit b94482d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/cjs/api/resolve-implicit-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ const tryExtensions = (
...args: Parameters<ResolveFilename>
) => {
for (const extension of implicitlyResolvableExtensions) {
const newArgs = args.slice() as Parameters<ResolveFilename>;
newArgs[0] += extension;

try {
args[0] += extension;
return resolve(...args);
return resolve(...newArgs);
} catch {}
}
};
Expand Down
10 changes: 9 additions & 1 deletion tests/specs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,24 @@ export default testSuite(({ describe }, node: NodeApis) => {
const { message, async } = api.require('./file', __filename);
console.log(message);
async.then(m => console.log(m.default));
api.require('./tsx?query=1', __filename);
api.require('./jsx', __filename);
api.require('./dir?query=3', __filename);
`,
...tsFiles,

'tsx.tsx': 'console.log(\'tsx\');',
'jsx.jsx': 'console.log(\'jsx\');',
'dir/index.jsx': 'console.log(\'dir\');',
});

const { stdout } = await execaNode(fixture.getPath('require.cjs'), [], {
nodePath: node.path,
nodeOptions: [],
});

expect(stdout).toBe('foo bar json file.ts\nasync');
expect(stdout).toBe('foo bar json file.ts\ntsx\njsx\ndir\nasync');
});
});
});
Expand Down

0 comments on commit b94482d

Please sign in to comment.