|
2 | 2 | import type { TreeNodeInput } from 'nanovis'
|
3 | 3 | import type { PluginBuildInfo, RolldownPluginBuildMetrics, SessionContext } from '~~/shared/types'
|
4 | 4 | import { Flamegraph, normalizeTreeNode } from 'nanovis'
|
| 5 | +import { relative } from 'pathe' |
5 | 6 | import { computed, onMounted, onUnmounted, ref, shallowRef, useTemplateRef, watch } from 'vue'
|
6 |
| -import { parseReadablePath } from '~/utils/filepath' |
7 | 7 | import { normalizeTimestamp } from '~/utils/format'
|
8 |
| -import { getFileTypeFromModuleId, ModuleTypeRules } from '~/utils/icon' |
9 | 8 |
|
10 | 9 | const props = defineProps<{
|
11 | 10 | session: SessionContext
|
12 | 11 | buildMetrics: RolldownPluginBuildMetrics
|
13 | 12 | }>()
|
14 | 13 |
|
15 |
| -const parsedPaths = computed(() => props.session.modulesList.map((mod) => { |
16 |
| - const path = parseReadablePath(mod.id, props.session.meta.cwd) |
17 |
| - const type = getFileTypeFromModuleId(mod.id) |
18 |
| - return { |
19 |
| - mod, |
20 |
| - path, |
21 |
| - type, |
22 |
| - } |
23 |
| -})) |
24 |
| -
|
25 |
| -// filter current module list existed module type |
26 |
| -const existedModuleTypes = computed(() => ModuleTypeRules.filter(rule => parsedPaths.value.some(mod => rule.match.test(mod.mod.id)))) |
27 |
| -
|
28 | 14 | const n = (node: TreeNodeInput<PluginBuildInfo>) => normalizeTreeNode(node, undefined, false)
|
29 | 15 |
|
| 16 | +function normalizeModulePath(path: string) { |
| 17 | + const normalized = path.replace(/%2F/g, '/') |
| 18 | + const cwd = props.session!.meta.cwd |
| 19 | + let relate = cwd ? relative(cwd, normalized) : normalized |
| 20 | + if (!relate.startsWith('.')) |
| 21 | + relate = `./${relate}` |
| 22 | + if (relate.startsWith('./')) |
| 23 | + return relate |
| 24 | + if (relate.match(/^(?:\.\.\/){1,3}[^.]/)) |
| 25 | + return relate |
| 26 | + return normalized |
| 27 | +} |
| 28 | +
|
30 | 29 | const tree = computed(() => {
|
31 |
| - // build children: module group by module type |
32 |
| - const resolveIds = existedModuleTypes.value.map((type, idx) => n({ |
33 |
| - id: `resolveId-${type.name}-${idx}`, |
34 |
| - text: type.name, |
35 |
| - children: props.buildMetrics.resolveIdMetrics.filter((item) => { |
36 |
| - return getFileTypeFromModuleId(item.module).name === type.name |
37 |
| - }).map((id, idx) => n({ |
38 |
| - id: `resolveId-${idx}`, |
39 |
| - text: id.module, |
40 |
| - size: id.duration, |
41 |
| - meta: id, |
42 |
| - })), |
| 30 | + const resolveIds = props.buildMetrics.resolveIdMetrics.map((id, idx) => n({ |
| 31 | + id: `resolveId-${idx}`, |
| 32 | + text: normalizeModulePath(id.module), |
| 33 | + size: id.duration, |
| 34 | + meta: id, |
43 | 35 | }))
|
44 | 36 |
|
45 |
| - const loads = existedModuleTypes.value.map((type, idx) => n({ |
46 |
| - id: `loads-${type.name}-${idx}`, |
47 |
| - text: type.name, |
48 |
| - children: props.buildMetrics.loadMetrics.filter((item) => { |
49 |
| - return getFileTypeFromModuleId(item.module).name === type.name |
50 |
| - }).map((id, idx) => n({ |
51 |
| - id: `resolveId-${idx}`, |
52 |
| - text: id.module, |
53 |
| - size: id.duration, |
54 |
| - meta: id, |
55 |
| - })), |
| 37 | + const loads = props.buildMetrics.loadMetrics.map((id, idx) => n({ |
| 38 | + id: `loads-${idx}`, |
| 39 | + text: normalizeModulePath(id.module), |
| 40 | + size: id.duration, |
| 41 | + meta: id, |
56 | 42 | }))
|
57 | 43 |
|
58 |
| - const transforms = existedModuleTypes.value.map((type, idx) => n({ |
59 |
| - id: `transforms-${type.name}-${idx}`, |
60 |
| - text: type.name, |
61 |
| - children: props.buildMetrics.transformMetrics.filter((item) => { |
62 |
| - return getFileTypeFromModuleId(item.module).name === type.name |
63 |
| - }).map((id, idx) => n({ |
64 |
| - id: `resolveId-${idx}`, |
65 |
| - text: id.module, |
66 |
| - size: id.duration, |
67 |
| - meta: id, |
68 |
| - })), |
| 44 | + const transforms = props.buildMetrics.transformMetrics.map((id, idx) => n({ |
| 45 | + id: `transforms-${idx}`, |
| 46 | + text: normalizeModulePath(id.module), |
| 47 | + size: id.duration, |
| 48 | + meta: id, |
69 | 49 | }))
|
70 | 50 |
|
71 |
| - // resolve/load/transform -> module type -> module |
| 51 | + // resolve/load/transform -> module |
72 | 52 | const children = [
|
73 | 53 | n({
|
74 | 54 | id: '~resolves',
|
|
0 commit comments