Skip to content

Commit 4a9176e

Browse files
authored
fix: use rspack-resolver fork for pnp support (#382)
1 parent 7d50d38 commit 4a9176e

File tree

5 files changed

+188
-169
lines changed

5 files changed

+188
-169
lines changed

.changeset/cool-apes-juggle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-import-resolver-typescript": patch
3+
---
4+
5+
fix: use `rspack-resolver` fork for pnp support

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"debug": "^4.4.0",
8686
"get-tsconfig": "^4.10.0",
8787
"is-bun-module": "^1.3.0",
88-
"oxc-resolver": "^5.0.0",
88+
"rspack-resolver": "^1.1.0",
8989
"stable-hash": "^0.0.5",
9090
"tinyglobby": "^0.2.12"
9191
},
@@ -96,15 +96,17 @@
9696
"@commitlint/cli": "^19.8.0",
9797
"@mozilla/glean": "^5.0.3",
9898
"@pkgr/rollup": "^6.0.0",
99+
"@total-typescript/ts-reset": "^0.6.1",
99100
"@types/debug": "^4.1.12",
100101
"@types/node": "^22.13.10",
102+
"@types/pnpapi": "^0.0.5",
101103
"@types/unist": "^3.0.3",
102104
"clean-pkg-json": "^1.2.1",
103105
"cross-env": "^7.0.3",
104106
"dummy.js": "link:dummy.js",
105107
"eslint": "^9.22.0",
106108
"eslint-import-resolver-typescript": "link:.",
107-
"eslint-plugin-import-x": "^4.7.0",
109+
"eslint-plugin-import-x": "^4.8.0",
108110
"lint-staged": "^15.5.0",
109111
"npm-run-all2": "^7.0.2",
110112
"prettier": "^3.5.3",

src/global.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import '@total-typescript/ts-reset'
2+
import type * as pnpapi from 'pnpapi'
3+
4+
declare module 'module' {
5+
namespace Module {
6+
function findPnpApi(source: string): typeof pnpapi
7+
}
8+
}

src/index.ts

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs'
2+
import module from 'node:module'
23
import path from 'node:path'
34

45
import isNodeCoreModule from '@nolyfill/is-core-module'
@@ -7,7 +8,7 @@ import type { TsConfigResult } from 'get-tsconfig'
78
import { createPathsMatcher, getTsconfig } from 'get-tsconfig'
89
import type { Version } from 'is-bun-module'
910
import { isBunModule } from 'is-bun-module'
10-
import { type NapiResolveOptions, ResolverFactory } from 'oxc-resolver'
11+
import { type NapiResolveOptions, ResolverFactory } from 'rspack-resolver'
1112
import { stableHash } from 'stable-hash'
1213
import { globSync, isDynamicPattern } from 'tinyglobby'
1314
import type { SetRequired } from 'type-fest'
@@ -94,7 +95,7 @@ let previousOptionsHash: string
9495
let optionsHash: string
9596
let cachedOptions: InternalResolverOptions | undefined
9697

97-
let prevCwd: string
98+
let cachedCwd: string
9899

99100
let mappersCachedOptions: InternalResolverOptions
100101
let mappers: Array<{
@@ -160,6 +161,18 @@ export function resolve(
160161
}
161162
}
162163

164+
/**
165+
* {@link https://github.com/webpack/enhanced-resolve/blob/38e9fd9acb79643a70e7bcd0d85dabc600ea321f/lib/PnpPlugin.js#L81-L83}
166+
*/
167+
if (process.versions.pnp && source === 'pnpapi') {
168+
return {
169+
found: true,
170+
path: module.findPnpApi(file).resolveToUnqualified(source, file, {
171+
considerBuiltins: false,
172+
}),
173+
}
174+
}
175+
163176
initMappers(cachedOptions)
164177

165178
let mappedPaths = getMappedPaths(source, file, cachedOptions.extensions, true)
@@ -383,18 +396,18 @@ function initMappers(options: InternalResolverOptions) {
383396
if (
384397
mappers.length > 0 &&
385398
mappersCachedOptions === options &&
386-
prevCwd === process.cwd()
399+
cachedCwd === process.cwd()
387400
) {
388401
return
389402
}
390-
prevCwd = process.cwd()
403+
cachedCwd = process.cwd()
391404
const configPaths = (
392405
typeof options.project === 'string'
393406
? [options.project]
394407
: // eslint-disable-next-line sonarjs/no-nested-conditional
395408
Array.isArray(options.project)
396409
? options.project
397-
: [process.cwd()]
410+
: [cachedCwd]
398411
) // 'tinyglobby' pattern must have POSIX separator
399412
.map(config => replacePathSeparator(config, path.sep, path.posix.sep))
400413

@@ -432,20 +445,18 @@ function initMappers(options: InternalResolverOptions) {
432445
}
433446

434447
if (!tsconfigResult) {
435-
// eslint-disable-next-line unicorn/no-useless-undefined
436-
return undefined
448+
return
437449
}
438450

439451
const mapperFn = createPathsMatcher(tsconfigResult)
440452

441453
if (!mapperFn) {
442-
// eslint-disable-next-line unicorn/no-useless-undefined
443-
return undefined
454+
return
444455
}
445456

446457
const files =
447-
tsconfigResult.config.files === undefined &&
448-
tsconfigResult.config.include === undefined
458+
tsconfigResult.config.files == null &&
459+
tsconfigResult.config.include == null
449460
? // Include everything if no files or include options
450461
globSync(defaultInclude, {
451462
absolute: true,
@@ -458,7 +469,7 @@ function initMappers(options: InternalResolverOptions) {
458469
})
459470
: [
460471
// https://www.typescriptlang.org/tsconfig/#files
461-
...(tsconfigResult.config.files !== undefined &&
472+
...(tsconfigResult.config.files != null &&
462473
tsconfigResult.config.files.length > 0
463474
? tsconfigResult.config.files.map(file =>
464475
path.normalize(
@@ -467,7 +478,7 @@ function initMappers(options: InternalResolverOptions) {
467478
)
468479
: []),
469480
// https://www.typescriptlang.org/tsconfig/#include
470-
...(tsconfigResult.config.include !== undefined &&
481+
...(tsconfigResult.config.include != null &&
471482
tsconfigResult.config.include.length > 0
472483
? globSync(tsconfigResult.config.include, {
473484
absolute: true,
@@ -487,7 +498,7 @@ function initMappers(options: InternalResolverOptions) {
487498
mapperFn,
488499
}
489500
})
490-
.filter(isDefined)
501+
.filter(Boolean)
491502

492503
mappersCachedOptions = options
493504
}
@@ -535,19 +546,6 @@ function toNativePathSeparator(p: string) {
535546
)
536547
}
537548

538-
/**
539-
* Check if value is defined.
540-
*
541-
* Helper function for TypeScript.
542-
* Should be removed when upgrading to TypeScript >= 5.5.
543-
*
544-
* @param {T | null | undefined} value Value
545-
* @returns `true` if value is defined, `false` otherwise
546-
*/
547-
function isDefined<T>(value: T | null | undefined): value is T {
548-
return value !== null && value !== undefined
549-
}
550-
551549
/**
552550
* Counts how many characters in strings `a` and `b` are exactly the same and in the same position.
553551
*

0 commit comments

Comments
 (0)