Skip to content

Commit 93339ca

Browse files
committed
refactor: workaround for privatenumber/get-tsconfig#19
1 parent 78a08e0 commit 93339ca

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

src/index.ts

+58-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs'
12
import path from 'node:path'
23
import { fileURLToPath } from 'node:url'
34

@@ -63,7 +64,8 @@ export function resolve(
6364
}
6465

6566
initMappers(options)
66-
const mappedPath = getMappedPath(source)
67+
68+
const mappedPath = getMappedPath(source, file, true)
6769
if (mappedPath) {
6870
log('matched ts path:', mappedPath)
6971
}
@@ -183,18 +185,65 @@ function removeJsExtension(id: string) {
183185
let mappersBuildForOptions: TsResolverOptions
184186
let mappers: Array<((specifier: string) => string[]) | null> | undefined
185187

188+
const JS_EXT_PATTERN = /\.([cm]js|jsx?)$/
189+
const RELATIVE_PATH_PATTERN = /^\.{1,2}(\/.*)?$/
190+
191+
const isFile = (path?: string | undefined): path is string => {
192+
try {
193+
return !!path && fs.statSync(path).isFile()
194+
} catch {
195+
return false
196+
}
197+
}
198+
186199
/**
187200
* @param {string} source the module to resolve; i.e './some-module'
201+
* @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js'
188202
* @returns The mapped path of the module or undefined
189203
*/
190-
function getMappedPath(source: string) {
191-
const paths = mappers!
192-
.map(mapper => mapper?.(source))
193-
.filter(path => !!path)
194-
.flat()
195-
196-
console.log('source:', source)
197-
console.log('paths:', paths)
204+
function getMappedPath(
205+
source: string,
206+
file: string,
207+
retry?: boolean,
208+
): string | undefined {
209+
let paths: string[] | undefined = []
210+
211+
if (RELATIVE_PATH_PATTERN.test(source)) {
212+
const resolved = path.resolve(path.dirname(file), source)
213+
if (isFile(resolved)) {
214+
paths = [resolved]
215+
}
216+
} else {
217+
paths = mappers!.flatMap(mapper => mapper?.(source)).filter(isFile)
218+
}
219+
220+
if (retry && paths.length === 0) {
221+
if (JS_EXT_PATTERN.test(source)) {
222+
const jsExt = path.extname(source)
223+
const tsExt = jsExt.replace('js', 'ts')
224+
const basename = source.replace(JS_EXT_PATTERN, '')
225+
return (
226+
getMappedPath(basename + tsExt, file) ||
227+
getMappedPath(source + '/index.ts', file) ||
228+
getMappedPath(source + '/index.tsx', file) ||
229+
getMappedPath(source + '/index.js', file) ||
230+
getMappedPath(
231+
basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt),
232+
file,
233+
false,
234+
)
235+
)
236+
}
237+
return (
238+
getMappedPath(source + '.ts', file) ||
239+
getMappedPath(source + '.tsx', file) ||
240+
getMappedPath(source + '.js', file) ||
241+
getMappedPath(source + '.d.ts', file) ||
242+
getMappedPath(source + '/index.ts', file) ||
243+
getMappedPath(source + '/index.tsx', file) ||
244+
getMappedPath(source + '/index.js', file)
245+
)
246+
}
198247

199248
if (paths.length > 1) {
200249
log('found multiple matching ts paths:', paths)

0 commit comments

Comments
 (0)