Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate enhanced-resolve to oxc-resolver #379

Merged
merged 2 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-bulldogs-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-import-resolver-typescript": minor
---

feat: migrate `enhanced-resolve` to `oxc-resolver`
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"lint": "run-p 'lint:*'",
"lint:es": "eslint src --cache -f friendly",
"lint:tsc": "tsc --noEmit",
"prepare": "simple-git-hooks",
"prepare": "simple-git-hooks && yarn-berry-deduplicate || exit 0",
"release": "changeset publish",
"test": "run-p 'test:*'",
"test:dotInclude": "eslint --ext ts,tsx tests/dotInclude --ignore-pattern \"!.dot\"",
Expand Down Expand Up @@ -82,10 +82,10 @@
"dependencies": {
"@nolyfill/is-core-module": "1.0.39",
"debug": "^4.3.7",
"enhanced-resolve": "^5.15.0",
"get-tsconfig": "^4.10.0",
"is-bun-module": "^1.0.2",
"stable-hash": "^0.0.4",
"oxc-resolver": "^5.0.0",
"stable-hash": "^0.0.5",
"tinyglobby": "^0.2.12"
},
"devDependencies": {
Expand All @@ -104,7 +104,7 @@
"eslint": "^8.57.1",
"eslint-import-resolver-typescript": "link:.",
"eslint-plugin-import": "npm:eslint-plugin-i@^2.29.1",
"eslint-plugin-import-x": "^4.5.0",
"eslint-plugin-import-x": "^4.7.0",
"lint-staged": "^13.3.0",
"npm-run-all2": "^5.0.2",
"prettier": "^2.8.8",
Expand All @@ -113,7 +113,9 @@
"size-limit": "^11.0.0",
"size-limit-preset-node-lib": "^0.3.0",
"type-coverage": "^2.27.0",
"typescript": "~5.1.0"
"type-fest": "^4.37.0",
"typescript": "~5.1.0",
"yarn-berry-deduplicate": "^6.1.1"
},
"resolutions": {
"eslint-import-resolver-typescript": "link:.",
Expand Down
53 changes: 15 additions & 38 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ import path from 'node:path'

import isNodeCoreModule from '@nolyfill/is-core-module'
import debug from 'debug'
import type { FileSystem, ResolveOptions, Resolver } from 'enhanced-resolve'
import enhancedResolve from 'enhanced-resolve'
import { createPathsMatcher, getTsconfig } from 'get-tsconfig'
import type { TsConfigResult } from 'get-tsconfig'
import { createPathsMatcher, getTsconfig } from 'get-tsconfig'
import type { Version } from 'is-bun-module'
import { isBunModule } from 'is-bun-module'
import stableHashExports from 'stable-hash'
import { type NapiResolveOptions, ResolverFactory } from 'oxc-resolver'
import { stableHash } from 'stable-hash'
import { globSync, isDynamicPattern } from 'tinyglobby'

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- esmodule interop
const stableHash = stableHashExports.default || stableHashExports
import type { SetRequired } from 'type-fest'

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

Expand Down Expand Up @@ -79,28 +76,17 @@ export const defaultMainFields = [

export const interfaceVersion = 2

export interface TsResolverOptions
extends Omit<ResolveOptions, 'fileSystem' | 'useSyncFileSystemCalls'> {
export interface TsResolverOptions extends NapiResolveOptions {
alwaysTryTypes?: boolean
project?: string[] | string
extensions?: string[]
}

type InternalResolverOptions = Required<
Pick<
ResolveOptions,
| 'conditionNames'
| 'extensionAlias'
| 'extensions'
| 'mainFields'
| 'useSyncFileSystemCalls'
>
type InternalResolverOptions = SetRequired<
NapiResolveOptions,
'conditionNames' | 'extensionAlias' | 'extensions' | 'mainFields'
> &
ResolveOptions &
TsResolverOptions

const fileSystem = fs as FileSystem

const JS_EXT_PATTERN = /\.(?:[cm]js|jsx?)$/
const RELATIVE_PATH_PATTERN = /^\.{1,2}(?:\/.*)?$/

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

let resolverCachedOptions: InternalResolverOptions
let cachedResolver: Resolver | undefined
let cachedResolver: ResolverFactory | undefined

/**
* @param source the module to resolve; i.e './some-module'
Expand All @@ -130,7 +116,7 @@ export function resolve(
source: string,
file: string,
options?: TsResolverOptions | null,
resolver: Resolver | null = null,
resolver?: ResolverFactory | null,
): {
found: boolean
path?: string | null
Expand All @@ -146,18 +132,12 @@ export function resolve(
extensions: options?.extensions ?? defaultExtensions,
extensionAlias: options?.extensionAlias ?? defaultExtensionAlias,
mainFields: options?.mainFields ?? defaultMainFields,
fileSystem: new enhancedResolve.CachedInputFileSystem(
fileSystem,
5 * 1000,
),
useSyncFileSystemCalls: true,
}
}

if (!resolver) {
if (!cachedResolver || resolverCachedOptions !== cachedOptions) {
cachedResolver =
enhancedResolve.ResolverFactory.createResolver(cachedOptions)
cachedResolver = new ResolverFactory(cachedOptions)
resolverCachedOptions = cachedOptions
}
resolver = cachedResolver
Expand Down Expand Up @@ -194,13 +174,12 @@ export function resolve(
let foundNodePath: string | undefined
for (const mappedPath of mappedPaths) {
try {
const resolved = resolver.resolveSync(
{},
const resolved = resolver.sync(
path.dirname(path.resolve(file)),
mappedPath,
)
if (resolved) {
foundNodePath = resolved
if (resolved.path) {
foundNodePath = resolved.path
break
}
} catch {
Expand Down Expand Up @@ -246,14 +225,12 @@ export function resolve(
export function createTypeScriptImportResolver(
options?: TsResolverOptions | null,
) {
const resolver = enhancedResolve.ResolverFactory.createResolver({
const resolver = new ResolverFactory({
...options,
conditionNames: options?.conditionNames ?? defaultConditionNames,
extensions: options?.extensions ?? defaultExtensions,
extensionAlias: options?.extensionAlias ?? defaultExtensionAlias,
mainFields: options?.mainFields ?? defaultMainFields,
fileSystem: new enhancedResolve.CachedInputFileSystem(fileSystem, 5 * 1000),
useSyncFileSystemCalls: true,
})

return {
Expand Down
Loading
Loading