Skip to content

Commit 50cf61b

Browse files
authoredJan 12, 2025··
feat(ui): allow hide/show node_modules in module graph tab (#7217)
1 parent dccdd55 commit 50cf61b

File tree

2 files changed

+72
-14
lines changed

2 files changed

+72
-14
lines changed
 

‎packages/ui/client/components/FileDetails.vue

+36-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import type { ModuleGraph } from '~/composables/module-graph'
33
import type { Params } from '~/composables/params'
44
import { hasFailedSnapshot } from '@vitest/ws-client'
5+
import { toJSON } from 'flatted'
56
import {
67
browserState,
78
client,
@@ -18,6 +19,7 @@ const draft = ref(false)
1819
const hasGraphBeenDisplayed = ref(false)
1920
const loadingModuleGraph = ref(false)
2021
const currentFilepath = ref<string | undefined>(undefined)
22+
const hideNodeModules = ref(true)
2123
2224
const 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
105128
debouncedWatch(
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 || ''"

‎packages/ui/client/components/views/ViewModuleGraph.vue

+36-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const props = defineProps<{
2121
projectName: string
2222
}>()
2323
24+
const hideNodeModules = defineModel<boolean>({ required: true })
25+
2426
const { graph } = toRefs(props)
2527
2628
const el = ref<HTMLDivElement>()
@@ -46,7 +48,7 @@ onUnmounted(() => {
4648
controller.value?.shutdown()
4749
})
4850
49-
watch(graph, resetGraphController)
51+
watch(graph, () => resetGraphController())
5052
5153
function setFilter(name: ModuleType, value: boolean) {
5254
controller.value?.filterNodesByType(value, name)
@@ -57,8 +59,16 @@ function setSelectedModule(id: string) {
5759
modalShow.value = true
5860
}
5961
60-
function resetGraphController() {
62+
function resetGraphController(reset = false) {
6163
controller.value?.shutdown()
64+
65+
// Force reload the module graph only when node_modules are shown.
66+
// The module graph doesn't contain node_modules entries.
67+
if (reset && !hideNodeModules.value) {
68+
hideNodeModules.value = true
69+
return
70+
}
71+
6272
if (!graph.value || !el.value) {
6373
return
6474
}
@@ -154,6 +164,28 @@ function bindOnClick(
154164
<div h-full min-h-75 flex-1 overflow="hidden">
155165
<div>
156166
<div flex items-center gap-4 px-3 py-2>
167+
<div
168+
flex="~ gap-1"
169+
items-center
170+
select-none
171+
>
172+
<input
173+
id="hide-node-modules"
174+
v-model="hideNodeModules"
175+
type="checkbox"
176+
>
177+
<label
178+
font-light
179+
text-sm
180+
ws-nowrap
181+
overflow-hidden
182+
select-none
183+
truncate
184+
for="hide-node-modules"
185+
border-b-2
186+
border="$cm-namespace"
187+
>Hide node_modules</label>
188+
</div>
157189
<div
158190
v-for="node of controller?.nodeTypes.sort()"
159191
:key="node"
@@ -173,6 +205,7 @@ function bindOnClick(
173205
ws-nowrap
174206
overflow-hidden
175207
capitalize
208+
select-none
176209
truncate
177210
:for="`type-${node}`"
178211
border-b-2
@@ -184,7 +217,7 @@ function bindOnClick(
184217
<IconButton
185218
v-tooltip.bottom="'Reset'"
186219
icon="i-carbon-reset"
187-
@click="resetGraphController"
220+
@click="resetGraphController(true)"
188221
/>
189222
</div>
190223
</div>

0 commit comments

Comments
 (0)
Please sign in to comment.