|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { TreeNodeInput } from 'nanovis' |
| 3 | +import type { PluginBuildInfo, RolldownPluginBuildMetrics, SessionContext } from '~~/shared/types' |
| 4 | +import { Flamegraph, normalizeTreeNode } from 'nanovis' |
| 5 | +import { relative } from 'pathe' |
| 6 | +import { computed, onMounted, onUnmounted, ref, shallowRef, useTemplateRef, watch } from 'vue' |
| 7 | +import { parseReadablePath } from '~/utils/filepath' |
| 8 | +import { normalizeTimestamp } from '~/utils/format' |
| 9 | +import { getFileTypeFromModuleId, ModuleTypeRules } from '~/utils/icon' |
| 10 | +
|
| 11 | +const props = defineProps<{ |
| 12 | + session: SessionContext |
| 13 | + buildMetrics: RolldownPluginBuildMetrics |
| 14 | +}>() |
| 15 | +
|
| 16 | +const parsedPaths = computed(() => props.session.modulesList.map((mod) => { |
| 17 | + const path = parseReadablePath(mod.id, props.session.meta.cwd) |
| 18 | + const type = getFileTypeFromModuleId(mod.id) |
| 19 | + return { |
| 20 | + mod, |
| 21 | + path, |
| 22 | + type, |
| 23 | + } |
| 24 | +})) |
| 25 | +const moduleTypes = computed(() => ModuleTypeRules.filter(rule => parsedPaths.value.some(mod => rule.match.test(mod.mod.id)))) |
| 26 | +
|
| 27 | +const n = (node: TreeNodeInput<PluginBuildInfo>) => normalizeTreeNode(node, undefined, false) |
| 28 | +
|
| 29 | +function normalizeModulePath(path: string) { |
| 30 | + const normalized = path.replace(/%2F/g, '/') |
| 31 | + const cwd = props.session!.meta.cwd |
| 32 | + let relate = cwd ? relative(cwd, normalized) : normalized |
| 33 | + if (!relate.startsWith('.')) |
| 34 | + relate = `./${relate}` |
| 35 | + if (relate.startsWith('./')) |
| 36 | + return relate |
| 37 | + if (relate.match(/^(?:\.\.\/){1,3}[^.]/)) |
| 38 | + return relate |
| 39 | + return normalized |
| 40 | +} |
| 41 | +
|
| 42 | +const tree = computed(() => { |
| 43 | + const resolveIds = moduleTypes.value.map((type, idx) => n({ |
| 44 | + id: `resolveId-${type.name}-${idx}`, |
| 45 | + text: type.description, |
| 46 | + children: props.buildMetrics.resolveIdMetrics.filter((item) => { |
| 47 | + return getFileTypeFromModuleId(item.module).name === type.name |
| 48 | + }).map((id, idx) => n({ |
| 49 | + id: `resolveId-${idx}`, |
| 50 | + text: normalizeModulePath(id.module), |
| 51 | + size: id.duration, |
| 52 | + })), |
| 53 | + })) |
| 54 | + const loads = moduleTypes.value.map((type, idx) => n({ |
| 55 | + id: `loads-${type.name}-${idx}`, |
| 56 | + text: type.description, |
| 57 | + children: props.buildMetrics.loadMetrics.filter((item) => { |
| 58 | + return getFileTypeFromModuleId(item.module).name === type.name |
| 59 | + }).map((id, idx) => n({ |
| 60 | + id: `resolveId-${idx}`, |
| 61 | + text: normalizeModulePath(id.module), |
| 62 | + size: id.duration, |
| 63 | + })), |
| 64 | + })) |
| 65 | + const transforms = moduleTypes.value.map((type, idx) => n({ |
| 66 | + id: `transforms-${type.name}-${idx}`, |
| 67 | + text: type.description, |
| 68 | + children: props.buildMetrics.transformMetrics.filter((item) => { |
| 69 | + return getFileTypeFromModuleId(item.module).name === type.name |
| 70 | + }).map((id, idx) => n({ |
| 71 | + id: `resolveId-${idx}`, |
| 72 | + text: normalizeModulePath(id.module), |
| 73 | + size: id.duration, |
| 74 | + })), |
| 75 | + })) |
| 76 | +
|
| 77 | + // resolve/load/transform -> module type -> module |
| 78 | + const children = [ |
| 79 | + n({ |
| 80 | + id: '~resolves', |
| 81 | + text: 'Resolve Id', |
| 82 | + children: resolveIds, |
| 83 | + }), |
| 84 | + n({ |
| 85 | + id: '~loads', |
| 86 | + text: 'Load', |
| 87 | + children: loads, |
| 88 | + }), |
| 89 | + n({ |
| 90 | + id: '~transforms', |
| 91 | + text: 'Transform', |
| 92 | + children: transforms, |
| 93 | + }), |
| 94 | + ] |
| 95 | +
|
| 96 | + return n({ |
| 97 | + id: '~root', |
| 98 | + text: 'Plugin Flamegraph', |
| 99 | + children, |
| 100 | + }) |
| 101 | +}) |
| 102 | +
|
| 103 | +const hoverNode = ref<{ |
| 104 | + plugin_name: string |
| 105 | + duration: number |
| 106 | + meta: PluginBuildInfo | undefined |
| 107 | +} | null>(null) |
| 108 | +const hoverX = ref<number>(0) |
| 109 | +const hoverY = ref<number>(0) |
| 110 | +const el = useTemplateRef<HTMLDivElement>('el') |
| 111 | +const flamegraph = shallowRef<Flamegraph<PluginBuildInfo> | null>(null) |
| 112 | +
|
| 113 | +function buildFlamegraph() { |
| 114 | + flamegraph.value = new Flamegraph(tree.value, { |
| 115 | + animate: true, |
| 116 | + palette: { |
| 117 | + fg: '#888', |
| 118 | + }, |
| 119 | + getSubtext: (node) => { |
| 120 | + const p = node.size / tree.value.size * 100 |
| 121 | + if (p > 15 && p !== 100) { |
| 122 | + return `${p.toFixed(1)}%` |
| 123 | + } |
| 124 | + return undefined |
| 125 | + }, |
| 126 | + onHover(node, e) { |
| 127 | + if (!node) { |
| 128 | + hoverNode.value = null |
| 129 | + return |
| 130 | + } |
| 131 | + if (e) { |
| 132 | + hoverX.value = e.clientX |
| 133 | + hoverY.value = e.clientY |
| 134 | + } |
| 135 | + hoverNode.value = { |
| 136 | + plugin_name: node.text!, |
| 137 | + duration: node.size, |
| 138 | + meta: node.meta, |
| 139 | + } |
| 140 | + }, |
| 141 | + }) |
| 142 | + el.value!.appendChild(flamegraph.value!.el) |
| 143 | +} |
| 144 | +
|
| 145 | +function disposeFlamegraph() { |
| 146 | + flamegraph.value?.dispose() |
| 147 | +} |
| 148 | +
|
| 149 | +onMounted(() => { |
| 150 | + buildFlamegraph() |
| 151 | +}) |
| 152 | +
|
| 153 | +onUnmounted(() => { |
| 154 | + disposeFlamegraph() |
| 155 | +}) |
| 156 | +
|
| 157 | +watch(tree, async () => { |
| 158 | + disposeFlamegraph() |
| 159 | + buildFlamegraph() |
| 160 | +}, { |
| 161 | + deep: true, |
| 162 | +}) |
| 163 | +</script> |
| 164 | + |
| 165 | +<template> |
| 166 | + <div relative border="t base" pb10 py1 mt4> |
| 167 | + <Teleport to="body"> |
| 168 | + <div |
| 169 | + v-if="hoverNode" |
| 170 | + border="~ base" rounded shadow px2 py1 fixed |
| 171 | + z-panel-content bg-glass pointer-events-none text-sm |
| 172 | + :style="{ left: `${hoverX}px`, top: `${hoverY}px` }" |
| 173 | + > |
| 174 | + <div flex="~" font-bold font-mono> |
| 175 | + <DisplayFileIcon v-if="hoverNode.meta" :filename="hoverNode.meta.module" mr1.5 /> |
| 176 | + {{ hoverNode.plugin_name }} |
| 177 | + </div> |
| 178 | + <div v-if="hoverNode.meta"> |
| 179 | + <div> |
| 180 | + <label>Start Time: </label> |
| 181 | + <time :datetime="new Date(hoverNode.meta.timestamp_start).toISOString()">{{ normalizeTimestamp(hoverNode.meta.timestamp_start) }}</time> |
| 182 | + </div> |
| 183 | + <div> |
| 184 | + <label>End Time: </label> |
| 185 | + <time :datetime="new Date(hoverNode.meta.timestamp_end).toISOString()">{{ normalizeTimestamp(hoverNode.meta.timestamp_end) }}</time> |
| 186 | + </div> |
| 187 | + </div> |
| 188 | + <div flex="~ gap-1"> |
| 189 | + <label>Duration: </label> |
| 190 | + <DisplayDuration :duration="hoverNode.duration" /> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + </Teleport> |
| 194 | + <div ref="el" min-h-30 /> |
| 195 | + </div> |
| 196 | +</template> |
0 commit comments