Skip to content

Commit 081853e

Browse files
fix: support TS resolution in JS files when allowJs is set (#535)
Co-authored-by: Hiroki Osame <[email protected]>
1 parent 8109677 commit 081853e

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

src/cjs/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ const resolveTsFilename = (
205205

206206
if (
207207
parent?.filename
208-
&& isTsFilePatten.test(parent.filename)
208+
&& (
209+
isTsFilePatten.test(parent.filename)
210+
|| tsconfig?.config.compilerOptions?.allowJs
211+
)
209212
&& tsPath
210213
) {
211214
for (const tryTsPath of tsPath) {

src/esm/loaders.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
isJsonPattern,
1818
getFormatFromFileUrl,
1919
fileProtocol,
20+
allowJs,
2021
type MaybePromise,
2122
type NodeError,
2223
} from './utils.js';
@@ -166,13 +167,11 @@ export const resolve: resolve = async (
166167
}
167168
}
168169

169-
/**
170-
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
171-
*/
172-
if (
173-
// !recursiveCall &&
174-
tsExtensionsPattern.test(context.parentURL!)
175-
) {
170+
// Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
171+
//
172+
// If `allowJs` is set in `tsconfig.json`, then we'll apply the same resolution logic
173+
// to files without a TypeScript extension.
174+
if (tsExtensionsPattern.test(context.parentURL!) || allowJs) {
176175
const tsPaths = resolveTsPath(specifier);
177176
if (tsPaths) {
178177
for (const tsPath of tsPaths) {

src/esm/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const tsconfig = (
1919

2020
export const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
2121
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
22+
export const allowJs = tsconfig?.config.compilerOptions?.allowJs ?? false;
2223

2324
export const fileProtocol = 'file://';
2425

tests/specs/smoke.ts

+19
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ const files = {
218218

219219
'file.txt': 'hello',
220220

221+
'import-typescript-parent.js': sourcemap.tag`
222+
import './import-typescript-child.js';
223+
`,
224+
225+
'import-typescript-child.ts': sourcemap.tag`
226+
console.log('imported');
227+
`,
228+
221229
node_modules: {
222230
'pkg-commonjs': {
223231
'package.json': JSON.stringify({
@@ -663,6 +671,17 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
663671
expect(pTsconfigAllowJs.stderr).toMatch('Error: No error thrown');
664672
expect(pTsconfigAllowJs.stdout).toBe('');
665673
});
674+
675+
test('allowJs in tsconfig.json', async ({ onTestFail }) => {
676+
const pTsconfigAllowJs = await tsx(['--tsconfig', 'tsconfig/tsconfig-allowJs.json', 'import-typescript-parent.js'], fixture.path);
677+
onTestFail((error) => {
678+
console.error(error);
679+
console.log(pTsconfigAllowJs);
680+
});
681+
expect(pTsconfigAllowJs.failed).toBe(false);
682+
expect(pTsconfigAllowJs.stderr).toBe('');
683+
expect(pTsconfigAllowJs.stdout).toBe('imported');
684+
});
666685
});
667686
});
668687
}

0 commit comments

Comments
 (0)