@@ -55,6 +55,11 @@ interface PreparedWrite {
5555 sourceIndex ?: number
5656}
5757
58+ interface ModuleBatchState {
59+ source ?: { hasSource : boolean , transformRequest ?: object }
60+ latest ?: { value : string , index : number }
61+ }
62+
5863export type {
5964 ViteInspectPluginMetricItem ,
6065 ViteInspectResolveIdItem ,
@@ -459,31 +464,38 @@ class PayloadViteInspectStore implements ViteInspectStore {
459464 const values : Array < string | null | undefined > = [ ]
460465 const prepared : PreparedWrite [ ] = [ ]
461466 const pluginCallWrites : InspectPluginCallWrite [ ] = [ ]
462- const sourceState = new Map < string , { hasSource : boolean , transformRequest ?: object } > ( )
463- // Latest code queued for each module in this batch, so a step that returns its input unchanged reuses that payload.
464- const latestCode = new Map < string , { value : string , index : number } > ( )
465- const pushCode = ( key : string , value : string | null | undefined ) : number => {
466- const latest = latestCode . get ( key )
467- if ( value != null && latest ?. value === value )
468- return latest . index
467+ // Keyed by scope, then module id, to avoid building a composite key per write.
468+ const moduleStates = new Map < string , Map < string , ModuleBatchState > > ( )
469+ const getModuleState = ( scope : string , moduleId : string ) : ModuleBatchState => {
470+ let scoped = moduleStates . get ( scope )
471+ if ( ! scoped ) {
472+ scoped = new Map ( )
473+ moduleStates . set ( scope , scoped )
474+ }
475+ let state = scoped . get ( moduleId )
476+ if ( ! state ) {
477+ state = { }
478+ scoped . set ( moduleId , state )
479+ }
480+ return state
481+ }
482+ const pushCode = ( state : ModuleBatchState , value : string | null | undefined ) : number => {
483+ if ( value != null && state . latest ?. value === value )
484+ return state . latest . index
469485 const index = values . push ( value ) - 1
470486 if ( value != null )
471- latestCode . set ( key , { value, index } )
487+ state . latest = { value, index }
472488 return index
473489 }
474-
475- const hasSource = ( scope : string , moduleId : string , transformRequest ?: object ) : boolean => {
476- const key = scopeKey ( scope , moduleId )
477- let state = sourceState . get ( key )
478- if ( ! state ) {
490+ const hasSource = ( state : ModuleBatchState , scope : string , moduleId : string , transformRequest ?: object ) : boolean => {
491+ if ( ! state . source ) {
479492 const stored = this . scopes . get ( scope ) ?. transforms . get ( moduleId )
480- state = {
493+ state . source = {
481494 hasSource : stored ?. items . some ( item => item . hasResult ) ?? false ,
482495 transformRequest : stored ?. transformRequest ,
483496 }
484- sourceState . set ( key , state )
485497 }
486- return state . hasSource && ( ! transformRequest || state . transformRequest === transformRequest )
498+ return state . source . hasSource && ( ! transformRequest || state . source . transformRequest === transformRequest )
487499 }
488500
489501 for ( const write of writes ) {
@@ -499,19 +511,19 @@ class PayloadViteInspectStore implements ViteInspectStore {
499511
500512 const item : PreparedWrite = { write }
501513 if ( write . operation === 'recordTransform' ) {
502- const key = scopeKey ( write . scope , write . moduleId )
503- if ( ! hasSource ( write . scope , write . moduleId , write . transformRequest ) ) {
504- latestCode . delete ( key )
505- item . sourceIndex = pushCode ( key , write . preTransformCode )
506- sourceState . set ( key , { hasSource : true , transformRequest : write . transformRequest } )
514+ const state = getModuleState ( write . scope , write . moduleId )
515+ if ( ! hasSource ( state , write . scope , write . moduleId , write . transformRequest ) ) {
516+ state . latest = undefined
517+ item . sourceIndex = pushCode ( state , write . preTransformCode )
518+ state . source = { hasSource : true , transformRequest : write . transformRequest }
507519 }
508- item . resultIndex = pushCode ( key , write . info . result )
520+ item . resultIndex = pushCode ( state , write . info . result )
509521 }
510522 else if ( write . operation === 'recordLoad' ) {
511- const key = scopeKey ( write . scope , write . moduleId )
512- latestCode . delete ( key )
513- item . resultIndex = pushCode ( key , write . info . result )
514- sourceState . set ( scopeKey ( write . scope , write . moduleId ) , { hasSource : write . info . result != null , transformRequest : write . transformRequest } )
523+ const state = getModuleState ( write . scope , write . moduleId )
524+ state . latest = undefined
525+ item . resultIndex = pushCode ( state , write . info . result )
526+ state . source = { hasSource : write . info . result != null , transformRequest : write . transformRequest }
515527 }
516528 prepared . push ( item )
517529 }
@@ -940,10 +952,6 @@ function rangeAt(
940952 return index == null ? undefined : ranges [ index ]
941953}
942954
943- function scopeKey ( scope : string , moduleId : string ) : string {
944- return `${ scope } \0${ moduleId } `
945- }
946-
947955function positiveInteger ( value : number | undefined , fallback : number ) : number {
948956 return Number . isFinite ( value ) && value ! > 0
949957 ? Math . max ( 1 , Math . floor ( value ! ) )
0 commit comments