@@ -13,14 +13,39 @@ function getRelativePath(filename) {
13
13
return result ;
14
14
}
15
15
16
+ function getNodeModuleName ( filename ) {
17
+ let foundNodeModules = false ;
18
+ let moduleName = [ ] ;
19
+
20
+ let s = filename . split ( path . sep ) ;
21
+ for ( let entry of s ) {
22
+ if ( foundNodeModules ) {
23
+ moduleName . push ( entry ) ;
24
+ if ( ! entry . startsWith ( "@" ) ) {
25
+ return moduleName . join ( "/" ) ;
26
+ }
27
+ }
28
+
29
+ if ( entry === "node_modules" ) {
30
+ foundNodeModules = true ;
31
+ }
32
+ }
33
+
34
+ return false ;
35
+ }
36
+
16
37
/* unordered */
17
- function getDependenciesFor ( filename , avoidCircular , allowNotFound = false ) {
38
+ function getDependenciesFor ( filename , avoidCircular , optionsArg = { } ) {
39
+ let options = Object . assign ( {
40
+ allowNotFound : false ,
41
+ nodeModuleNamesOnly : false ,
42
+ } , optionsArg ) ;
18
43
let absoluteFilename = getAbsolutePath ( filename )
19
44
20
45
try {
21
46
require ( absoluteFilename ) ;
22
47
} catch ( e ) {
23
- if ( e . code === "MODULE_NOT_FOUND" && allowNotFound ) {
48
+ if ( e . code === "MODULE_NOT_FOUND" && options . allowNotFound ) {
24
49
// do nothing
25
50
} else {
26
51
throw e ;
@@ -39,14 +64,14 @@ function getDependenciesFor(filename, avoidCircular, allowNotFound = false) {
39
64
let dependencies = new Set ( ) ;
40
65
41
66
if ( ! mod ) {
42
- if ( ! allowNotFound ) {
67
+ if ( ! options . allowNotFound ) {
43
68
throw new Error ( `Could not find ${ filename } in @11ty/dependency-tree` ) ;
44
69
}
45
70
} else {
46
71
let relativeFilename = getRelativePath ( mod . filename ) ;
47
72
if ( ! avoidCircular ) {
48
73
avoidCircular = { } ;
49
- } else {
74
+ } else if ( ! options . nodeModuleNamesOnly ) {
50
75
dependencies . add ( relativeFilename ) ;
51
76
}
52
77
@@ -55,10 +80,14 @@ function getDependenciesFor(filename, avoidCircular, allowNotFound = false) {
55
80
if ( mod . children ) {
56
81
for ( let child of mod . children ) {
57
82
let relativeChildFilename = getRelativePath ( child . filename ) ;
58
- if ( relativeChildFilename . indexOf ( "node_modules" ) === - 1 && // filter out node_modules
83
+ let nodeModuleName = getNodeModuleName ( child . filename ) ;
84
+
85
+ if ( options . nodeModuleNamesOnly && nodeModuleName ) {
86
+ dependencies . add ( nodeModuleName ) ;
87
+ } else if ( nodeModuleName === false && // filter out node_modules
59
88
! dependencies . has ( relativeChildFilename ) && // avoid infinite looping with circular deps
60
89
! avoidCircular [ relativeChildFilename ] ) {
61
- for ( let dependency of getDependenciesFor ( relativeChildFilename , avoidCircular , allowNotFound ) ) {
90
+ for ( let dependency of getDependenciesFor ( relativeChildFilename , avoidCircular , options ) ) {
62
91
dependencies . add ( dependency ) ;
63
92
}
64
93
}
@@ -70,7 +99,8 @@ function getDependenciesFor(filename, avoidCircular, allowNotFound = false) {
70
99
}
71
100
72
101
function getCleanDependencyListFor ( filename , options = { } ) {
73
- return Array . from ( getDependenciesFor ( filename , null , options . allowNotFound ) ) ;
102
+ return Array . from ( getDependenciesFor ( filename , null , options ) ) ;
74
103
}
75
104
76
105
module . exports = getCleanDependencyListFor ;
106
+ module . exports . getNodeModuleName = getNodeModuleName ;
0 commit comments