2
2
import type { ModuleGraph } from ' ~/composables/module-graph'
3
3
import type { Params } from ' ~/composables/params'
4
4
import { hasFailedSnapshot } from ' @vitest/ws-client'
5
+ import { toJSON } from ' flatted'
5
6
import {
6
7
browserState ,
7
8
client ,
@@ -18,6 +19,7 @@ const draft = ref(false)
18
19
const hasGraphBeenDisplayed = ref (false )
19
20
const loadingModuleGraph = ref (false )
20
21
const currentFilepath = ref <string | undefined >(undefined )
22
+ const hideNodeModules = ref (true )
21
23
22
24
const graphData = computed (() => {
23
25
const c = current .value
@@ -61,10 +63,12 @@ function onDraft(value: boolean) {
61
63
draft .value = value
62
64
}
63
65
64
- async function loadModuleGraph() {
66
+ const nodeModuleRegex = / [/\\ ] node_modules[/\\ ] /
67
+
68
+ async function loadModuleGraph(force = false ) {
65
69
if (
66
70
loadingModuleGraph .value
67
- || graphData .value ?.filepath === currentFilepath .value
71
+ || ( graphData .value ?.filepath === currentFilepath .value && ! force )
68
72
) {
69
73
return
70
74
}
@@ -76,20 +80,39 @@ async function loadModuleGraph() {
76
80
try {
77
81
const gd = graphData .value
78
82
if (! gd ) {
83
+ loadingModuleGraph .value = false
79
84
return
80
85
}
81
86
82
87
if (
83
- ! currentFilepath .value
88
+ force
89
+ || ! currentFilepath .value
84
90
|| gd .filepath !== currentFilepath .value
85
91
|| (! graph .value .nodes .length && ! graph .value .links .length )
86
92
) {
93
+ let moduleGraph = await client .rpc .getModuleGraph (
94
+ gd .projectName ,
95
+ gd .filepath ,
96
+ !! browserState ,
97
+ )
98
+ // remove node_modules from the graph when enabled
99
+ if (hideNodeModules .value ) {
100
+ // when using static html reporter, we've the meta as global, we need to clone it
101
+ if (isReport ) {
102
+ moduleGraph
103
+ = typeof window .structuredClone !== ' undefined'
104
+ ? window .structuredClone (moduleGraph )
105
+ : toJSON (moduleGraph )
106
+ }
107
+ moduleGraph .inlined = moduleGraph .inlined .filter (
108
+ n => ! nodeModuleRegex .test (n ),
109
+ )
110
+ moduleGraph .externalized = moduleGraph .externalized .filter (
111
+ n => ! nodeModuleRegex .test (n ),
112
+ )
113
+ }
87
114
graph .value = getModuleGraph (
88
- await client .rpc .getModuleGraph (
89
- gd .projectName ,
90
- gd .filepath ,
91
- !! browserState ,
92
- ),
115
+ moduleGraph ,
93
116
gd .filepath ,
94
117
)
95
118
currentFilepath .value = gd .filepath
@@ -103,10 +126,11 @@ async function loadModuleGraph() {
103
126
}
104
127
105
128
debouncedWatch (
106
- () => [graphData .value , viewMode .value ] as const ,
107
- ([, vm ] ) => {
129
+ () => [graphData .value , viewMode .value , hideNodeModules . value ] as const ,
130
+ ([, vm , hide ], old ) => {
108
131
if (vm === ' graph' ) {
109
- loadModuleGraph ()
132
+ // only force reload when hide is changed
133
+ loadModuleGraph (old && hide !== old [2 ])
110
134
}
111
135
},
112
136
{ debounce: 100 , immediate: true },
@@ -221,6 +245,7 @@ const projectNameTextColor = computed(() => {
221
245
<div v-if =" hasGraphBeenDisplayed" :flex-1 =" viewMode === 'graph' && ''" >
222
246
<ViewModuleGraph
223
247
v-show =" viewMode === 'graph' && !loadingModuleGraph"
248
+ v-model =" hideNodeModules"
224
249
:graph =" graph"
225
250
data-testid =" graph"
226
251
:project-name =" current.file.projectName || ''"
0 commit comments