Skip to content

Commit 5dccdfb

Browse files
davidensingerJounQin
authored andcommitted
fix: add support for tsconfig.json paths imports with .js extension for tsx importee
1 parent 7d63ec7 commit 5dccdfb

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/index.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,25 @@ function getMappedPaths(
362362
const isJs = JS_EXT_PATTERN.test(source)
363363
if (isJs) {
364364
const jsExt = path.extname(source)
365+
// cjs -> cts, js -> ts, jsx -> tsx, mjs -> mts
365366
const tsExt = jsExt.replace('js', 'ts')
367+
366368
const basename = source.replace(JS_EXT_PATTERN, '')
367369

368-
const mappedPaths = getMappedPaths(basename + tsExt, file)
370+
let resolved = getMappedPaths(basename + tsExt, file)
369371

370-
const resolved =
371-
mappedPaths.length > 0
372-
? mappedPaths
373-
: getMappedPaths(
374-
basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt),
375-
file,
376-
)
372+
if (resolved.length === 0 && jsExt === '.js') {
373+
// js -> tsx
374+
const tsxExt = jsExt.replace('js', 'tsx')
375+
resolved = getMappedPaths(basename + tsxExt, file)
376+
}
377+
378+
if (resolved.length === 0) {
379+
resolved = getMappedPaths(
380+
basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt),
381+
file,
382+
)
383+
}
377384

378385
if (resolved.length > 0) {
379386
return resolved

tests/withPaths/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import './subfolder/tsxImportee'
66

77
// import using tsconfig.json path mapping
88
import 'folder/tsImportee'
9+
import 'folder/tsImportee.js'
910
import 'folder/tsxImportee'
11+
import 'folder/tsxImportee.js'
1012
import 'folder/subfolder/tsImportee'
13+
import 'folder/subfolder/tsImportee.js'
1114
import 'folder/subfolder/tsxImportee'
15+
import 'folder/subfolder/tsxImportee.js'
1216

1317
// import module with typings set in package.json
1418
import 'folder/module'

0 commit comments

Comments
 (0)