Skip to content

Commit

Permalink
fix: support TS resolution in JS files when allowJs is set (#535)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
  • Loading branch information
nwalters512 and privatenumber committed Apr 25, 2024
1 parent 8109677 commit 081853e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/cjs/index.ts
Expand Up @@ -205,7 +205,10 @@ const resolveTsFilename = (

if (
parent?.filename
&& isTsFilePatten.test(parent.filename)
&& (
isTsFilePatten.test(parent.filename)
|| tsconfig?.config.compilerOptions?.allowJs
)
&& tsPath
) {
for (const tryTsPath of tsPath) {
Expand Down
13 changes: 6 additions & 7 deletions src/esm/loaders.ts
Expand Up @@ -17,6 +17,7 @@ import {
isJsonPattern,
getFormatFromFileUrl,
fileProtocol,
allowJs,
type MaybePromise,
type NodeError,
} from './utils.js';
Expand Down Expand Up @@ -166,13 +167,11 @@ export const resolve: resolve = async (
}
}

/**
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
*/
if (
// !recursiveCall &&
tsExtensionsPattern.test(context.parentURL!)
) {
// Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
//
// If `allowJs` is set in `tsconfig.json`, then we'll apply the same resolution logic
// to files without a TypeScript extension.
if (tsExtensionsPattern.test(context.parentURL!) || allowJs) {
const tsPaths = resolveTsPath(specifier);
if (tsPaths) {
for (const tsPath of tsPaths) {
Expand Down
1 change: 1 addition & 0 deletions src/esm/utils.ts
Expand Up @@ -19,6 +19,7 @@ const tsconfig = (

export const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
export const allowJs = tsconfig?.config.compilerOptions?.allowJs ?? false;

export const fileProtocol = 'file://';

Expand Down
19 changes: 19 additions & 0 deletions tests/specs/smoke.ts
Expand Up @@ -218,6 +218,14 @@ const files = {

'file.txt': 'hello',

'import-typescript-parent.js': sourcemap.tag`
import './import-typescript-child.js';
`,

'import-typescript-child.ts': sourcemap.tag`
console.log('imported');
`,

node_modules: {
'pkg-commonjs': {
'package.json': JSON.stringify({
Expand Down Expand Up @@ -663,6 +671,17 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
expect(pTsconfigAllowJs.stderr).toMatch('Error: No error thrown');
expect(pTsconfigAllowJs.stdout).toBe('');
});

test('allowJs in tsconfig.json', async ({ onTestFail }) => {
const pTsconfigAllowJs = await tsx(['--tsconfig', 'tsconfig/tsconfig-allowJs.json', 'import-typescript-parent.js'], fixture.path);
onTestFail((error) => {
console.error(error);
console.log(pTsconfigAllowJs);
});
expect(pTsconfigAllowJs.failed).toBe(false);
expect(pTsconfigAllowJs.stderr).toBe('');
expect(pTsconfigAllowJs.stdout).toBe('imported');
});
});
});
}
Expand Down

0 comments on commit 081853e

Please sign in to comment.