@@ -2,6 +2,37 @@ import { join } from 'pathe'
22import { RolldownEventsReader } from '../../rolldown/events-reader'
33import { defineRpcFunction } from '../utils'
44
5+ export interface ModuleInfo {
6+ id : string
7+ loads : RolldownModuleLoadInfo [ ]
8+ transforms : RolldownModuleTransformInfo [ ]
9+
10+ // TODO: Awaits https://github.com/rolldown/rolldown/issues/4135
11+ deps : string [ ]
12+ importers : string [ ]
13+ }
14+
15+ export interface RolldownModuleLoadInfo {
16+ id : string
17+ plugin_name : string
18+ source : string | null
19+ timestamp_start : number
20+ timestamp_end : number
21+ duration : number
22+ }
23+
24+ export interface RolldownModuleTransformInfo {
25+ id : string
26+ plugin_name : string
27+ source_from : string | null
28+ source_to : string | null
29+ timestamp_start : number
30+ timestamp_end : number
31+ duration : number
32+ }
33+
34+ const DURATION_THRESHOLD = 10
35+
536export const rolldownGetModuleInfo = defineRpcFunction ( {
637 name : 'vite:rolldown:get-module-info' ,
738 type : 'query' ,
@@ -11,9 +42,67 @@ export const rolldownGetModuleInfo = defineRpcFunction({
1142 const reader = RolldownEventsReader . get ( join ( cwd , '.rolldown' , build , 'log.json' ) )
1243 await reader . read ( )
1344 const events = reader . manager . events . filter ( event => event . module_id === module )
14- return {
15- events,
45+
46+ if ( ! events . length )
47+ return null
48+
49+ const info : ModuleInfo = {
50+ id : module ,
51+ loads : [ ] ,
52+ transforms : [ ] ,
53+ deps : [ ] ,
54+ importers : [ ] ,
1655 }
56+
57+ for ( const event of events ) {
58+ if ( event . kind === 'HookLoadCallEnd' ) {
59+ // TODO: use ID to pair start and end
60+ const start = events . find ( e => e . kind === 'HookLoadCallStart' && e . module_id === event . module_id && e . plugin_name === event . plugin_name )
61+ if ( ! start ) {
62+ console . error ( `[rolldown] Load call start not found for ${ event . event_id } ` )
63+ continue
64+ }
65+ const duration = + event . timestamp - + start . timestamp
66+ if ( ! event . source && duration < DURATION_THRESHOLD )
67+ continue
68+ info . loads . push ( {
69+ id : event . event_id ,
70+ plugin_name : event . plugin_name ,
71+ source : event . source ,
72+ timestamp_start : + start . timestamp ,
73+ timestamp_end : + event . timestamp ,
74+ duration,
75+ } )
76+ }
77+ }
78+
79+ for ( const event of events ) {
80+ if ( event . kind === 'HookTransformCallEnd' ) {
81+ // TODO: use ID to pair start and end
82+ const start = events . find ( e => e . kind === 'HookTransformCallStart' && e . module_id === event . module_id && e . plugin_name === event . plugin_name )
83+ if ( ! start || start . kind !== 'HookTransformCallStart' ) {
84+ console . error ( `[rolldown] Transform call start not found for ${ event . event_id } ` )
85+ continue
86+ }
87+ const duration = + event . timestamp - + start . timestamp
88+ if ( start . source === event . transformed_source && duration < DURATION_THRESHOLD )
89+ continue
90+ info . transforms . push ( {
91+ id : event . event_id ,
92+ plugin_name : event . plugin_name ,
93+ source_from : start . source ,
94+ source_to : event . transformed_source ,
95+ timestamp_start : + start . timestamp ,
96+ timestamp_end : + event . timestamp ,
97+ duration,
98+ } )
99+ }
100+ }
101+
102+ info . loads . sort ( ( a , b ) => a . timestamp_start - b . timestamp_start )
103+ info . transforms . sort ( ( a , b ) => a . timestamp_start - b . timestamp_start )
104+
105+ return info
17106 } ,
18107 }
19108 } ,
0 commit comments