1
1
import * as path from 'path'
2
- import Module from 'module'
3
2
import findUp from 'find-up'
4
3
import resolveFrom from 'resolve-from'
5
4
import importFrom from 'import-from'
6
5
6
+ let isPnp
7
+ let pnpApi
8
+
7
9
export 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
+
8
18
const pnpPath = findUp . sync (
9
19
( 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' )
11
25
if ( findUp . sync . exists ( pnpFile ) ) {
12
26
return pnpFile
13
27
}
@@ -17,62 +31,47 @@ export function withUserEnvironment(base, root, cb) {
17
31
} ,
18
32
{ cwd : base }
19
33
)
34
+
20
35
if ( pnpPath ) {
21
- return withPnpEnvironment ( pnpPath , cb )
36
+ isPnp = true
37
+ pnpApi = __non_webpack_require__ ( pnpPath )
38
+ pnpApi . setup ( )
39
+ } else {
40
+ isPnp = false
22
41
}
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
35
42
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
+ }
43
45
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 ( / \/ $ / , '' ) + '/' )
46
49
}
47
50
48
51
const pnpRequire = ( request , from ) => {
49
52
return __non_webpack_require__ ( pnpResolve ( request , from ) )
50
53
}
51
54
52
- const res = cb ( { isPnP : true , resolve : pnpResolve , require : pnpRequire } )
55
+ const res = cb ( { isPnp : true , resolve : pnpResolve , require : pnpRequire } )
53
56
54
57
// check if it return a thenable
55
58
if ( res != null && res . then ) {
56
59
return res . then (
57
60
( x ) => {
58
- restore ( )
59
61
return x
60
62
} ,
61
63
( err ) => {
62
- restore ( )
63
64
throw err
64
65
}
65
66
)
66
67
}
67
68
68
- restore ( )
69
-
70
69
return res
71
70
}
72
71
73
72
function withNonPnpEnvironment ( base , cb ) {
74
73
return cb ( {
75
- isPnP : false ,
74
+ isPnp : false ,
76
75
require ( request , from = base ) {
77
76
return importFrom ( from , request )
78
77
} ,
0 commit comments