@@ -15,6 +15,40 @@ ctx.addEventListener('message', (ev: MessageEvent<Args>) => {
15
15
function createDepGraph ( args : Args ) : DepGraph {
16
16
const { moduleDeps, sourceModules, targetModules } = args ;
17
17
18
+ if ( sourceModules . length === 0 ) {
19
+ const { vertices, edges } = bfs (
20
+ targetModules ,
21
+ module => moduleDeps . importedBy [ module ] ?. map ( ( { path } ) => path ) ?? [ ] ,
22
+ ) ;
23
+ return {
24
+ modules : vertices . map ( module => moduleDeps . modules [ module ] ) ,
25
+ imports : edges . map ( ( { from, to } ) => ( {
26
+ fromPath : from ,
27
+ toPath : to ,
28
+ isDynamic :
29
+ moduleDeps . importedBy [ from ] . find ( ( { path } ) => path === to )
30
+ ?. isDynamic ?? false ,
31
+ } ) ) ,
32
+ } ;
33
+ }
34
+
35
+ if ( targetModules . length === 0 ) {
36
+ const { vertices, edges } = bfs (
37
+ sourceModules ,
38
+ module => moduleDeps . deps [ module ] ?. map ( ( { path } ) => path ) ?? [ ] ,
39
+ ) ;
40
+ return {
41
+ modules : vertices . map ( module => moduleDeps . modules [ module ] ) ,
42
+ imports : edges . map ( ( { from, to } ) => ( {
43
+ fromPath : from ,
44
+ toPath : to ,
45
+ isDynamic :
46
+ moduleDeps . deps [ from ] . find ( ( { path } ) => path === to ) ?. isDynamic ??
47
+ false ,
48
+ } ) ) ,
49
+ } ;
50
+ }
51
+
18
52
const paths = findAllPaths (
19
53
targetModules ,
20
54
sourceModules ,
@@ -48,7 +82,36 @@ function createDepGraph(args: Args): DepGraph {
48
82
} ;
49
83
}
50
84
51
- function findAllPaths < T > ( from : T [ ] , to : T [ ] , adjacent : ( vertex : T ) => T [ ] ) {
85
+ function bfs < T > (
86
+ from : T [ ] ,
87
+ adjacent : ( vertex : T ) => T [ ] ,
88
+ ) : {
89
+ vertices : T [ ] ;
90
+ edges : { from : T ; to : T } [ ] ;
91
+ } {
92
+ const queue = from ;
93
+ const discovered = new Set ( from ) ;
94
+ const vertices : T [ ] = [ ] ;
95
+ const edges : { from : T ; to : T } [ ] = [ ] ;
96
+ while ( queue . length > 0 ) {
97
+ const vertex = queue . shift ( ) ! ;
98
+ vertices . push ( vertex ) ;
99
+ for ( const other of adjacent ( vertex ) ) {
100
+ edges . push ( { from : vertex , to : other } ) ;
101
+ if ( ! discovered . has ( other ) ) {
102
+ queue . push ( other ) ;
103
+ discovered . add ( other ) ;
104
+ }
105
+ }
106
+ }
107
+ return { vertices, edges } ;
108
+ }
109
+
110
+ function findAllPaths < T > (
111
+ from : T [ ] ,
112
+ to : T [ ] ,
113
+ adjacent : ( vertex : T ) => T [ ] ,
114
+ ) : T [ ] [ ] {
52
115
const ends = new Set ( to ) ;
53
116
const isPathEnd = ( vertex : T ) => ends . has ( vertex ) ;
54
117
@@ -69,7 +132,7 @@ function findAllPathsUtil<T>(
69
132
visited : Set < T > ,
70
133
path : T [ ] ,
71
134
paths : T [ ] [ ] ,
72
- ) {
135
+ ) : void {
73
136
visited . add ( vertex ) ;
74
137
path . push ( vertex ) ;
75
138
0 commit comments