Skip to content

Commit 234c612

Browse files
committed
feat: enable alwaysTryTypes by default, add noWarnOnMultipleProjects option
1 parent bfcd8f2 commit 234c612

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

Diff for: src/index.ts

+8-6
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 => {
@@ -91,10 +91,12 @@ export const resolve = (
9191

9292
source = removeQuerystring(source)
9393

94+
options ||= {}
95+
9496
// eslint-disable-next-line sonar/label-position, sonar/no-labels
9597
resolve: if (!resolver) {
9698
// must be a array with 2+ items here already ensured by `normalizeOptions`
97-
const project = options!.project as string[]
99+
const project = options.project as string[]
98100
for (const tsconfigPath of project) {
99101
const resolverCached = resolverCache.get(tsconfigPath)
100102
if (resolverCached) {
@@ -128,7 +130,7 @@ export const resolve = (
128130
...options,
129131
tsconfig: {
130132
references: 'auto',
131-
...options!.tsconfig,
133+
...options.tsconfig,
132134
configFile: tsconfigPath,
133135
},
134136
}
@@ -171,7 +173,7 @@ export const resolve = (
171173
// if path is neither absolute nor relative
172174
if (
173175
((foundPath && JS_EXT_PATTERN.test(foundPath)) ||
174-
(options?.alwaysTryTypes && !foundPath)) &&
176+
(options.alwaysTryTypes !== false && !foundPath)) &&
175177
!/^@types[/\\]/.test(source) &&
176178
!path.isAbsolute(source) &&
177179
!source.startsWith('.')
@@ -194,15 +196,15 @@ export const resolve = (
194196
"didn't find",
195197
source,
196198
'with',
197-
options!.tsconfig?.configFile || options!.project,
199+
options.tsconfig?.configFile || options.project,
198200
)
199201
}
200202

201203
return resolved
202204
}
203205

204206
export const createTypeScriptImportResolver = (
205-
options?: TypeScriptResolverOptions,
207+
options?: TypeScriptResolverOptions | null,
206208
) => {
207209
options = normalizeOptions(options)
208210
const resolver = options.project ? null : new ResolverFactory(options)

Diff for: src/normalize-options.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@ import { 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 { TsResolverOptions, TypeScriptResolverOptions } from './types.js'
15+
import { 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

Diff for: src/types.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { NapiResolveOptions } from 'oxc-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

Diff for: tests/baseEslintConfig.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = project => ({
1414
'import/resolver': {
1515
typescript: {
1616
project,
17-
alwaysTryTypes: true,
17+
noWarnOnMultipleProjects: true,
1818
},
1919
},
2020
},

Diff for: tests/importXResolverV3/eslint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports =
2424
'import-x/resolver-next': [
2525
createTypeScriptImportResolver({
2626
project: absoluteGlobPath,
27+
noWarnOnMultipleProjects: true,
2728
}),
2829
],
2930
},

0 commit comments

Comments
 (0)