Skip to content

Commit 7139ef3

Browse files
fix(worker): keep chunks conflicted with unused workers that are still used by a live worker (#23956)
Co-authored-by: sapphi-red <[email protected]>
1 parent c755150 commit 7139ef3

6 files changed

Lines changed: 36 additions & 7 deletions

File tree

‎packages/vite/src/node/plugins/worker.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎playground/worker/__tests__/es/worker-es.spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ describe.runIf(isBuild)('build', () => {
164164
false,
165165
)
166166

167+
// The importer of this nested worker is tree-shaken from the main bundle,
168+
// but a live worker still uses it, so the nested worker must be emitted.
169+
const sharedWorker = files.find((f) => f.includes('dce-test-shared-worker'))
170+
expect(sharedWorker).toBeDefined()
171+
const nestedWorker = fs
172+
.readFileSync(path.resolve(assetsDir, sharedWorker!), 'utf-8')
173+
.match(/[\w-]*dce-test-shared-nested-worker[\w-]*\.js/)?.[0]
174+
expect(nestedWorker).toBeDefined()
175+
expect(files).toContain(nestedWorker)
176+
167177
// sanity: the worker we DO use is still emitted (`my-worker`).
168178
expect(files.some((f) => f.includes('my-worker'))).toBe(true)
169179
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import DceTestSharedNestedWorker from './dce-test-shared-nested-worker.js?worker'
2+
3+
export { DceTestSharedNestedWorker }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
self.postMessage('dce-test-shared-nested-worker is live')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { DceTestSharedNestedWorker } from './dce-test-shared-importer.js'
2+
3+
new DceTestSharedNestedWorker()
4+
self.postMessage('dce-test-shared-worker is live')

‎playground/worker/worker/main-module.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import * as depSelfReferenceUrlWorker from '@vitejs/test-dep-self-reference-url-
44
// then skip emitting the dce-test-worker asset (and its nested worker too).
55
import { dceTestWorker as _dceTestWorker } from '../dce-test-importer.js'
66
import DceTestLiveWorker from '../dce-test-live-worker.js?worker'
7+
// imported but never used, so it's tree-shaken here, but the worker below uses
8+
// it to start its nested worker, which must still be emitted.
9+
import { DceTestSharedNestedWorker as _DceTestSharedNestedWorker } from '../dce-test-shared-importer.js'
10+
import DceTestSharedWorker from '../dce-test-shared-worker.js?worker'
711
import { mode } from '../modules/workerImport'
812
import InlineSharedWorker from '../my-inline-shared-worker?sharedworker&inline'
913
import mySharedWorker from '../my-shared-worker?sharedworker&name=shared'
@@ -20,6 +24,7 @@ function text(el, text) {
2024
document.querySelector('.mode-true').textContent = mode
2125

2226
new DceTestLiveWorker()
27+
new DceTestSharedWorker()
2328

2429
const worker = new myWorker()
2530
worker.postMessage('ping')

0 commit comments

Comments
 (0)