Skip to content

Commit 3d8e5f8

Browse files
committed
feat: enable alwaysTryTypes by default, add noWarnOnMultipleProjects option
1 parent c97fb78 commit 3d8e5f8

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const oxcResolve = (
5959
export const resolve = (
6060
source: string,
6161
file: string,
62-
options?: TypeScriptResolverOptions,
62+
options?: TypeScriptResolverOptions | null,
6363
resolver?: ResolverFactory | null,
6464
// eslint-disable-next-line sonarjs/cognitive-complexity
6565
): ResolvedResult => {
@@ -173,7 +173,7 @@ export const resolve = (
173173
// if path is neither absolute nor relative
174174
if (
175175
((foundPath && JS_EXT_PATTERN.test(foundPath)) ||
176-
(options?.alwaysTryTypes && !foundPath)) &&
176+
(options.alwaysTryTypes !== false && !foundPath)) &&
177177
!/^@types[/\\]/.test(source) &&
178178
!path.isAbsolute(source) &&
179179
!source.startsWith('.')
@@ -204,7 +204,7 @@ export const resolve = (
204204
}
205205

206206
export const createTypeScriptImportResolver = (
207-
options?: TypeScriptResolverOptions,
207+
options?: TypeScriptResolverOptions | null,
208208
) => {
209209
options = normalizeOptions(options)
210210
const resolver = options.project ? null : new ResolverFactory(options)

src/normalize-options.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@ import type { TsconfigOptions } from 'oxc-resolver'
22
import { globSync, isDynamicPattern } from 'tinyglobby'
33

44
import {
5+
DEFAULT_CONFIGS,
6+
DEFAULT_IGNORE,
7+
DEFAULT_TRY_PATHS,
58
defaultConditionNames,
6-
defaultExtensions,
79
defaultExtensionAlias,
10+
defaultExtensions,
811
defaultMainFields,
9-
DEFAULT_CONFIGS,
10-
DEFAULT_TRY_PATHS,
11-
DEFAULT_IGNORE,
1212
} from './constants.js'
1313
import { tryFile } from './helpers.js'
1414
import { log } from './logger.js'
15-
import type { TsResolverOptions, TypeScriptResolverOptions } from './types.js'
15+
import type { TypeScriptResolverOptions } from './types.js'
1616

1717
export let defaultConfigFile: string
1818

1919
const configFileMapping = new Map<string, TypeScriptResolverOptions>()
2020

21+
let warned: boolean | undefined
22+
2123
export function normalizeOptions(
2224
options?: TypeScriptResolverOptions | null,
23-
): TsResolverOptions
25+
): TypeScriptResolverOptions
2426
// eslint-disable-next-line sonarjs/cognitive-complexity
2527
export function normalizeOptions(
26-
options?: TsResolverOptions | null,
27-
): TsResolverOptions {
28-
let { project, tsconfig } = (options ??= {})
28+
options?: TypeScriptResolverOptions | null,
29+
): TypeScriptResolverOptions {
30+
let { project, tsconfig, noWarnOnMultipleProjects } = (options ||= {})
2931

3032
let { configFile, references }: Partial<TsconfigOptions> = tsconfig ?? {}
3133

@@ -52,6 +54,11 @@ export function normalizeOptions(
5254
}
5355
if (project.length <= 1) {
5456
project = undefined
57+
} else if (!warned && !noWarnOnMultipleProjects) {
58+
warned = true
59+
console.warn(
60+
'Multiple projects found, consider using a single `tsconfig` with `references` to speed up, or use `noWarnOnMultipleProjects` to suppress this warning',
61+
)
5562
}
5663
}
5764

src/types.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { NapiResolveOptions } from 'rspack-resolver'
22

3-
export interface TsResolverOptions extends NapiResolveOptions {
3+
export interface TypeScriptResolverOptions extends NapiResolveOptions {
44
project?: string[] | string
5+
/**
6+
* @default true - whether to always try to resolve `@types` packages
7+
*/
58
alwaysTryTypes?: boolean
9+
noWarnOnMultipleProjects?: boolean
610
}
7-
8-
export type TypeScriptResolverOptions = TsResolverOptions | null

tests/base.eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const base = project => ({
1515
'import-x/resolver': {
1616
typescript: {
1717
project,
18-
alwaysTryTypes: true,
18+
noWarnOnMultipleProjects: true,
1919
},
2020
},
2121
},

tests/importXResolverV3/eslint.config.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports =
2626
'import-x/resolver-next': [
2727
createTypeScriptImportResolver({
2828
project: absoluteGlobPath,
29+
noWarnOnMultipleProjects: true,
2930
}),
3031
],
3132
},

0 commit comments

Comments
 (0)