Skip to content

Commit 1dae7b6

Browse files
committed
refactor: remove some unnecessary codes
1 parent fce9c63 commit 1dae7b6

File tree

4 files changed

+58
-44
lines changed

4 files changed

+58
-44
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -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.2",
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

+24-26
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'
@@ -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
*

yarn.lock

+23-17
Original file line numberDiff line numberDiff line change
@@ -3771,6 +3771,13 @@ __metadata:
37713771
languageName: node
37723772
linkType: hard
37733773

3774+
"@total-typescript/ts-reset@npm:^0.6.1":
3775+
version: 0.6.1
3776+
resolution: "@total-typescript/ts-reset@npm:0.6.1"
3777+
checksum: 10/3e18063433c4667561b2a5ea6b698d95f8ca2cbecc7e3717b60f9266e2fd7070b2006cab0df255e60eaad5658d1cec4afc89ea4ca5645c8f36a312093bba3645
3778+
languageName: node
3779+
linkType: hard
3780+
37743781
"@tsconfig/node10@npm:^1.0.7":
37753782
version: 1.0.11
37763783
resolution: "@tsconfig/node10@npm:1.0.11"
@@ -4050,6 +4057,13 @@ __metadata:
40504057
languageName: node
40514058
linkType: hard
40524059

4060+
"@types/pnpapi@npm:^0.0.5":
4061+
version: 0.0.5
4062+
resolution: "@types/pnpapi@npm:0.0.5"
4063+
checksum: 10/46d8474b77358c8cc48b957c059de6ad0839d18d8e90252c500c6bc30702e44e2629cf330350f7b291f35d863190b5390b641992c0601853a5d8b129fa91bd4d
4064+
languageName: node
4065+
linkType: hard
4066+
40534067
"@types/resolve@npm:1.20.2":
40544068
version: 1.20.2
40554069
resolution: "@types/resolve@npm:1.20.2"
@@ -6691,16 +6705,18 @@ __metadata:
66916705
"@mozilla/glean": "npm:^5.0.3"
66926706
"@nolyfill/is-core-module": "npm:1.0.39"
66936707
"@pkgr/rollup": "npm:^6.0.0"
6708+
"@total-typescript/ts-reset": "npm:^0.6.1"
66946709
"@types/debug": "npm:^4.1.12"
66956710
"@types/node": "npm:^22.13.10"
6711+
"@types/pnpapi": "npm:^0.0.5"
66966712
"@types/unist": "npm:^3.0.3"
66976713
clean-pkg-json: "npm:^1.2.1"
66986714
cross-env: "npm:^7.0.3"
66996715
debug: "npm:^4.4.0"
67006716
dummy.js: "link:dummy.js"
67016717
eslint: "npm:^9.22.0"
67026718
eslint-import-resolver-typescript: "link:."
6703-
eslint-plugin-import-x: "npm:^4.7.2"
6719+
eslint-plugin-import-x: "npm:^4.8.0"
67046720
get-tsconfig: "npm:^4.10.0"
67056721
is-bun-module: "npm:^1.3.0"
67066722
lint-staged: "npm:^15.5.0"
@@ -6796,25 +6812,24 @@ __metadata:
67966812
languageName: node
67976813
linkType: hard
67986814

6799-
"eslint-plugin-import-x@npm:^4.6.1, eslint-plugin-import-x@npm:^4.7.2":
6800-
version: 4.7.2
6801-
resolution: "eslint-plugin-import-x@npm:4.7.2"
6815+
"eslint-plugin-import-x@npm:^4.6.1, eslint-plugin-import-x@npm:^4.8.0":
6816+
version: 4.8.0
6817+
resolution: "eslint-plugin-import-x@npm:4.8.0"
68026818
dependencies:
68036819
"@types/doctrine": "npm:^0.0.9"
68046820
"@typescript-eslint/utils": "npm:^8.26.1"
68056821
debug: "npm:^4.4.0"
68066822
doctrine: "npm:^3.0.0"
68076823
eslint-import-resolver-node: "npm:^0.3.9"
68086824
get-tsconfig: "npm:^4.10.0"
6809-
is-glob: "npm:^4.0.3"
6810-
minimatch: "npm:^10.0.1"
6825+
picomatch: "npm:^4.0.2"
68116826
rspack-resolver: "npm:^1.1.0"
68126827
semver: "npm:^7.7.1"
68136828
stable-hash: "npm:^0.0.5"
68146829
tslib: "npm:^2.8.1"
68156830
peerDependencies:
68166831
eslint: ^8.57.0 || ^9.0.0
6817-
checksum: 10/866ef0301576310fb55a07748909373d93593bc891ef869e05a6122ffc1dc60b609aa016a70a4be1ff9186d9608422357e2949d7f09a6e38d6d95a494e9b8805
6832+
checksum: 10/ab0fe7b33a8c0f08ba7e7f0a4e0a7421f82f15688b775d6cd6dcd7b84ab5a511fd35bb6dd026156ff12b83b2be0b3d185638a1c886ecbd12979efafc0a298428
68186833
languageName: node
68196834
linkType: hard
68206835

@@ -10648,16 +10663,7 @@ __metadata:
1064810663
languageName: node
1064910664
linkType: hard
1065010665

10651-
"minimatch@npm:6 || 7 || 8 || 9 || 10, minimatch@npm:^10.0.1":
10652-
version: 10.0.1
10653-
resolution: "minimatch@npm:10.0.1"
10654-
dependencies:
10655-
brace-expansion: "npm:^2.0.1"
10656-
checksum: 10/082e7ccbc090d5f8c4e4e029255d5a1d1e3af37bda837da2b8b0085b1503a1210c91ac90d9ebfe741d8a5f286ece820a1abb4f61dc1f82ce602a055d461d93f3
10657-
languageName: node
10658-
linkType: hard
10659-
10660-
"minimatch@npm:9.0.5, minimatch@npm:^9.0.0, minimatch@npm:^9.0.3, minimatch@npm:^9.0.4, minimatch@npm:^9.0.5":
10666+
"minimatch@npm:6 || 7 || 8 || 9 || 10, minimatch@npm:9.0.5, minimatch@npm:^9.0.0, minimatch@npm:^9.0.3, minimatch@npm:^9.0.4, minimatch@npm:^9.0.5":
1066110667
version: 9.0.5
1066210668
resolution: "minimatch@npm:9.0.5"
1066310669
dependencies:

0 commit comments

Comments
 (0)