@@ -176,9 +176,12 @@ class WorkerOutputCache {
176176 childBundleIds . add ( childBundleId )
177177 }
178178
179- getLiveAssetFileNames ( mainLiveModuleIds : Set < string > ) : Set < string > {
179+ getLiveAssetFileNames (
180+ rootBundleId : BundleId ,
181+ rootLiveModuleIds : Set < string > ,
182+ ) : Set < string > {
180183 const liveBundles = new Set < string > ( )
181- const queue : [ BundleId , Set < string > ] [ ] = [ [ undefined , mainLiveModuleIds ] ]
184+ const queue : [ BundleId , Set < string > ] [ ] = [ [ rootBundleId , rootLiveModuleIds ] ]
182185 while ( queue . length > 0 ) {
183186 const [ bundleId , moduleIds ] = queue . shift ( ) !
184187 const referencesByModule = this . bundleReferences . get ( bundleId )
@@ -792,12 +795,18 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
792795 // Rolldown later tree-shakes the module that referenced them. This also
793796 // needs to run for worker sub-builds so dead nested workers don't become
794797 // referenced assets of their parent worker bundle.
795- if ( shouldFilter ) {
796- const rootBundleId = isWorker ? config . bundleChain . at ( - 1 ) : undefined
798+ const rootBundleId = isWorker ? config . bundleChain . at ( - 1 ) : undefined
799+ const liveFileNames = shouldFilter
800+ ? cache . getLiveAssetFileNames ( rootBundleId , liveModuleIds )
801+ : undefined
802+ if ( liveFileNames ) {
797803 for ( const workerBundle of cache . getDeadDirectlyReferencedBundles (
798804 rootBundleId ,
799805 liveModuleIds ,
800806 ) ) {
807+ // Still used through another live worker (e.g. its importer is
808+ // tree-shaken here but kept in a worker).
809+ if ( liveFileNames . has ( workerBundle . entryFilename ) ) continue
801810 const emittedAsset = bundle [ workerBundle . entryFilename ]
802811 if ( emittedAsset ?. type === 'asset' ) {
803812 delete bundle [ workerBundle . entryFilename ]
@@ -807,9 +816,6 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
807816
808817 if ( isWorker ) return
809818
810- const liveFileNames = shouldFilter
811- ? cache . getLiveAssetFileNames ( liveModuleIds )
812- : undefined
813819 for ( const asset of cache . getAssets ( ) ) {
814820 if ( liveFileNames && ! liveFileNames . has ( asset . fileName ) ) continue
815821 if ( emittedAssets . has ( asset . fileName ) ) continue
0 commit comments