Skip to content

Commit f52bf2d

Browse files
committed
refactor: support custom extensions on resolving
1 parent ca59b47 commit f52bf2d

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

src/index.ts

+30-22
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,14 @@ export function resolve(
9595
found: boolean
9696
path?: string | null
9797
} {
98-
const opts: ResolveOptions & TsResolverOptions = {
98+
const opts: Required<
99+
Pick<
100+
ResolveOptions,
101+
'conditionNames' | 'extensions' | 'mainFields' | 'useSyncFileSystemCalls'
102+
>
103+
> &
104+
ResolveOptions &
105+
TsResolverOptions = {
99106
...options,
100107
extensions: options?.extensions ?? defaultExtensions,
101108
mainFields: options?.mainFields ?? defaultMainFields,
@@ -122,7 +129,7 @@ export function resolve(
122129

123130
initMappers(opts)
124131

125-
const mappedPath = getMappedPath(source, file, true)
132+
const mappedPath = getMappedPath(source, file, opts.extensions, true)
126133
if (mappedPath) {
127134
log('matched ts path:', mappedPath)
128135
}
@@ -250,12 +257,15 @@ const isFile = (path?: string | undefined): path is string => {
250257
/**
251258
* @param {string} source the module to resolve; i.e './some-module'
252259
* @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js'
260+
* @param {string[]} extensions the extensions to try
261+
* @param {boolean} retry should retry on failed to resolve
253262
* @returns The mapped path of the module or undefined
254263
*/
255264
// eslint-disable-next-line sonarjs/cognitive-complexity
256265
function getMappedPath(
257266
source: string,
258267
file: string,
268+
extensions = defaultExtensions,
259269
retry?: boolean,
260270
): string | undefined {
261271
let paths: string[] | undefined = []
@@ -269,9 +279,7 @@ function getMappedPath(
269279
paths = mappers!
270280
.map(mapper =>
271281
mapper?.(source).map(item =>
272-
path.extname(item)
273-
? item
274-
: ['ts', 'tsx', '.d.ts', 'js'].map(ext => `${item}.${ext}`),
282+
['', ...extensions].map(ext => `${item}${ext}`),
275283
),
276284
)
277285
.flat(2)
@@ -283,28 +291,28 @@ function getMappedPath(
283291
const jsExt = path.extname(source)
284292
const tsExt = jsExt.replace('js', 'ts')
285293
const basename = source.replace(JS_EXT_PATTERN, '')
286-
return (
294+
295+
const resolved =
287296
getMappedPath(basename + tsExt, file) ||
288-
getMappedPath(source + '/index.ts', file) ||
289-
getMappedPath(source + '/index.tsx', file) ||
290-
getMappedPath(source + '/index.d.ts', file) ||
291297
getMappedPath(
292298
basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt),
293299
file,
294-
) ||
295-
getMappedPath(source + '/index.js', file)
296-
)
300+
)
301+
302+
if (resolved) {
303+
return resolved
304+
}
305+
}
306+
307+
for (const ext of extensions) {
308+
const resolved =
309+
getMappedPath(source + ext, file) ||
310+
getMappedPath(source + `/index${ext}`, file)
311+
312+
if (resolved) {
313+
return resolved
314+
}
297315
}
298-
return (
299-
getMappedPath(source + '.ts', file) ||
300-
getMappedPath(source + '.tsx', file) ||
301-
getMappedPath(source + '.js', file) ||
302-
getMappedPath(source + '.d.ts', file) ||
303-
getMappedPath(source + '/index.ts', file) ||
304-
getMappedPath(source + '/index.tsx', file) ||
305-
getMappedPath(source + '/index.d.ts', file) ||
306-
getMappedPath(source + '/index.js', file)
307-
)
308316
}
309317

310318
if (paths.length > 1) {

0 commit comments

Comments
 (0)