@@ -8,6 +8,10 @@ export type RolldownEvent = Event & {
88
99type ModuleBuildHookEvents = Exclude < Event , 'StringRef' > & ( HookResolveIdCallStart | HookResolveIdCallEnd | HookLoadCallStart | HookLoadCallEnd | HookTransformCallStart | HookTransformCallEnd )
1010
11+ const DURATION_THRESHOLD = 10
12+ const MODULE_BUILD_START_HOOKS = [ 'HookResolveIdCallStart' , 'HookLoadCallStart' , 'HookTransformCallStart' ]
13+ const MODULE_BUILD_END_HOOKS = [ 'HookResolveIdCallEnd' , 'HookLoadCallEnd' , 'HookTransformCallEnd' ]
14+
1115export class RolldownEventsManager {
1216 events : RolldownEvent [ ] = [ ]
1317 chunks : Map < number , ChunkInfo > = new Map ( )
@@ -29,11 +33,12 @@ export class RolldownEventsManager {
2933 }
3034
3135 recordModuleBuildMetrics ( event : ModuleBuildHookEvents ) {
32- if ( [ 'HookResolveIdCallStart' , 'HookLoadCallStart' , 'HookTransformCallStart' ] . includes ( event . action ) ) {
36+ if ( MODULE_BUILD_START_HOOKS . includes ( event . action ) ) {
3337 this . module_build_hook_events . set ( `${ event . action } _${ event . call_id } ` , event )
3438 }
35- else if ( [ 'HookResolveIdCallEnd' , 'HookLoadCallEnd' , 'HookTransformCallEnd' ] . includes ( event . action ) ) {
36- const start = this . module_build_hook_events . get ( `${ event . action . replace ( 'End' , 'Start' ) } _${ event . call_id } ` )
39+ else if ( MODULE_BUILD_END_HOOKS . includes ( event . action ) ) {
40+ const start_event_name = `${ event . action . replace ( 'End' , 'Start' ) } _${ event . call_id } `
41+ const start = this . module_build_hook_events . get ( start_event_name )
3742 const module_id = event . action === 'HookResolveIdCallEnd' ? event . resolved_id ! : ( event as HookLoadCallEnd | HookTransformCallEnd ) . module_id
3843 if ( start ) {
3944 const info = {
@@ -48,7 +53,7 @@ export class RolldownEventsManager {
4853 module_build_metrics . resolve_ids . push ( info )
4954 }
5055 else if ( event . action === 'HookLoadCallEnd' ) {
51- if ( ! event . content && info . duration < 10 ) {
56+ if ( ! event . content && info . duration < DURATION_THRESHOLD ) {
5257 return
5358 }
5459 module_build_metrics . loads . push ( info )
@@ -58,7 +63,7 @@ export class RolldownEventsManager {
5863 const _end = event as HookTransformCallEnd
5964 const no_changes = _start . content === _end . content
6065 // TODO: remove module id check when rolldown fixes the unique call id
61- if ( ( no_changes && info . duration < 10 ) || _start . module_id !== _end . module_id ) {
66+ if ( ( no_changes && info . duration < DURATION_THRESHOLD ) || _start . module_id !== _end . module_id ) {
6267 return
6368 }
6469
@@ -113,6 +118,7 @@ export class RolldownEventsManager {
113118 }
114119
115120 if ( event . action === 'ModuleGraphReady' ) {
121+ this . module_build_hook_events . clear ( )
116122 for ( const module of event . modules ) {
117123 this . modules . set ( module . id , {
118124 ...module ,
0 commit comments