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

fix: use rspack-resolver fork for pnp support #382

Merged
merged 3 commits into from
Mar 16, 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/cool-apes-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-import-resolver-typescript": patch
---

fix: use `rspack-resolver` fork for pnp support
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"debug": "^4.4.0",
"get-tsconfig": "^4.10.0",
"is-bun-module": "^1.3.0",
"oxc-resolver": "^5.0.0",
"rspack-resolver": "^1.1.0",
"stable-hash": "^0.0.5",
"tinyglobby": "^0.2.12"
},
Expand All @@ -96,15 +96,17 @@
"@commitlint/cli": "^19.8.0",
"@mozilla/glean": "^5.0.3",
"@pkgr/rollup": "^6.0.0",
"@total-typescript/ts-reset": "^0.6.1",
"@types/debug": "^4.1.12",
"@types/node": "^22.13.10",
"@types/pnpapi": "^0.0.5",
"@types/unist": "^3.0.3",
"clean-pkg-json": "^1.2.1",
"cross-env": "^7.0.3",
"dummy.js": "link:dummy.js",
"eslint": "^9.22.0",
"eslint-import-resolver-typescript": "link:.",
"eslint-plugin-import-x": "^4.7.0",
"eslint-plugin-import-x": "^4.8.0",
"lint-staged": "^15.5.0",
"npm-run-all2": "^7.0.2",
"prettier": "^3.5.3",
Expand Down
8 changes: 8 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import '@total-typescript/ts-reset'
import type * as pnpapi from 'pnpapi'

declare module 'module' {
namespace Module {
function findPnpApi(source: string): typeof pnpapi
}
}
52 changes: 25 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs'
import module from 'node:module'
import path from 'node:path'

import isNodeCoreModule from '@nolyfill/is-core-module'
Expand All @@ -7,7 +8,7 @@ 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 { type NapiResolveOptions, ResolverFactory } from 'oxc-resolver'
import { type NapiResolveOptions, ResolverFactory } from 'rspack-resolver'
import { stableHash } from 'stable-hash'
import { globSync, isDynamicPattern } from 'tinyglobby'
import type { SetRequired } from 'type-fest'
Expand Down Expand Up @@ -94,7 +95,7 @@ let previousOptionsHash: string
let optionsHash: string
let cachedOptions: InternalResolverOptions | undefined

let prevCwd: string
let cachedCwd: string

let mappersCachedOptions: InternalResolverOptions
let mappers: Array<{
Expand Down Expand Up @@ -160,6 +161,18 @@ export function resolve(
}
}

/**
* {@link https://github.com/webpack/enhanced-resolve/blob/38e9fd9acb79643a70e7bcd0d85dabc600ea321f/lib/PnpPlugin.js#L81-L83}
*/
if (process.versions.pnp && source === 'pnpapi') {
return {
found: true,
path: module.findPnpApi(file).resolveToUnqualified(source, file, {
considerBuiltins: false,
}),
}
}

initMappers(cachedOptions)

let mappedPaths = getMappedPaths(source, file, cachedOptions.extensions, true)
Expand Down Expand Up @@ -383,18 +396,18 @@ function initMappers(options: InternalResolverOptions) {
if (
mappers.length > 0 &&
mappersCachedOptions === options &&
prevCwd === process.cwd()
cachedCwd === process.cwd()
) {
return
}
prevCwd = process.cwd()
cachedCwd = process.cwd()
const configPaths = (
typeof options.project === 'string'
? [options.project]
: // eslint-disable-next-line sonarjs/no-nested-conditional
Array.isArray(options.project)
? options.project
: [process.cwd()]
: [cachedCwd]
) // 'tinyglobby' pattern must have POSIX separator
.map(config => replacePathSeparator(config, path.sep, path.posix.sep))

Expand Down Expand Up @@ -432,20 +445,18 @@ function initMappers(options: InternalResolverOptions) {
}

if (!tsconfigResult) {
// eslint-disable-next-line unicorn/no-useless-undefined
return undefined
return
}

const mapperFn = createPathsMatcher(tsconfigResult)

if (!mapperFn) {
// eslint-disable-next-line unicorn/no-useless-undefined
return undefined
return
}

const files =
tsconfigResult.config.files === undefined &&
tsconfigResult.config.include === undefined
tsconfigResult.config.files == null &&
tsconfigResult.config.include == null
? // Include everything if no files or include options
globSync(defaultInclude, {
absolute: true,
Expand All @@ -458,7 +469,7 @@ function initMappers(options: InternalResolverOptions) {
})
: [
// https://www.typescriptlang.org/tsconfig/#files
...(tsconfigResult.config.files !== undefined &&
...(tsconfigResult.config.files != null &&
tsconfigResult.config.files.length > 0
? tsconfigResult.config.files.map(file =>
path.normalize(
Expand All @@ -467,7 +478,7 @@ function initMappers(options: InternalResolverOptions) {
)
: []),
// https://www.typescriptlang.org/tsconfig/#include
...(tsconfigResult.config.include !== undefined &&
...(tsconfigResult.config.include != null &&
tsconfigResult.config.include.length > 0
? globSync(tsconfigResult.config.include, {
absolute: true,
Expand All @@ -487,7 +498,7 @@ function initMappers(options: InternalResolverOptions) {
mapperFn,
}
})
.filter(isDefined)
.filter(Boolean)

mappersCachedOptions = options
}
Expand Down Expand Up @@ -535,19 +546,6 @@ function toNativePathSeparator(p: string) {
)
}

/**
* Check if value is defined.
*
* Helper function for TypeScript.
* Should be removed when upgrading to TypeScript >= 5.5.
*
* @param {T | null | undefined} value Value
* @returns `true` if value is defined, `false` otherwise
*/
function isDefined<T>(value: T | null | undefined): value is T {
return value !== null && value !== undefined
}

/**
* Counts how many characters in strings `a` and `b` are exactly the same and in the same position.
*
Expand Down
Loading
Loading