@@ -13,25 +13,43 @@ const debugDetail = require('debug')('webpack-plugin/details');
13
13
* this global var is reset every run in case of circular dependencies between files
14
14
*/
15
15
let modulesProcessed = [ ] ;
16
- /**
17
- * this is used for displaying logs only
18
- */
19
- let optionsGlobal = { } ;
20
16
let baseVendorPkg ;
21
17
let moduleRootOverride = { } ;
22
18
let modulePaths = [ ] ;
23
19
let moduleNames = [ ] ;
24
20
25
- function installedRootModulePaths ( ) {
26
- return fileSystem . readdirSync ( path . join ( optionsGlobal . root , 'node_modules' ) )
27
- . filter ( dir => ! ( / ^ \. / . test ( dir ) ) )
28
- . map ( dir => path . resolve ( optionsGlobal . root , 'node_modules' , dir ) ) ;
21
+ function installedRootModulePaths ( moduleDir , ensurePackageJson = true ) {
22
+ let rootModules = fileSystem . readdirSync ( moduleDir )
23
+ . filter ( dir => ! ( / ^ \. / . test ( dir ) ) ) ;
24
+
25
+ let scoped = rootModules
26
+ . filter ( dir => dir . indexOf ( '@' ) === 0 ) ;
27
+
28
+ rootModules = rootModules
29
+ . filter ( dir => dir . indexOf ( '@' ) !== 0 )
30
+ . map ( dir => path . resolve ( moduleDir , dir ) ) ;
31
+
32
+ scoped . forEach ( dir => {
33
+ rootModules = rootModules . concat ( installedRootModulePaths ( dir , false ) ) ;
34
+ } ) ;
35
+
36
+ if ( ensurePackageJson ) {
37
+ // ensure package.json exists:
38
+ rootModules = rootModules
39
+ . filter ( dir => {
40
+ let stats ;
41
+ try { stats = fileSystem . statSync ( path . join ( dir , 'package.json' ) ) } catch ( _ ) { }
42
+ return stats && stats . isFile ( ) ;
43
+ } ) ;
44
+ }
45
+
46
+ return rootModules ;
29
47
}
30
48
31
- function installedLocalModulePaths ( ) {
32
- return execa ( 'npm' , [ 'ls' , '--parseable' ] , { cwd : optionsGlobal . root } )
33
- . then ( res => installedRootModulePaths ( ) . concat ( res . stdout . split ( '\n' ) . filter ( ( line , i ) => i !== 0 && ! ! line ) ) )
34
- . catch ( res => installedRootModulePaths ( ) . concat ( res . stdout . split ( '\n' ) . filter ( ( line , i ) => i !== 0 && ! ! line ) ) ) ;
49
+ function installedLocalModulePaths ( options ) {
50
+ return execa ( 'npm' , [ 'ls' , '--parseable' ] , { cwd : options . root } )
51
+ . then ( res => installedRootModulePaths ( path . join ( options . root , 'node_modules' ) ) . concat ( res . stdout . split ( '\n' ) . filter ( ( line , i ) => i !== 0 && ! ! line ) ) )
52
+ . catch ( res => installedRootModulePaths ( path . join ( options . root , 'node_modules' ) ) . concat ( res . stdout . split ( '\n' ) . filter ( ( line , i ) => i !== 0 && ! ! line ) ) ) ;
35
53
}
36
54
37
55
function getFilesRecursively ( targetDir , extension ) {
@@ -61,15 +79,14 @@ function getFilesRecursively(targetDir, extension) {
61
79
*/
62
80
export async function processAll ( options ) {
63
81
modulesProcessed = [ ] ;
64
- optionsGlobal = options ;
65
82
const dependencies = { } ;
66
83
const nodeModules = path . join ( options . root , 'node_modules' ) ;
67
84
const packageJson = path . join ( options . root , 'package.json' ) ;
68
85
69
86
debugDetail ( `starting resolution: ${ options . root } ` ) ;
70
87
71
88
if ( modulePaths . length === 0 ) {
72
- modulePaths = ( await installedLocalModulePaths ( ) )
89
+ modulePaths = ( await installedLocalModulePaths ( options ) )
73
90
. map ( line => path . normalize ( line ) ) ;
74
91
moduleNames = modulePaths
75
92
. map ( line => {
0 commit comments