From 5152ff18206c1933ebedc945cceec6cb22198ea4 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Mon, 18 May 2026 14:25:55 +0900 Subject: [PATCH 1/5] feat(worker): remove worker chunk if it's detected that it's not referenced --- packages/vite/src/node/plugins/worker.ts | 119 +++++++++++++++++- .../src/node/plugins/workerImportMetaUrl.ts | 12 +- .../worker/__tests__/es/worker-es.spec.ts | 16 +++ playground/worker/dce-test-importer.js | 3 + playground/worker/dce-test-nested-worker.js | 1 + playground/worker/dce-test-worker.js | 4 + playground/worker/worker/main-module.js | 4 + 7 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 playground/worker/dce-test-importer.js create mode 100644 playground/worker/dce-test-nested-worker.js create mode 100644 playground/worker/dce-test-worker.js diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 25eedfc7b1aac5..18448acfaa131a 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -56,6 +56,22 @@ class WorkerOutputCache { /* entryFilename */ string >() private invalidatedBundles = new Set() + /** + * Module IDs in the main bundle that reference each worker bundle. + * Cleared at the start of each non-worker build and re-populated by load/transform hooks. + */ + private bundleReferencingIds = new Map< + /* childInputId */ string, + Set + >() + /** + * For each worker bundle, the set of other worker bundles it references + * (nested workers). Persists across rebuilds; cleared in removeBundle. + */ + private bundleNestedReferences = new Map< + /* parentInputId */ string, + Set + >() saveWorkerBundle( file: string, @@ -126,6 +142,71 @@ class WorkerOutputCache { this.assets.delete(asset) } } + + this.bundleReferencingIds.delete(file) + this.bundleNestedReferences.delete(file) + } + + recordReference( + parentInputId: string | undefined, + childInputId: string, + referencingModuleId: string, + ) { + if (parentInputId !== undefined) { + let set = this.bundleNestedReferences.get(parentInputId) + if (!set) { + set = new Set() + this.bundleNestedReferences.set(parentInputId, set) + } + set.add(childInputId) + } else { + let set = this.bundleReferencingIds.get(childInputId) + if (!set) { + set = new Set() + this.bundleReferencingIds.set(childInputId, set) + } + set.add(referencingModuleId) + } + } + + clearDirectReferences() { + this.bundleReferencingIds.clear() + } + + getLiveAssetFileNames(liveModuleIds: Set): Set { + const liveBundles = new Set() + const queue: string[] = [] + for (const [inputId, refIds] of this.bundleReferencingIds) { + for (const refId of refIds) { + if (liveModuleIds.has(refId)) { + liveBundles.add(inputId) + queue.push(inputId) + break + } + } + } + while (queue.length > 0) { + const parent = queue.shift()! + const nested = this.bundleNestedReferences.get(parent) + if (!nested) continue + for (const child of nested) { + if (!liveBundles.has(child)) { + liveBundles.add(child) + queue.push(child) + } + } + } + + const liveFileNames = new Set() + for (const inputId of liveBundles) { + const wb = this.bundles.get(inputId) + if (!wb) continue + liveFileNames.add(wb.entryFilename) + for (const fileName of wb.referencedAssets) { + liveFileNames.add(fileName) + } + } + return liveFileNames } getWorkerBundle(file: string) { @@ -159,6 +240,17 @@ const inlineRE = /[?&]inline\b/ export const WORKER_FILE_ID = 'worker_file' const workerOutputCaches = new WeakMap() +export function recordWorkerReference( + config: ResolvedConfig, + parentInputId: string | undefined, + childInputId: string, + referencingModuleId: string, +): void { + workerOutputCaches + .get(config.mainConfig || config)! + .recordReference(parentInputId, childInputId, referencingModuleId) +} + async function bundleWorkerEntry( config: ResolvedConfig, id: string, @@ -402,6 +494,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { buildStart() { if (isWorker) return emittedAssets.clear() + workerOutputCaches.get(config)!.clearDirectReferences() }, load: { @@ -429,6 +522,12 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { if (isWorker && config.bundleChain.at(-1) === cleanUrl(id)) { urlCode = 'self.location.href' } else if (inlineRE.test(id)) { + recordWorkerReference( + config, + config.bundleChain.at(-1), + cleanUrl(id), + id, + ) const result = await bundleWorkerEntry(config, id) for (const file of result.watchedFiles) { this.addWatchFile(file) @@ -479,6 +578,12 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { map: { mappings: '' }, } } else { + recordWorkerReference( + config, + config.bundleChain.at(-1), + cleanUrl(id), + id, + ) const result = await workerFileToUrl(config, id) let url: string if ( @@ -637,7 +742,19 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { ) { return } - for (const asset of workerOutputCaches.get(config)!.getAssets()) { + const cache = workerOutputCaches.get(config)! + const liveModuleIds = new Set() + for (const fileName in bundle) { + const chunk = bundle[fileName] + if (chunk.type === 'chunk') { + for (const moduleId of chunk.moduleIds) { + liveModuleIds.add(moduleId) + } + } + } + const liveFileNames = cache.getLiveAssetFileNames(liveModuleIds) + for (const asset of cache.getAssets()) { + if (!liveFileNames.has(asset.fileName)) continue if (emittedAssets.has(asset.fileName)) continue emittedAssets.add(asset.fileName) diff --git a/packages/vite/src/node/plugins/workerImportMetaUrl.ts b/packages/vite/src/node/plugins/workerImportMetaUrl.ts index 1e58cb7f8ddcef..ac832596e689a5 100644 --- a/packages/vite/src/node/plugins/workerImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/workerImportMetaUrl.ts @@ -11,7 +11,11 @@ import { createBackCompatIdResolver } from '../idResolver' import type { ResolveIdFn } from '../idResolver' import { cleanUrl, slash } from '../../shared/utils' import type { WorkerType } from './worker' -import { WORKER_FILE_ID, workerFileToUrl } from './worker' +import { + WORKER_FILE_ID, + recordWorkerReference, + workerFileToUrl, +} from './worker' import { fileToUrl, toOutputFilePathInJSForBundledDev } from './asset' import type { InternalResolveOptions } from './resolve' import { tryFsResolve } from './resolve' @@ -255,6 +259,12 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin { } else { let builtUrl: string if (isBundled) { + recordWorkerReference( + config, + config.bundleChain.at(-1), + cleanUrl(file), + id, + ) const result = await workerFileToUrl(config, file) if (this.environment.config.command === 'serve') { builtUrl = toOutputFilePathInJSForBundledDev( diff --git a/playground/worker/__tests__/es/worker-es.spec.ts b/playground/worker/__tests__/es/worker-es.spec.ts index 781ead7a765435..2468cfe5380634 100644 --- a/playground/worker/__tests__/es/worker-es.spec.ts +++ b/playground/worker/__tests__/es/worker-es.spec.ts @@ -141,6 +141,22 @@ describe.runIf(isBuild)('build', () => { .poll(() => page.textContent('.nested-worker-constructor')) .toMatch('"type":"constructor"') }) + + test('dead-code-eliminated worker asset is not emitted', () => { + const assetsDir = path.resolve(testDir, 'dist/es/assets') + const files = fs.readdirSync(assetsDir) + + // dce-test-importer.js is imported from main-module.js but its export is + // unused; rolldown tree-shakes the importer, so its `?worker` import + // should never reach the output bundle. + expect(files.some((f) => f.includes('dce-test-worker'))).toBe(false) + // the nested worker is only reachable via the (tree-shaken) parent worker, + // so it must not be emitted either. + expect(files.some((f) => f.includes('dce-test-nested-worker'))).toBe(false) + + // sanity: the worker we DO use is still emitted (`my-worker`). + expect(files.some((f) => f.includes('my-worker'))).toBe(true) + }) }) test('module worker', async () => { diff --git a/playground/worker/dce-test-importer.js b/playground/worker/dce-test-importer.js new file mode 100644 index 00000000000000..b5af3b02afe242 --- /dev/null +++ b/playground/worker/dce-test-importer.js @@ -0,0 +1,3 @@ +import DceTestWorker from './dce-test-worker.js?worker' + +export const dceTestWorker = DceTestWorker diff --git a/playground/worker/dce-test-nested-worker.js b/playground/worker/dce-test-nested-worker.js new file mode 100644 index 00000000000000..7b6fa58decf2ce --- /dev/null +++ b/playground/worker/dce-test-nested-worker.js @@ -0,0 +1 @@ +self.postMessage('dce-test-nested-worker should be tree-shaken') diff --git a/playground/worker/dce-test-worker.js b/playground/worker/dce-test-worker.js new file mode 100644 index 00000000000000..7a492e0f437c72 --- /dev/null +++ b/playground/worker/dce-test-worker.js @@ -0,0 +1,4 @@ +import DceTestNestedWorker from './dce-test-nested-worker.js?worker' + +const _nested = new DceTestNestedWorker() +self.postMessage('dce-test-worker should be tree-shaken') diff --git a/playground/worker/worker/main-module.js b/playground/worker/worker/main-module.js index 87259a46279712..cc38cb4bf3bc9e 100644 --- a/playground/worker/worker/main-module.js +++ b/playground/worker/worker/main-module.js @@ -1,4 +1,8 @@ import * as depSelfReferenceUrlWorker from '@vitejs/test-dep-self-reference-url-worker' +// imported but never used — the dead-importer module itself is side-effect-free, +// so rolldown DCEs it along with its `?worker` import. The worker plugin should +// then skip emitting the dce-test-worker asset (and its nested worker too). +import { dceTestWorker as _dceTestWorker } from '../dce-test-importer.js' import myWorker from '../my-worker.ts?worker' import InlineWorker from '../my-worker.ts?worker&inline' import InlineSharedWorker from '../my-inline-shared-worker?sharedworker&inline' From 366385164955c365a0cffc51fa635918e7e50444 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Fri, 24 Jul 2026 11:59:33 +0900 Subject: [PATCH 2/5] fix: watch mode --- packages/vite/src/node/plugins/worker.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index a78039ea75b6a0..32f7b5ba7de2e3 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -752,9 +752,14 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { } } } - const liveFileNames = cache.getLiveAssetFileNames(liveModuleIds) + // Reference tracking relies on hooks running for every module, which is + // not guaranteed when an incremental build reuses cached modules. + const liveFileNames = + isBuild && !config.build.watch + ? cache.getLiveAssetFileNames(liveModuleIds) + : undefined for (const asset of cache.getAssets()) { - if (!liveFileNames.has(asset.fileName)) continue + if (liveFileNames && !liveFileNames.has(asset.fileName)) continue if (emittedAssets.has(asset.fileName)) continue emittedAssets.add(asset.fileName) From a28483564d83933ab486c1bb89235c7c551f83e1 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Fri, 24 Jul 2026 15:05:13 +0900 Subject: [PATCH 3/5] fix: nested case --- packages/vite/src/node/plugins/worker.ts | 102 +++++++++--------- .../worker/__tests__/es/worker-es.spec.ts | 7 ++ .../worker/dce-test-live-nested-worker.js | 1 + .../worker/dce-test-live-worker-importer.js | 3 + playground/worker/dce-test-live-worker.js | 3 + playground/worker/worker/main-module.js | 3 + 6 files changed, 66 insertions(+), 53 deletions(-) create mode 100644 playground/worker/dce-test-live-nested-worker.js create mode 100644 playground/worker/dce-test-live-worker-importer.js create mode 100644 playground/worker/dce-test-live-worker.js diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 32f7b5ba7de2e3..0f0cabd0a99fbd 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -32,6 +32,7 @@ type WorkerBundle = { entryCode: string entryUrlPlaceholder: string referencedAssets: Set + moduleIds: Set watchedFiles: string[] } @@ -43,6 +44,9 @@ type WorkerBundleAsset = { source: string | Uint8Array } +/** `undefined` identifies the main bundle. */ +type BundleId = string | undefined + class WorkerOutputCache { /** * worker bundle information for each input id @@ -56,21 +60,9 @@ class WorkerOutputCache { /* entryFilename */ string >() private invalidatedBundles = new Set() - /** - * Module IDs in the main bundle that reference each worker bundle. - * Cleared at the start of each non-worker build and re-populated by load/transform hooks. - */ - private bundleReferencingIds = new Map< - /* childInputId */ string, - Set - >() - /** - * For each worker bundle, the set of other worker bundles it references - * (nested workers). Persists across rebuilds; cleared in removeBundle. - */ - private bundleNestedReferences = new Map< - /* parentInputId */ string, - Set + private bundleReferences = new Map< + BundleId, + Map> >() saveWorkerBundle( @@ -79,6 +71,7 @@ class WorkerOutputCache { outputEntryFilename: string, outputEntryCode: string, outputAssets: WorkerBundleAsset[], + moduleIds: Set, logger: Logger, ): WorkerBundle { for (const asset of outputAssets) { @@ -90,6 +83,7 @@ class WorkerOutputCache { entryUrlPlaceholder: this.generateEntryUrlPlaceholder(outputEntryFilename), referencedAssets: new Set(outputAssets.map((asset) => asset.fileName)), + moduleIds, watchedFiles, } this.bundles.set(file, bundle) @@ -143,56 +137,48 @@ class WorkerOutputCache { } } - this.bundleReferencingIds.delete(file) - this.bundleNestedReferences.delete(file) + this.bundleReferences.delete(file) } recordReference( - parentInputId: string | undefined, + parentInputId: BundleId, childInputId: string, referencingModuleId: string, ) { - if (parentInputId !== undefined) { - let set = this.bundleNestedReferences.get(parentInputId) - if (!set) { - set = new Set() - this.bundleNestedReferences.set(parentInputId, set) - } - set.add(childInputId) - } else { - let set = this.bundleReferencingIds.get(childInputId) - if (!set) { - set = new Set() - this.bundleReferencingIds.set(childInputId, set) - } - set.add(referencingModuleId) + let referencesByModule = this.bundleReferences.get(parentInputId) + if (!referencesByModule) { + referencesByModule = new Map() + this.bundleReferences.set(parentInputId, referencesByModule) + } + let childInputIds = referencesByModule.get(referencingModuleId) + if (!childInputIds) { + childInputIds = new Set() + referencesByModule.set(referencingModuleId, childInputIds) } + childInputIds.add(childInputId) } - clearDirectReferences() { - this.bundleReferencingIds.clear() + clearMainBundleReferences() { + this.bundleReferences.delete(undefined) } - getLiveAssetFileNames(liveModuleIds: Set): Set { + getLiveAssetFileNames(mainLiveModuleIds: Set): Set { const liveBundles = new Set() - const queue: string[] = [] - for (const [inputId, refIds] of this.bundleReferencingIds) { - for (const refId of refIds) { - if (liveModuleIds.has(refId)) { - liveBundles.add(inputId) - queue.push(inputId) - break - } - } - } + const queue: [BundleId, Set][] = [[undefined, mainLiveModuleIds]] while (queue.length > 0) { - const parent = queue.shift()! - const nested = this.bundleNestedReferences.get(parent) - if (!nested) continue - for (const child of nested) { - if (!liveBundles.has(child)) { - liveBundles.add(child) - queue.push(child) + const [bundleId, moduleIds] = queue.shift()! + const referencesByModule = this.bundleReferences.get(bundleId) + if (!referencesByModule) continue + for (const moduleId of moduleIds) { + const childInputIds = referencesByModule.get(moduleId) + if (!childInputIds) continue + for (const childInputId of childInputIds) { + if (liveBundles.has(childInputId)) continue + liveBundles.add(childInputId) + const childBundle = this.bundles.get(childInputId) + if (childBundle) { + queue.push([childInputId, childBundle.moduleIds]) + } } } } @@ -359,6 +345,15 @@ async function bundleWorkerEntry( await bundle.close() } + const moduleIds = new Set() + for (const output of result.output) { + if (output.type === 'chunk') { + for (const moduleId of output.moduleIds) { + moduleIds.add(moduleId) + } + } + } + const { output: [outputChunk, ...outputChunks], } = result @@ -392,6 +387,7 @@ async function bundleWorkerEntry( outputChunk.fileName, outputChunk.code, assets, + moduleIds, config.logger, ) return newBundleInfo @@ -494,7 +490,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { buildStart() { if (isWorker) return emittedAssets.clear() - workerOutputCaches.get(config)!.clearDirectReferences() + workerOutputCaches.get(config)!.clearMainBundleReferences() }, load: { diff --git a/playground/worker/__tests__/es/worker-es.spec.ts b/playground/worker/__tests__/es/worker-es.spec.ts index 2468cfe5380634..dc2d46352bad59 100644 --- a/playground/worker/__tests__/es/worker-es.spec.ts +++ b/playground/worker/__tests__/es/worker-es.spec.ts @@ -154,6 +154,13 @@ describe.runIf(isBuild)('build', () => { // so it must not be emitted either. expect(files.some((f) => f.includes('dce-test-nested-worker'))).toBe(false) + // This parent worker is live, but the module that references its nested + // worker is tree-shaken from the parent worker bundle. + expect(files.some((f) => f.includes('dce-test-live-worker'))).toBe(true) + expect(files.some((f) => f.includes('dce-test-live-nested-worker'))).toBe( + false, + ) + // sanity: the worker we DO use is still emitted (`my-worker`). expect(files.some((f) => f.includes('my-worker'))).toBe(true) }) diff --git a/playground/worker/dce-test-live-nested-worker.js b/playground/worker/dce-test-live-nested-worker.js new file mode 100644 index 00000000000000..d11e764e05375a --- /dev/null +++ b/playground/worker/dce-test-live-nested-worker.js @@ -0,0 +1 @@ +self.postMessage('dce-test-live-nested-worker should be tree-shaken') diff --git a/playground/worker/dce-test-live-worker-importer.js b/playground/worker/dce-test-live-worker-importer.js new file mode 100644 index 00000000000000..2e86ec0b718fe9 --- /dev/null +++ b/playground/worker/dce-test-live-worker-importer.js @@ -0,0 +1,3 @@ +import DceTestLiveNestedWorker from './dce-test-live-nested-worker.js?worker' + +export { DceTestLiveNestedWorker } diff --git a/playground/worker/dce-test-live-worker.js b/playground/worker/dce-test-live-worker.js new file mode 100644 index 00000000000000..ce3963f437bb4c --- /dev/null +++ b/playground/worker/dce-test-live-worker.js @@ -0,0 +1,3 @@ +import { DceTestLiveNestedWorker as _DceTestLiveNestedWorker } from './dce-test-live-worker-importer.js' + +self.postMessage('dce-test-live-worker is live') diff --git a/playground/worker/worker/main-module.js b/playground/worker/worker/main-module.js index cc38cb4bf3bc9e..8dd7965a793c4a 100644 --- a/playground/worker/worker/main-module.js +++ b/playground/worker/worker/main-module.js @@ -1,4 +1,5 @@ import * as depSelfReferenceUrlWorker from '@vitejs/test-dep-self-reference-url-worker' +import DceTestLiveWorker from '../dce-test-live-worker.js?worker' // imported but never used — the dead-importer module itself is side-effect-free, // so rolldown DCEs it along with its `?worker` import. The worker plugin should // then skip emitting the dce-test-worker asset (and its nested worker too). @@ -18,6 +19,8 @@ function text(el, text) { document.querySelector('.mode-true').textContent = mode +new DceTestLiveWorker() + const worker = new myWorker() worker.postMessage('ping') worker.addEventListener('message', (e) => { From 84c8539f2a9351a570f7b964180193daf1382567 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Fri, 24 Jul 2026 15:35:18 +0900 Subject: [PATCH 4/5] refactor: tweak --- packages/vite/src/node/plugins/worker.ts | 88 ++++++++++++++---------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 0f0cabd0a99fbd..de710004e2326d 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -1,6 +1,11 @@ import path from 'node:path' import MagicString from 'magic-string' -import type { RolldownOutput, RollupError } from 'rolldown' +import type { + OutputAsset, + OutputChunk, + RolldownOutput, + RollupError, +} from 'rolldown' import colors from 'picocolors' import { type ImportSpecifier, init, parse } from 'es-module-lexer' import { viteWebWorkerPostPlugin as nativeWebWorkerPostPlugin } from 'rolldown/experimental' @@ -44,8 +49,11 @@ type WorkerBundleAsset = { source: string | Uint8Array } +/** The input ID of a worker entry, which identifies its bundle. */ +type WorkerBundleId = string + /** `undefined` identifies the main bundle. */ -type BundleId = string | undefined +type BundleId = WorkerBundleId | undefined class WorkerOutputCache { /** @@ -60,9 +68,20 @@ class WorkerOutputCache { /* entryFilename */ string >() private invalidatedBundles = new Set() + /** + * Worker references grouped by their containing bundle and module. + * `referencingModuleId` is the module whose inclusion keeps the reference + * live: the worker wrapper module for a `?worker` import, or the importing + * module for a `new URL(..., import.meta.url)` worker reference. + * `childBundleId` identifies the referenced worker bundle and is used as its + * `BundleId` when traversing that bundle's references. + */ private bundleReferences = new Map< BundleId, - Map> + Map< + /* referencingModuleId */ string, + Set + > >() saveWorkerBundle( @@ -142,7 +161,7 @@ class WorkerOutputCache { recordReference( parentInputId: BundleId, - childInputId: string, + childBundleId: WorkerBundleId, referencingModuleId: string, ) { let referencesByModule = this.bundleReferences.get(parentInputId) @@ -150,12 +169,12 @@ class WorkerOutputCache { referencesByModule = new Map() this.bundleReferences.set(parentInputId, referencesByModule) } - let childInputIds = referencesByModule.get(referencingModuleId) - if (!childInputIds) { - childInputIds = new Set() - referencesByModule.set(referencingModuleId, childInputIds) + let childBundleIds = referencesByModule.get(referencingModuleId) + if (!childBundleIds) { + childBundleIds = new Set() + referencesByModule.set(referencingModuleId, childBundleIds) } - childInputIds.add(childInputId) + childBundleIds.add(childBundleId) } clearMainBundleReferences() { @@ -170,14 +189,14 @@ class WorkerOutputCache { const referencesByModule = this.bundleReferences.get(bundleId) if (!referencesByModule) continue for (const moduleId of moduleIds) { - const childInputIds = referencesByModule.get(moduleId) - if (!childInputIds) continue - for (const childInputId of childInputIds) { - if (liveBundles.has(childInputId)) continue - liveBundles.add(childInputId) - const childBundle = this.bundles.get(childInputId) + const childBundleIds = referencesByModule.get(moduleId) + if (!childBundleIds) continue + for (const childBundleId of childBundleIds) { + if (liveBundles.has(childBundleId)) continue + liveBundles.add(childBundleId) + const childBundle = this.bundles.get(childBundleId) if (childBundle) { - queue.push([childInputId, childBundle.moduleIds]) + queue.push([childBundleId, childBundle.moduleIds]) } } } @@ -229,12 +248,12 @@ const workerOutputCaches = new WeakMap() export function recordWorkerReference( config: ResolvedConfig, parentInputId: string | undefined, - childInputId: string, + childBundleId: WorkerBundleId, referencingModuleId: string, ): void { workerOutputCaches .get(config.mainConfig || config)! - .recordReference(parentInputId, childInputId, referencingModuleId) + .recordReference(parentInputId, childBundleId, referencingModuleId) } async function bundleWorkerEntry( @@ -345,14 +364,7 @@ async function bundleWorkerEntry( await bundle.close() } - const moduleIds = new Set() - for (const output of result.output) { - if (output.type === 'chunk') { - for (const moduleId of output.moduleIds) { - moduleIds.add(moduleId) - } - } - } + const moduleIds = collectIncludedModuleIds(result.output) const { output: [outputChunk, ...outputChunks], @@ -739,15 +751,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { return } const cache = workerOutputCaches.get(config)! - const liveModuleIds = new Set() - for (const fileName in bundle) { - const chunk = bundle[fileName] - if (chunk.type === 'chunk') { - for (const moduleId of chunk.moduleIds) { - liveModuleIds.add(moduleId) - } - } - } + const liveModuleIds = collectIncludedModuleIds(Object.values(bundle)) // Reference tracking relies on hooks running for every module, which is // not guaranteed when an incremental build reuses cached modules. const liveFileNames = @@ -790,6 +794,20 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { } } +function collectIncludedModuleIds( + outputs: (OutputChunk | OutputAsset)[], +): Set { + const moduleIds = new Set() + for (const output of outputs) { + if (output.type === 'chunk') { + for (const moduleId of output.moduleIds) { + moduleIds.add(moduleId) + } + } + } + return moduleIds +} + function isSameContent(a: string | Uint8Array, b: string | Uint8Array) { if (typeof a === 'string') { if (typeof b === 'string') { From 701ff1f49685e65f55895e1755660955198a903a Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Fri, 24 Jul 2026 15:51:06 +0900 Subject: [PATCH 5/5] refactor: cleanup --- packages/vite/src/node/plugins/worker.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index de710004e2326d..25e81f650c726c 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -177,10 +177,6 @@ class WorkerOutputCache { childBundleIds.add(childBundleId) } - clearMainBundleReferences() { - this.bundleReferences.delete(undefined) - } - getLiveAssetFileNames(mainLiveModuleIds: Set): Set { const liveBundles = new Set() const queue: [BundleId, Set][] = [[undefined, mainLiveModuleIds]] @@ -502,7 +498,6 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { buildStart() { if (isWorker) return emittedAssets.clear() - workerOutputCaches.get(config)!.clearMainBundleReferences() }, load: {