Skip to content

Commit 5cc5b18

Browse files
committed
feat: visualize dynamic imports
1 parent d4a1875 commit 5cc5b18

6 files changed

Lines changed: 1544 additions & 937 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"type": "module",
33
"version": "0.0.0-alpha.0",
44
"private": true,
5-
"packageManager": "pnpm@10.11.0",
5+
"packageManager": "pnpm@10.11.1",
66
"scripts": {
77
"build": "pnpm -r run build",
88
"build:debug": "NUXT_DEBUG_BUILD=true pnpm -r run build",

packages/devtools/src/app/components/modules/Graph.vue

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import type { HierarchyLink, HierarchyNode } from 'd3-hierarchy'
3-
import type { ModuleListItem, SessionContext } from '../../types/data'
3+
import type { ModuleImport, ModuleListItem, SessionContext } from '../../types/data'
44
import { useEventListener } from '@vueuse/core'
55
import { hierarchy, tree } from 'd3-hierarchy'
66
import { linkHorizontal, linkVertical } from 'd3-shape'
@@ -10,8 +10,15 @@ const props = defineProps<{
1010
session: SessionContext
1111
modules: ModuleListItem[]
1212
}>()
13-
type Link = HierarchyLink<ModuleListItem> & {
13+
14+
interface Node {
15+
module: ModuleListItem
16+
import?: ModuleImport
17+
}
18+
19+
type Link = HierarchyLink<Node> & {
1420
id: string
21+
import?: ModuleImport
1522
}
1623
1724
const graphRender = ref<'normal' | 'mini'>('normal')
@@ -31,9 +38,9 @@ const height = ref(window.innerHeight)
3138
const scale = ref(1)
3239
const nodesRefMap = shallowReactive(new Map<string, HTMLDivElement>())
3340
34-
const nodes = shallowRef<HierarchyNode<ModuleListItem>[]>([])
41+
const nodes = shallowRef<HierarchyNode<Node>[]>([])
3542
const links = shallowRef<Link[]>([])
36-
const nodesMap = shallowReactive(new Map<string, HierarchyNode<ModuleListItem>>())
43+
const nodesMap = shallowReactive(new Map<string, HierarchyNode<Node>>())
3744
const linksMap = shallowReactive(new Map<string, Link>())
3845
3946
const modulesMap = computed(() => {
@@ -62,29 +69,34 @@ function calculateGraph() {
6269
height.value = window.innerHeight
6370
6471
const seen = new Set<ModuleListItem>()
65-
const root = hierarchy<ModuleListItem>(
66-
{ id: '~root' } as any,
67-
(node) => {
68-
if (node.id === '~root') {
72+
const root = hierarchy<Node>(
73+
{ module: { id: '~root' } } as any,
74+
(parent) => {
75+
if (parent.module.id === '~root') {
6976
rootModules.value.forEach(x => seen.add(x))
70-
return rootModules.value
77+
return rootModules.value.map(x => ({ module: x }))
7178
}
72-
const modules = node.imports.map((x) => {
73-
const module = modulesMap.value.get(x.id)
74-
if (module) {
75-
if (seen.has(module)) {
79+
const modules = parent.module.imports
80+
.map((x): Node | undefined => {
81+
const module = modulesMap.value.get(x.id)
82+
if (!module)
7683
return undefined
77-
}
84+
if (seen.has(module))
85+
return undefined
86+
7887
seen.add(module)
79-
}
80-
return module
81-
}).filter(x => x !== undefined)
88+
return {
89+
module,
90+
import: x,
91+
}
92+
})
93+
.filter(x => x !== undefined)
8294
return modules
8395
},
8496
)
8597
8698
// Calculate the layout
87-
const layout = tree<ModuleListItem>()
99+
const layout = tree<Node>()
88100
.nodeSize([SPACING.height, SPACING.width + SPACING.gap])
89101
layout(root)
90102
@@ -111,14 +123,15 @@ function calculateGraph() {
111123
nodes.value = _nodes
112124
nodesMap.clear()
113125
for (const node of _nodes) {
114-
nodesMap.set(node.data.id, node)
126+
nodesMap.set(node.data.module.id, node)
115127
}
116128
const _links = root.links()
117-
.filter(x => x.source.data.id !== '~root')
118-
.map((x) => {
129+
.filter(x => x.source.data.module.id !== '~root')
130+
.map((x): Link => {
119131
return {
120132
...x,
121-
id: `${x.source.data.id}|${x.target.data.id}`,
133+
import: x.source.data.import,
134+
id: `${x.source.data.module.id}|${x.target.data.module.id}`,
122135
}
123136
})
124137
linksMap.clear()
@@ -221,6 +234,7 @@ onMounted(() => {
221234
:key="link.id"
222235
:d="generateLink(link)!"
223236
:class="getLinkColor(link)"
237+
:stroke-dasharray="link.import?.kind === 'dynamic-import' ? '3 6' : undefined"
224238
fill="none"
225239
/>
226240
</g>
@@ -238,17 +252,17 @@ onMounted(() => {
238252
</svg> -->
239253
<template
240254
v-for="node of nodes"
241-
:key="node.data.id"
255+
:key="node.data.module.id"
242256
>
243-
<template v-if="node.data.id !== '~root'">
257+
<template v-if="node.data.module.id !== '~root'">
244258
<DisplayModuleId
245-
:id="node.data.id"
246-
:ref="(el: any) => nodesRefMap.set(node.data.id, el?.$el)"
259+
:id="node.data.module.id"
260+
:ref="(el: any) => nodesRefMap.set(node.data.module.id, el?.$el)"
247261
absolute hover="bg-active" block px2 p1 bg-glass z-graph-node
248262
border="~ base rounded"
249263
:link="true"
250264
:session="session"
251-
:pkg="node.data"
265+
:pkg="node.data.module"
252266
:minimal="true"
253267
:style="{
254268
left: `${node.x}px`,

packages/devtools/src/app/types/data.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { ModuleImport } from '@rolldown/debug'
22

3+
export type { ModuleImport }
4+
35
export interface ModuleListItem {
46
id: string
57
fileType: string

packages/devtools/src/node/rpc/functions/rolldown-get-session-summary.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@ export const rolldownGetSessionSummary = defineRpcFunction({
1010
handler: async ({ session }: { session: string }) => {
1111
const reader = RolldownEventsReader.get(join(cwd, '.rolldown', session, 'logs.json'))
1212
await reader.read()
13+
14+
// TODO: read from meta.json
15+
const plugins = new Set(
16+
reader.manager.events.map(e => 'plugin_name' in e ? e.plugin_name : null)
17+
.filter(x => !!x),
18+
)
19+
1320
return {
1421
id: session,
1522
rootDir: cwd,
23+
plugins: Array.from(plugins),
1624
modules: Array.from(reader.manager.modules.values())
1725
.sort((a, b) => a.id.localeCompare(b.id)),
1826
}

0 commit comments

Comments
 (0)