@@ -14,7 +14,13 @@ const StringDecoder = require('string_decoder');
14
14
const ignorePattern = $$BLACKLIST ? new RegExp ( $$BLACKLIST ) : null ;
15
15
16
16
const builtinModules = new Set ( Module . builtinModules || Object . keys ( process . binding ( 'natives' ) ) ) ;
17
+
18
+ const topLevelLocator = { name : null , reference : null } ;
19
+ const blacklistedLocator = { name : NaN , reference : NaN } ;
20
+
21
+ // Used for compatibility purposes - cf setupCompatibilityLayer
17
22
const patchedModules = new Map ( ) ;
23
+ const fallbackLocators = [ topLevelLocator ] ;
18
24
19
25
// Splits a require request into its components, or return null if the request is a file path
20
26
const pathRegExp = / ^ (? ! \. { 0 , 2 } (?: \/ | $ ) ) ( (?: @ [ ^ \/ ] + \/ ) ? [ ^ \/ ] + ) \/ ? ( .* | ) $ / ;
@@ -26,9 +32,7 @@ const isStrictRegExp = /^\.{0,2}\//;
26
32
// Matches if the path must point to a directory (ie ends with /)
27
33
const isDirRegExp = / \/ $ / ;
28
34
29
- const topLevelLocator = { name : null , reference : null } ;
30
- const blacklistedLocator = { name : NaN , reference : NaN } ;
31
-
35
+ // Keep a reference around ("module" is a common name in this context, so better rename it to something more significant)
32
36
const pnpModule = module ;
33
37
34
38
/**
@@ -374,13 +378,15 @@ exports.resolveToUnqualified = function resolveToUnqualified(request, issuer, {c
374
378
375
379
let dependencyReference = issuerInformation . packageDependencies . get ( dependencyName ) ;
376
380
377
- // If we can't find it, we check if we can potentially load it from the top-level packages
378
- // it 's a bit of a hack, but it improves compatibility with the existing Node ecosystem. Hopefully we should
379
- // eventually be able to kill it and become stricter once pnp gets enough traction
381
+ // If we can't find it, we check if we can potentially load it from the packages that have been defined as potential fallbacks.
382
+ // It 's a bit of a hack, but it improves compatibility with the existing Node ecosystem. Hopefully we should eventually be able
383
+ // to kill this logic and become stricter once pnp gets enough traction and the affected packages fix themselves.
380
384
381
- if ( dependencyReference === undefined ) {
382
- const topLevelInformation = getPackageInformationSafe ( topLevelLocator ) ;
383
- dependencyReference = topLevelInformation . packageDependencies . get ( dependencyName ) ;
385
+ if ( issuerLocator !== topLevelLocator ) {
386
+ for ( let t = 0 , T = fallbackLocators . length ; dependencyReference === undefined && t < T ; ++ t ) {
387
+ const fallbackInformation = getPackageInformationSafe ( fallbackLocators [ t ] ) ;
388
+ dependencyReference = fallbackInformation . packageDependencies . get ( dependencyName ) ;
389
+ }
384
390
}
385
391
386
392
// If we can't find the path, and if the package making the request is the top-level, we can offer nicer error messages
@@ -673,12 +679,26 @@ exports.setupCompatibilityLayer = () => {
673
679
return stack [ 2 ] . getFileName ( ) ;
674
680
} ;
675
681
682
+ // ESLint currently doesn't have any portable way for shared configs to specify their own
683
+ // plugins that should be used (https://github.com/eslint/eslint/issues/10125). This will
684
+ // likely get fixed at some point, but it'll take time and in the meantime we'll just add
685
+ // additional fallback entries for common shared configs.
686
+
687
+ for ( const name of [ `react-scripts` ] ) {
688
+ const packageInformationStore = packageInformationStores . get ( name ) ;
689
+ if ( packageInformationStore ) {
690
+ for ( const reference of packageInformationStore . keys ( ) ) {
691
+ fallbackLocators . push ( { name, reference} ) ;
692
+ }
693
+ }
694
+ }
695
+
676
696
// We need to shim the "resolve" module, because Liftoff uses it in order to find the location
677
697
// of the module in the dependency tree. And Liftoff is used to power Gulp, which doesn't work
678
698
// at all unless modulePath is set, which we cannot configure from any other way than through
679
699
// the Liftoff pipeline (the key isn't whitelisted for env or cli options).
680
700
681
- patchedModules . set ( ' resolve' , realResolve => {
701
+ patchedModules . set ( / ^ r e s o l v e $ / , realResolve => {
682
702
const mustBeShimmed = caller => {
683
703
const callerLocator = exports . findPackageLocator ( caller ) ;
684
704
@@ -754,11 +774,14 @@ exports.setupCompatibilityLayer = () => {
754
774
} ;
755
775
756
776
if ( module . parent && module . parent . id === 'internal/preload' ) {
757
- exports . setup ( ) ;
758
777
exports . setupCompatibilityLayer ( ) ;
778
+
779
+ exports . setup ( ) ;
759
780
}
760
781
761
782
if ( process . mainModule === module ) {
783
+ exports . setupCompatibilityLayer ( ) ;
784
+
762
785
const reportError = ( code , message , data ) => {
763
786
process . stdout . write ( `${ JSON . stringify ( [ { code, message, data} , null ] ) } \n` ) ;
764
787
} ;
0 commit comments