1
1
import fs from 'node:fs'
2
+ import module from 'node:module'
2
3
import path from 'node:path'
3
4
4
5
import isNodeCoreModule from '@nolyfill/is-core-module'
@@ -7,7 +8,7 @@ import type { TsConfigResult } from 'get-tsconfig'
7
8
import { createPathsMatcher , getTsconfig } from 'get-tsconfig'
8
9
import type { Version } from 'is-bun-module'
9
10
import { isBunModule } from 'is-bun-module'
10
- import { type NapiResolveOptions , ResolverFactory } from 'oxc -resolver'
11
+ import { type NapiResolveOptions , ResolverFactory } from 'rspack -resolver'
11
12
import { stableHash } from 'stable-hash'
12
13
import { globSync , isDynamicPattern } from 'tinyglobby'
13
14
import type { SetRequired } from 'type-fest'
@@ -94,7 +95,7 @@ let previousOptionsHash: string
94
95
let optionsHash : string
95
96
let cachedOptions : InternalResolverOptions | undefined
96
97
97
- let prevCwd : string
98
+ let cachedCwd : string
98
99
99
100
let mappersCachedOptions : InternalResolverOptions
100
101
let mappers : Array < {
@@ -160,6 +161,18 @@ export function resolve(
160
161
}
161
162
}
162
163
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
+
163
176
initMappers ( cachedOptions )
164
177
165
178
let mappedPaths = getMappedPaths ( source , file , cachedOptions . extensions , true )
@@ -383,18 +396,18 @@ function initMappers(options: InternalResolverOptions) {
383
396
if (
384
397
mappers . length > 0 &&
385
398
mappersCachedOptions === options &&
386
- prevCwd === process . cwd ( )
399
+ cachedCwd === process . cwd ( )
387
400
) {
388
401
return
389
402
}
390
- prevCwd = process . cwd ( )
403
+ cachedCwd = process . cwd ( )
391
404
const configPaths = (
392
405
typeof options . project === 'string'
393
406
? [ options . project ]
394
407
: // eslint-disable-next-line sonarjs/no-nested-conditional
395
408
Array . isArray ( options . project )
396
409
? options . project
397
- : [ process . cwd ( ) ]
410
+ : [ cachedCwd ]
398
411
) // 'tinyglobby' pattern must have POSIX separator
399
412
. map ( config => replacePathSeparator ( config , path . sep , path . posix . sep ) )
400
413
@@ -432,20 +445,18 @@ function initMappers(options: InternalResolverOptions) {
432
445
}
433
446
434
447
if ( ! tsconfigResult ) {
435
- // eslint-disable-next-line unicorn/no-useless-undefined
436
- return undefined
448
+ return
437
449
}
438
450
439
451
const mapperFn = createPathsMatcher ( tsconfigResult )
440
452
441
453
if ( ! mapperFn ) {
442
- // eslint-disable-next-line unicorn/no-useless-undefined
443
- return undefined
454
+ return
444
455
}
445
456
446
457
const files =
447
- tsconfigResult . config . files === undefined &&
448
- tsconfigResult . config . include === undefined
458
+ tsconfigResult . config . files == null &&
459
+ tsconfigResult . config . include == null
449
460
? // Include everything if no files or include options
450
461
globSync ( defaultInclude , {
451
462
absolute : true ,
@@ -458,7 +469,7 @@ function initMappers(options: InternalResolverOptions) {
458
469
} )
459
470
: [
460
471
// https://www.typescriptlang.org/tsconfig/#files
461
- ...( tsconfigResult . config . files !== undefined &&
472
+ ...( tsconfigResult . config . files != null &&
462
473
tsconfigResult . config . files . length > 0
463
474
? tsconfigResult . config . files . map ( file =>
464
475
path . normalize (
@@ -467,7 +478,7 @@ function initMappers(options: InternalResolverOptions) {
467
478
)
468
479
: [ ] ) ,
469
480
// https://www.typescriptlang.org/tsconfig/#include
470
- ...( tsconfigResult . config . include !== undefined &&
481
+ ...( tsconfigResult . config . include != null &&
471
482
tsconfigResult . config . include . length > 0
472
483
? globSync ( tsconfigResult . config . include , {
473
484
absolute : true ,
@@ -487,7 +498,7 @@ function initMappers(options: InternalResolverOptions) {
487
498
mapperFn,
488
499
}
489
500
} )
490
- . filter ( isDefined )
501
+ . filter ( Boolean )
491
502
492
503
mappersCachedOptions = options
493
504
}
@@ -535,19 +546,6 @@ function toNativePathSeparator(p: string) {
535
546
)
536
547
}
537
548
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
-
551
549
/**
552
550
* Counts how many characters in strings `a` and `b` are exactly the same and in the same position.
553
551
*
0 commit comments