Skip to content

Commit 97d38c7

Browse files
webfansplzantfu
andauthored
feat: build flow duration and file size changes (#38)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent b458653 commit 97d38c7

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

packages/devtools/src/app/components/data/ModuleDetailsLoader.vue

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import type { ModuleInfo, RolldownModuleTransformInfo, SessionContext } from '~~/shared/types'
33
import { useRpc } from '#imports'
44
import { computedAsync } from '@vueuse/core'
5-
import { nextTick, ref, watchEffect } from 'vue'
5+
import { computed, nextTick, ref, watchEffect } from 'vue'
6+
import { getContentByteSize } from '~~/app/utils/format'
67
78
const props = defineProps<{
89
session: SessionContext
@@ -44,6 +45,41 @@ const info = computedAsync(async () => {
4445
} as ModuleInfo
4546
})
4647
48+
const durations = computed(() => {
49+
const data = info.value
50+
const _resolveIds = data?.resolve_ids.reduce((t, node) => {
51+
t += node.duration
52+
return t
53+
}, 0) ?? 0
54+
const _loads = data?.loads?.reduce((t, node) => {
55+
t += node.duration
56+
return t
57+
}, 0) ?? 0
58+
const _transforms = data?.transforms.reduce((t, node) => {
59+
t += node.duration
60+
return t
61+
}, 0) ?? 0
62+
const total = _resolveIds + _loads + _transforms
63+
return {
64+
resolveIds: _resolveIds,
65+
loads: _loads,
66+
transforms: _transforms,
67+
total,
68+
}
69+
})
70+
71+
const sourceCodeSize = computed(() => {
72+
const data = info.value?.transforms
73+
const source = data?.[0]?.content_from ?? ''
74+
return getContentByteSize(source)
75+
})
76+
77+
const transformedCodeSize = computed(() => {
78+
const data = info.value?.transforms?.filter(t => t.content_to)?.reverse()
79+
const source = data?.[0]?.content_to ?? ''
80+
return getContentByteSize(source)
81+
})
82+
4783
function selectFlowNode(v: boolean) {
4884
flowNodeSelected.value = v
4985
}
@@ -60,7 +96,40 @@ function selectFlowNode(v: boolean) {
6096
border="~ base rounded-lg"
6197
flex="~ col gap-2"
6298
>
63-
<DisplayModuleId :id="module" px2 py1 :session />
99+
<DisplayModuleId :id="module" px2 pt1 :session />
100+
<div text-xs font-mono flex="~ items-center gap-3" ml2>
101+
<DisplayDuration
102+
:duration="durations.resolveIds" flex="~ gap-1 items-center"
103+
:title="`resolveId hooks cost: ${durations.resolveIds}ms`"
104+
>
105+
<span i-ph-magnifying-glass-duotone inline-block />
106+
</DisplayDuration>
107+
<DisplayDuration
108+
:duration="durations.loads" flex="~ gap-1 items-center"
109+
:title="`load hooks cost: ${durations.loads}ms`"
110+
>
111+
<span i-ph-upload-simple-duotone inline-block />
112+
</DisplayDuration>
113+
<DisplayDuration
114+
:duration="durations.transforms" flex="~ gap-1 items-center"
115+
:title="`transform hooks cost: ${durations.transforms}ms`"
116+
>
117+
<span i-ph-magic-wand-duotone inline-block />
118+
</DisplayDuration>
119+
<span op40>|</span>
120+
<DisplayDuration
121+
:duration="durations.total" flex="~ gap-1 items-center"
122+
:title="`Total build cost: ${durations.total}ms`"
123+
>
124+
<span i-ph-clock-duotone inline-block />
125+
</DisplayDuration>
126+
<span op40>|</span>
127+
<div flex="~ gap-1 items-center">
128+
<DisplayFileSizeBadge title="Source code size" :bytes="sourceCodeSize" />
129+
<span i-carbon-arrow-right op50 />
130+
<DisplayFileSizeBadge title="Transformed code size" :bytes="transformedCodeSize" />
131+
</div>
132+
</div>
64133
<div flex="~ gap-2">
65134
<button
66135
:class="view === 'flow' ? 'text-primary' : ''"

packages/devtools/src/app/components/display/Duration.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,7 @@ const units = computed(() => {
5959
<DisplayNumberWithUnit
6060
:class="getDurationColor(ms)"
6161
:number="units[0]!" :unit="units[1]!"
62-
/>
62+
>
63+
<slot />
64+
</DisplayNumberWithUnit>
6365
</template>

packages/devtools/src/app/components/display/NumberWithUnit.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ defineProps<{
66
</script>
77

88
<template>
9-
<span block>
9+
<div>
10+
<slot />
1011
<span>{{ number }}</span><span ml-0.4 text-xs op75>{{ unit }}</span>
11-
</span>
12+
</div>
1213
</template>

packages/devtools/src/app/utils/format.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ export function bytesToHumanSize(bytes: number, digits = 2) {
44
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
55
const i = Math.floor(Math.log(bytes) / Math.log(1024))
66
if (i === 0)
7-
return ['<1', 'K']
7+
return [bytes, 'B']
88
return [(+(bytes / 1024 ** i).toFixed(digits)).toLocaleString(), sizes[i]]
99
}
1010

11+
export function getContentByteSize(content: string) {
12+
if (!content)
13+
return 0
14+
return new TextEncoder().encode(content).length
15+
}
16+
1117
export function toTree(modules: ModuleDest[], name: string) {
1218
const node: ModuleTreeNode = { name, children: {}, items: [] }
1319

0 commit comments

Comments
 (0)