22import type { ModuleGraph } from ' ~/composables/module-graph'
33import type { Params } from ' ~/composables/params'
44import { hasFailedSnapshot } from ' @vitest/ws-client'
5+ import { toJSON } from ' flatted'
56import {
67 browserState ,
78 client ,
@@ -18,6 +19,7 @@ const draft = ref(false)
1819const hasGraphBeenDisplayed = ref (false )
1920const loadingModuleGraph = ref (false )
2021const currentFilepath = ref <string | undefined >(undefined )
22+ const hideNodeModules = ref (true )
2123
2224const graphData = computed (() => {
2325 const c = current .value
@@ -61,10 +63,12 @@ function onDraft(value: boolean) {
6163 draft .value = value
6264}
6365
64- async function loadModuleGraph() {
66+ const nodeModuleRegex = / [/\\ ] node_modules[/\\ ] /
67+
68+ async function loadModuleGraph(force = false ) {
6569 if (
6670 loadingModuleGraph .value
67- || graphData .value ?.filepath === currentFilepath .value
71+ || ( graphData .value ?.filepath === currentFilepath .value && ! force )
6872 ) {
6973 return
7074 }
@@ -76,20 +80,39 @@ async function loadModuleGraph() {
7680 try {
7781 const gd = graphData .value
7882 if (! gd ) {
83+ loadingModuleGraph .value = false
7984 return
8085 }
8186
8287 if (
83- ! currentFilepath .value
88+ force
89+ || ! currentFilepath .value
8490 || gd .filepath !== currentFilepath .value
8591 || (! graph .value .nodes .length && ! graph .value .links .length )
8692 ) {
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+ }
87114 graph .value = getModuleGraph (
88- await client .rpc .getModuleGraph (
89- gd .projectName ,
90- gd .filepath ,
91- !! browserState ,
92- ),
115+ moduleGraph ,
93116 gd .filepath ,
94117 )
95118 currentFilepath .value = gd .filepath
@@ -103,10 +126,11 @@ async function loadModuleGraph() {
103126}
104127
105128debouncedWatch (
106- () => [graphData .value , viewMode .value ] as const ,
107- ([, vm ] ) => {
129+ () => [graphData .value , viewMode .value , hideNodeModules . value ] as const ,
130+ ([, vm , hide ], old ) => {
108131 if (vm === ' graph' ) {
109- loadModuleGraph ()
132+ // only force reload when hide is changed
133+ loadModuleGraph (old && hide !== old [2 ])
110134 }
111135 },
112136 { debounce: 100 , immediate: true },
@@ -221,6 +245,7 @@ const projectNameTextColor = computed(() => {
221245 <div v-if =" hasGraphBeenDisplayed" :flex-1 =" viewMode === 'graph' && ''" >
222246 <ViewModuleGraph
223247 v-show =" viewMode === 'graph' && !loadingModuleGraph"
248+ v-model =" hideNodeModules"
224249 :graph =" graph"
225250 data-testid =" graph"
226251 :project-name =" current.file.projectName || ''"
0 commit comments