Skip to content

Commit 6814443

Browse files
authored
feat: migrate enhanced-resolve to oxc-resolver (#379)
1 parent 904356b commit 6814443

File tree

4 files changed

+689
-195
lines changed

4 files changed

+689
-195
lines changed

.changeset/thin-bulldogs-fetch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-import-resolver-typescript": minor
3+
---
4+
5+
feat: migrate `enhanced-resolve` to `oxc-resolver`

package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"lint": "run-p 'lint:*'",
4949
"lint:es": "eslint src --cache -f friendly",
5050
"lint:tsc": "tsc --noEmit",
51-
"prepare": "simple-git-hooks",
51+
"prepare": "simple-git-hooks && yarn-berry-deduplicate || exit 0",
5252
"release": "changeset publish",
5353
"test": "run-p 'test:*'",
5454
"test:dotInclude": "eslint --ext ts,tsx tests/dotInclude --ignore-pattern \"!.dot\"",
@@ -82,10 +82,10 @@
8282
"dependencies": {
8383
"@nolyfill/is-core-module": "1.0.39",
8484
"debug": "^4.3.7",
85-
"enhanced-resolve": "^5.15.0",
8685
"get-tsconfig": "^4.10.0",
8786
"is-bun-module": "^1.0.2",
88-
"stable-hash": "^0.0.4",
87+
"oxc-resolver": "^5.0.0",
88+
"stable-hash": "^0.0.5",
8989
"tinyglobby": "^0.2.12"
9090
},
9191
"devDependencies": {
@@ -104,7 +104,7 @@
104104
"eslint": "^8.57.1",
105105
"eslint-import-resolver-typescript": "link:.",
106106
"eslint-plugin-import": "npm:eslint-plugin-i@^2.29.1",
107-
"eslint-plugin-import-x": "^4.5.0",
107+
"eslint-plugin-import-x": "^4.7.0",
108108
"lint-staged": "^13.3.0",
109109
"npm-run-all2": "^5.0.2",
110110
"prettier": "^2.8.8",
@@ -113,7 +113,9 @@
113113
"size-limit": "^11.0.0",
114114
"size-limit-preset-node-lib": "^0.3.0",
115115
"type-coverage": "^2.27.0",
116-
"typescript": "~5.1.0"
116+
"type-fest": "^4.37.0",
117+
"typescript": "~5.1.0",
118+
"yarn-berry-deduplicate": "^6.1.1"
117119
},
118120
"resolutions": {
119121
"eslint-import-resolver-typescript": "link:.",

src/index.ts

+15-38
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ import path from 'node:path'
33

44
import isNodeCoreModule from '@nolyfill/is-core-module'
55
import debug from 'debug'
6-
import type { FileSystem, ResolveOptions, Resolver } from 'enhanced-resolve'
7-
import enhancedResolve from 'enhanced-resolve'
8-
import { createPathsMatcher, getTsconfig } from 'get-tsconfig'
96
import type { TsConfigResult } from 'get-tsconfig'
7+
import { createPathsMatcher, getTsconfig } from 'get-tsconfig'
108
import type { Version } from 'is-bun-module'
119
import { isBunModule } from 'is-bun-module'
12-
import stableHashExports from 'stable-hash'
10+
import { type NapiResolveOptions, ResolverFactory } from 'oxc-resolver'
11+
import { stableHash } from 'stable-hash'
1312
import { globSync, isDynamicPattern } from 'tinyglobby'
14-
15-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- esmodule interop
16-
const stableHash = stableHashExports.default || stableHashExports
13+
import type { SetRequired } from 'type-fest'
1714

1815
const IMPORTER_NAME = 'eslint-import-resolver-typescript'
1916

@@ -79,28 +76,17 @@ export const defaultMainFields = [
7976

8077
export const interfaceVersion = 2
8178

82-
export interface TsResolverOptions
83-
extends Omit<ResolveOptions, 'fileSystem' | 'useSyncFileSystemCalls'> {
79+
export interface TsResolverOptions extends NapiResolveOptions {
8480
alwaysTryTypes?: boolean
8581
project?: string[] | string
86-
extensions?: string[]
8782
}
8883

89-
type InternalResolverOptions = Required<
90-
Pick<
91-
ResolveOptions,
92-
| 'conditionNames'
93-
| 'extensionAlias'
94-
| 'extensions'
95-
| 'mainFields'
96-
| 'useSyncFileSystemCalls'
97-
>
84+
type InternalResolverOptions = SetRequired<
85+
NapiResolveOptions,
86+
'conditionNames' | 'extensionAlias' | 'extensions' | 'mainFields'
9887
> &
99-
ResolveOptions &
10088
TsResolverOptions
10189

102-
const fileSystem = fs as FileSystem
103-
10490
const JS_EXT_PATTERN = /\.(?:[cm]js|jsx?)$/
10591
const RELATIVE_PATH_PATTERN = /^\.{1,2}(?:\/.*)?$/
10692

@@ -118,7 +104,7 @@ let mappers: Array<{
118104
}> = []
119105

120106
let resolverCachedOptions: InternalResolverOptions
121-
let cachedResolver: Resolver | undefined
107+
let cachedResolver: ResolverFactory | undefined
122108

123109
/**
124110
* @param source the module to resolve; i.e './some-module'
@@ -130,7 +116,7 @@ export function resolve(
130116
source: string,
131117
file: string,
132118
options?: TsResolverOptions | null,
133-
resolver: Resolver | null = null,
119+
resolver?: ResolverFactory | null,
134120
): {
135121
found: boolean
136122
path?: string | null
@@ -146,18 +132,12 @@ export function resolve(
146132
extensions: options?.extensions ?? defaultExtensions,
147133
extensionAlias: options?.extensionAlias ?? defaultExtensionAlias,
148134
mainFields: options?.mainFields ?? defaultMainFields,
149-
fileSystem: new enhancedResolve.CachedInputFileSystem(
150-
fileSystem,
151-
5 * 1000,
152-
),
153-
useSyncFileSystemCalls: true,
154135
}
155136
}
156137

157138
if (!resolver) {
158139
if (!cachedResolver || resolverCachedOptions !== cachedOptions) {
159-
cachedResolver =
160-
enhancedResolve.ResolverFactory.createResolver(cachedOptions)
140+
cachedResolver = new ResolverFactory(cachedOptions)
161141
resolverCachedOptions = cachedOptions
162142
}
163143
resolver = cachedResolver
@@ -194,13 +174,12 @@ export function resolve(
194174
let foundNodePath: string | undefined
195175
for (const mappedPath of mappedPaths) {
196176
try {
197-
const resolved = resolver.resolveSync(
198-
{},
177+
const resolved = resolver.sync(
199178
path.dirname(path.resolve(file)),
200179
mappedPath,
201180
)
202-
if (resolved) {
203-
foundNodePath = resolved
181+
if (resolved.path) {
182+
foundNodePath = resolved.path
204183
break
205184
}
206185
} catch {
@@ -246,14 +225,12 @@ export function resolve(
246225
export function createTypeScriptImportResolver(
247226
options?: TsResolverOptions | null,
248227
) {
249-
const resolver = enhancedResolve.ResolverFactory.createResolver({
228+
const resolver = new ResolverFactory({
250229
...options,
251230
conditionNames: options?.conditionNames ?? defaultConditionNames,
252231
extensions: options?.extensions ?? defaultExtensions,
253232
extensionAlias: options?.extensionAlias ?? defaultExtensionAlias,
254233
mainFields: options?.mainFields ?? defaultMainFields,
255-
fileSystem: new enhancedResolve.CachedInputFileSystem(fileSystem, 5 * 1000),
256-
useSyncFileSystemCalls: true,
257234
})
258235

259236
return {

0 commit comments

Comments
 (0)