Skip to content

Commit faaebf4

Browse files
committed
feat!: use oxc-resolver directly
1 parent 7666123 commit faaebf4

7 files changed

+105
-129
lines changed

Diff for: package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@
6464
"peerDependencies": {
6565
"eslint": "*",
6666
"eslint-plugin-import": "*",
67-
"eslint-plugin-import-x": "*",
68-
"oxc-resolver": "^4.0.0 || ^5.0.0"
67+
"eslint-plugin-import-x": "*"
6968
},
7069
"peerDependenciesMeta": {
7170
"eslint-plugin-import": {
@@ -78,8 +77,9 @@
7877
"dependencies": {
7978
"@pkgr/core": "^0.1.1",
8079
"debug": "^4.3.7",
81-
"eslint-import-resolver-oxc": "^0.12.0",
82-
"is-bun-module": "^1.0.2"
80+
"is-bun-module": "^1.0.2",
81+
"oxc-resolver": "^5.0.0",
82+
"stable-hash": "^0.0.4"
8383
},
8484
"devDependencies": {
8585
"@1stg/eslint-config": "7.0.1",
@@ -101,7 +101,6 @@
101101
"eslint-plugin-import-x": "^4.5.0",
102102
"lint-staged": "^13.3.0",
103103
"npm-run-all2": "^5.0.2",
104-
"oxc-resolver": "^4.0.0",
105104
"prettier": "^2.8.8",
106105
"react": "^18.3.1",
107106
"simple-git-hooks": "^2.11.1",

Diff for: patches/eslint-import-resolver-oxc+0.12.0.patch

-13
This file was deleted.

Diff for: src/.eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
extends: require.resolve('@1stg/eslint-config'),
2+
extends: '@1stg',
33
}

Diff for: src/index.ts

+41-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { isBuiltin } from 'node:module'
22
import path from 'node:path'
33

4-
import { resolve as oxcResolve } from 'eslint-import-resolver-oxc'
54
import { type Version, isBunModule } from 'is-bun-module'
65
import { ResolverFactory } from 'oxc-resolver'
6+
import stableHash_ from 'stable-hash'
77

88
import { JS_EXT_PATTERN } from './constants.js'
99
import { mangleScopedPackage, removeQuerystring } from './helpers.js'
@@ -16,6 +16,28 @@ export * from './helpers.js'
1616
export * from './normalize-options.js'
1717
export type * from './types.js'
1818

19+
// CJS <-> ESM interop
20+
const stableHash = 'default' in stableHash_ ? stableHash_.default : stableHash_
21+
22+
const resolverCache = new Map<string, ResolverFactory>()
23+
24+
const oxcResolve = (
25+
source: string,
26+
file: string,
27+
resolver: ResolverFactory,
28+
) => {
29+
const result = resolver.sync(path.dirname(file), source)
30+
if (result.path) {
31+
return {
32+
found: true,
33+
path: result.path,
34+
}
35+
}
36+
return {
37+
found: false,
38+
}
39+
}
40+
1941
export const resolve = (
2042
source: string,
2143
file: string,
@@ -39,20 +61,28 @@ export const resolve = (
3961
}
4062
}
4163

42-
source = removeQuerystring(source)
43-
4464
if (resolver == null) {
45-
options = normalizeOptions(options)
65+
const optionsHash = stableHash(options)
66+
// take `cwd` into account -- #217
67+
const cacheKey = `${optionsHash}:${process.cwd()}`
68+
let cached = resolverCache.get(cacheKey)
69+
if (!cached) {
70+
options = normalizeOptions(options)
71+
resolverCache.set(cacheKey, (cached = new ResolverFactory(options)))
72+
}
73+
resolver = cached
4674
}
4775

48-
const resolved = oxcResolve(source, file, options, resolver)
76+
source = removeQuerystring(source)
77+
78+
const resolved = oxcResolve(source, file, resolver)
4979

5080
const foundPath = resolved.path
5181

5282
// naive attempt at `@types/*` resolution,
5383
// if path is neither absolute nor relative
5484
if (
55-
(JS_EXT_PATTERN.test(foundPath!) ||
85+
((foundPath && JS_EXT_PATTERN.test(foundPath)) ||
5686
(options?.alwaysTryTypes && !foundPath)) &&
5787
!/^@types[/\\]/.test(source) &&
5888
!path.isAbsolute(source) &&
@@ -61,27 +91,21 @@ export const resolve = (
6191
const definitelyTyped = oxcResolve(
6292
'@types/' + mangleScopedPackage(source),
6393
file,
64-
options,
94+
resolver,
6595
)
96+
6697
if (definitelyTyped.found) {
6798
return definitelyTyped
6899
}
69100
}
70101

71102
if (foundPath) {
72103
log('matched path:', foundPath)
73-
74-
return {
75-
found: true,
76-
path: foundPath,
77-
}
104+
} else {
105+
log("didn't find", source)
78106
}
79107

80-
log("didn't find ", source)
81-
82-
return {
83-
found: false,
84-
}
108+
return resolved
85109
}
86110

87111
export const createTypeScriptImportResolver = (

Diff for: src/normalize-options.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ export const normalizeOptions = (
6262

6363
if (fallback) {
6464
log('auto setting `tsconfig.configFile` to', defaultConfigFile)
65-
options = { ...options, tsconfig: { configFile: defaultConfigFile } }
65+
options = {
66+
...options,
67+
tsconfig: { ...options?.tsconfig, configFile: defaultConfigFile },
68+
}
6669
}
6770

6871
if (options?.tsconfig && !options.tsconfig.references) {

Diff for: src/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { OxcResolverOptions } from 'eslint-import-resolver-oxc'
1+
import type { NapiResolveOptions } from 'oxc-resolver'
22

3-
export interface TypeScriptResolverOptions extends OxcResolverOptions {
3+
export interface TypeScriptResolverOptions extends NapiResolveOptions {
44
/**
55
* @deprecated Use `tsconfig` instead, `references` is supported
66
*/

0 commit comments

Comments
 (0)