11import * as path from 'path'
2- import Module from 'module'
32import findUp from 'find-up'
43import resolveFrom from 'resolve-from'
54import importFrom from 'import-from'
65
6+ let isPnp
7+ let pnpApi
8+
79export function withUserEnvironment ( base , root , cb ) {
10+ if ( isPnp === true ) {
11+ return withPnpEnvironment ( base , cb )
12+ }
13+
14+ if ( isPnp === false ) {
15+ return withNonPnpEnvironment ( base , cb )
16+ }
17+
818 const pnpPath = findUp . sync (
919 ( dir ) => {
10- const pnpFile = path . join ( dir , '.pnp.js' )
20+ let pnpFile = path . join ( dir , '.pnp.js' )
21+ if ( findUp . sync . exists ( pnpFile ) ) {
22+ return pnpFile
23+ }
24+ pnpFile = path . join ( dir , '.pnp.cjs' )
1125 if ( findUp . sync . exists ( pnpFile ) ) {
1226 return pnpFile
1327 }
@@ -17,62 +31,47 @@ export function withUserEnvironment(base, root, cb) {
1731 } ,
1832 { cwd : base }
1933 )
34+
2035 if ( pnpPath ) {
21- return withPnpEnvironment ( pnpPath , cb )
36+ isPnp = true
37+ pnpApi = __non_webpack_require__ ( pnpPath )
38+ pnpApi . setup ( )
39+ } else {
40+ isPnp = false
2241 }
23- return withNonPnpEnvironment ( base , cb )
24- }
25-
26- function withPnpEnvironment ( pnpPath , cb ) {
27- const basePath = path . dirname ( pnpPath )
28-
29- // pnp will patch `module` and `fs` to load package in pnp environment
30- // backup the functions which will be patched here
31- const originalModule = Object . create ( null )
32- originalModule . _load = Module . _load
33- originalModule . _resolveFilename = Module . _resolveFilename
34- originalModule . _findPath = Module . _findPath
3542
36- const pnpapi = __non_webpack_require__ ( pnpPath )
37-
38- // get into pnp environment
39- pnpapi . setup ( )
40-
41- // restore the patched function, we can not load any package after called this
42- const restore = ( ) => Object . assign ( Module , originalModule )
43+ return withUserEnvironment ( base , root , cb )
44+ }
4345
44- const pnpResolve = ( request , from = basePath ) => {
45- return pnpapi . resolveRequest ( request , from + '/' )
46+ function withPnpEnvironment ( base , cb ) {
47+ const pnpResolve = ( request , from = base ) => {
48+ return pnpApi . resolveRequest ( request , from . replace ( / \/ $ / , '' ) + '/' )
4649 }
4750
4851 const pnpRequire = ( request , from ) => {
4952 return __non_webpack_require__ ( pnpResolve ( request , from ) )
5053 }
5154
52- const res = cb ( { isPnP : true , resolve : pnpResolve , require : pnpRequire } )
55+ const res = cb ( { isPnp : true , resolve : pnpResolve , require : pnpRequire } )
5356
5457 // check if it return a thenable
5558 if ( res != null && res . then ) {
5659 return res . then (
5760 ( x ) => {
58- restore ( )
5961 return x
6062 } ,
6163 ( err ) => {
62- restore ( )
6364 throw err
6465 }
6566 )
6667 }
6768
68- restore ( )
69-
7069 return res
7170}
7271
7372function withNonPnpEnvironment ( base , cb ) {
7473 return cb ( {
75- isPnP : false ,
74+ isPnp : false ,
7675 require ( request , from = base ) {
7776 return importFrom ( from , request )
7877 } ,
0 commit comments