Skip to content

Commit 8dca329

Browse files
authored
refactor: module details rpc function (#62)
1 parent 2c81f76 commit 8dca329

4 files changed

Lines changed: 50 additions & 109 deletions

File tree

packages/devtools/src/node/rolldown/events-manager.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type RolldownEvent = Event & {
66
event_id: string
77
}
88

9-
type ModuleBuildHookEvents = Exclude<Event, 'StringRef'> & (HookResolveIdCallStart | HookResolveIdCallEnd | HookLoadCallStart | HookLoadCallEnd | HookTransformCallStart | HookTransformCallEnd)
9+
type ModuleBuildHookEvents = (Exclude<Event, 'StringRef'> & (HookResolveIdCallStart | HookResolveIdCallEnd | HookLoadCallStart | HookLoadCallEnd | HookTransformCallStart | HookTransformCallEnd)) & { event_id: string }
1010

1111
const DURATION_THRESHOLD = 10
1212
const MODULE_BUILD_START_HOOKS = ['HookResolveIdCallStart', 'HookLoadCallStart', 'HookTransformCallStart']
@@ -43,21 +43,33 @@ export class RolldownEventsManager {
4343
const module_id = event.action === 'HookResolveIdCallEnd' ? event.resolved_id! : (event as HookLoadCallEnd | HookTransformCallEnd).module_id
4444
if (start) {
4545
const info = {
46-
start_time: +start.timestamp,
47-
end_time: +event.timestamp,
46+
id: event.event_id,
47+
timestamp_start: +start.timestamp,
48+
timestamp_end: +event.timestamp,
4849
duration: +event.timestamp - +start.timestamp,
4950
plugin_id: event.plugin_id,
5051
plugin_name: event.plugin_name,
5152
}
5253
const module_build_metrics = this.module_build_metrics.get(module_id) ?? { resolve_ids: [], loads: [], transforms: [] }
5354
if (event.action === 'HookResolveIdCallEnd') {
54-
module_build_metrics.resolve_ids.push(info)
55+
module_build_metrics.resolve_ids.push({
56+
...info,
57+
type: 'resolve',
58+
importer: (start as HookResolveIdCallStart).importer,
59+
module_request: (start as HookResolveIdCallStart).module_request,
60+
import_kind: (start as HookResolveIdCallStart).import_kind,
61+
resolved_id: event.resolved_id,
62+
})
5563
}
5664
else if (event.action === 'HookLoadCallEnd') {
5765
if (!event.content && info.duration < DURATION_THRESHOLD) {
5866
return
5967
}
60-
module_build_metrics.loads.push(info)
68+
module_build_metrics.loads.push({
69+
...info,
70+
type: 'load',
71+
content: event.content,
72+
})
6173
}
6274
else if (event.action === 'HookTransformCallEnd') {
6375
const _start = start as HookTransformCallStart
@@ -69,6 +81,9 @@ export class RolldownEventsManager {
6981

7082
module_build_metrics.transforms.push({
7183
...info,
84+
type: 'transform',
85+
content_from: _start.content,
86+
content_to: _end.content,
7287
source_code_size: getContentByteSize(_start.content!),
7388
transformed_code_size: getContentByteSize(_end.content!),
7489
})

packages/devtools/src/node/rpc/functions/rolldown-get-module-info.ts

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import type { ModuleInfo, RolldownResolveInfo } from '../../../shared/types'
1+
import type { ModuleInfo } from '../../../shared/types'
22
import { defineRpcFunction } from '../utils'
33

4-
const DURATION_THRESHOLD = 10
5-
64
export const rolldownGetModuleInfo = defineRpcFunction({
75
name: 'vite:rolldown:get-module-info',
86
type: 'query',
@@ -15,79 +13,24 @@ export const rolldownGetModuleInfo = defineRpcFunction({
1513
if (!events.length)
1614
return null
1715

16+
const moduleInfo = reader.manager.modules.get(module)
17+
1818
const info: Omit<ModuleInfo, 'transforms'> = {
1919
id: module,
20-
loads: [],
2120
imports: [],
2221
importers: [],
23-
resolve_ids: [],
2422
chunks: [],
2523
assets: [],
2624
build_metrics: {
2725
resolve_ids: [],
2826
loads: [],
2927
transforms: [],
3028
},
31-
...reader.manager.modules.get(module) || {},
29+
...moduleInfo || {},
30+
loads: moduleInfo?.build_metrics?.loads ?? [],
31+
resolve_ids: moduleInfo?.build_metrics?.resolve_ids ?? [],
3232
}
3333

34-
events.forEach((start, index) => {
35-
if (start.action !== 'HookLoadCallStart' || start.module_id !== module)
36-
return
37-
38-
const end = events.find(e => e.action === 'HookLoadCallEnd' && e.call_id === start.call_id, index)
39-
if (!end || end.action !== 'HookLoadCallEnd') {
40-
console.error(`[rolldown] Load call end not found for ${start.call_id}`)
41-
return
42-
}
43-
const duration = +end.timestamp - +start.timestamp
44-
if (!end.content && duration < DURATION_THRESHOLD)
45-
return
46-
47-
info.loads.push({
48-
type: 'load',
49-
id: start.event_id,
50-
plugin_name: start.plugin_name,
51-
plugin_id: start.plugin_id,
52-
content: end.content,
53-
timestamp_start: +start.timestamp,
54-
timestamp_end: +end.timestamp,
55-
duration,
56-
})
57-
})
58-
59-
events.forEach((end) => {
60-
if (end.action !== 'HookResolveIdCallEnd' || end.resolved_id !== module)
61-
return
62-
const start = events.find(e => e.action === 'HookResolveIdCallStart' && e.call_id === end.call_id)
63-
if (!start || start.action !== 'HookResolveIdCallStart') {
64-
console.error(`[rolldown] resolveId call start not found for ${end.event_id}`)
65-
return
66-
}
67-
68-
const duration = +end.timestamp - +start.timestamp
69-
const data: RolldownResolveInfo = {
70-
type: 'resolve',
71-
id: end.event_id,
72-
importer: start.importer,
73-
module_request: start.module_request,
74-
import_kind: start.import_kind,
75-
plugin_name: end.plugin_name,
76-
plugin_id: end.plugin_id,
77-
resolved_id: end.resolved_id,
78-
timestamp_start: +start.timestamp,
79-
timestamp_end: +end.timestamp,
80-
duration,
81-
}
82-
83-
// In Rolldown, resolveId might be called multiple times with different threads of Rust
84-
// If that happens, we only keep the last one
85-
const existingIndex = info.resolve_ids.findIndex(r => r.importer === start.importer && r.module_request === start.module_request && r.import_kind === start.import_kind && r.plugin_id === end.plugin_id)
86-
if (existingIndex >= 0)
87-
info.resolve_ids.splice(existingIndex, 1)
88-
info.resolve_ids.push(data)
89-
})
90-
9134
info.chunks = Array.from(reader.manager.chunks.values())
9235
.filter(chunk => chunk.modules.includes(module))
9336
.map(chunk => ({

packages/devtools/src/node/rpc/functions/rolldown-get-module-transforms.ts

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import type { RolldownModuleTransformInfo } from '../../../shared/types'
1+
import type { RolldownModuleTransformInfo } from '~~/shared/types'
22
import { diffLines } from 'diff'
33
import { defineRpcFunction } from '../utils'
44

5-
const DURATION_THRESHOLD = 10
6-
75
export const rolldownGetModuleTransforms = defineRpcFunction({
86
name: 'vite:rolldown:get-module-transforms',
97
type: 'query',
@@ -12,48 +10,33 @@ export const rolldownGetModuleTransforms = defineRpcFunction({
1210
handler: async ({ session, module }: { session: string, module: string }) => {
1311
const reader = await manager.loadSession(session)
1412
const events = reader.manager.events
15-
const transforms: RolldownModuleTransformInfo[] = []
16-
17-
if (!events.length)
18-
return transforms
19-
20-
events.forEach((start, index) => {
21-
if (start.action !== 'HookTransformCallStart' || start.module_id !== module)
22-
return
23-
24-
const end = events.find(e => e.action === 'HookTransformCallEnd' && e.call_id === start.call_id, index)
25-
if (!end || end.action !== 'HookTransformCallEnd') {
26-
console.error(`[rolldown] Transform call end not found for ${start.event_id}`)
27-
return
28-
}
29-
const duration = +end.timestamp - +start.timestamp
30-
if (end.content === start.content && duration < DURATION_THRESHOLD)
31-
return
32-
13+
const moduleInfo = reader.manager.modules.get(module)
14+
const transforms = moduleInfo?.build_metrics?.transforms ?? []
15+
16+
if (!events.length) {
17+
return transforms.map<RolldownModuleTransformInfo>(transform => ({
18+
...transform,
19+
diff_added: 0,
20+
diff_removed: 0,
21+
}))
22+
}
23+
24+
const normalizedTransforms: RolldownModuleTransformInfo[] = transforms.map((transform) => {
3325
let diff_added = 0
3426
let diff_removed = 0
35-
if (start.content !== end.content && start.content != null && end.content != null) {
36-
const delta = diffLines(start.content, end.content)
27+
if (transform.content_from !== transform.content_to && transform.content_from != null && transform.content_to != null) {
28+
const delta = diffLines(transform.content_from, transform.content_to)
3729
diff_added = delta.filter(d => d.added).map(d => d.value).join('').split(/\n/g).length
3830
diff_removed = delta.filter(d => d.removed).map(d => d.value).join('').split(/\n/g).length
3931
}
40-
41-
transforms.push({
42-
type: 'transform',
43-
id: start.event_id,
44-
plugin_name: start.plugin_name,
45-
plugin_id: start.plugin_id,
46-
content_from: start.content,
47-
content_to: end.content,
32+
return {
33+
...transform,
4834
diff_added,
4935
diff_removed,
50-
timestamp_start: +start.timestamp,
51-
timestamp_end: +end.timestamp,
52-
duration,
53-
})
36+
}
5437
})
5538

56-
return transforms.sort((a, b) => a.plugin_id - b.plugin_id)
39+
return normalizedTransforms.sort((a, b) => a.plugin_id - b.plugin_id)
5740
},
5841
}
5942
},

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ export interface BuildMetrics {
88
duration: number
99
source_code_size?: number
1010
transformed_code_size?: number
11-
start_time: number
12-
end_time: number
11+
timestamp_start: number
12+
timestamp_end: number
1313
}
1414

1515
export interface ModuleBuildMetrics {
16-
resolve_ids: BuildMetrics[]
17-
loads: BuildMetrics[]
18-
transforms: BuildMetrics[]
16+
resolve_ids: RolldownResolveInfo[]
17+
loads: RolldownModuleLoadInfo[]
18+
transforms: Array<Omit<RolldownModuleTransformInfo, 'diff_added' | 'diff_removed'> & { source_code_size: number, transformed_code_size: number }>
1919
}
2020
export type { PluginItem }
2121

0 commit comments

Comments
 (0)