Skip to content

Commit 69454d7

Browse files
authored
fix(worker): make worker output consistent with client and SSR (#21871)
Signed-off-by: will Farrell <[email protected]>
1 parent 12932f5 commit 69454d7

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import workerUrl from './worker?worker&url'
2+
console.log(workerUrl)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* global self */
2+
const msg = 'hello from worker'
3+
self.postMessage(msg)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { resolve } from 'node:path'
2+
import { expect, test } from 'vitest'
3+
import type { OutputChunk, RolldownOutput } from 'rolldown'
4+
import { build } from '../../build'
5+
6+
const fixturesDir = resolve(import.meta.dirname, 'fixtures')
7+
8+
test('?worker&url should produce the same hash in client and SSR builds', async () => {
9+
const root = resolve(fixturesDir, 'worker-url')
10+
11+
const clientResult = (await build({
12+
root,
13+
logLevel: 'silent',
14+
build: {
15+
write: false,
16+
rolldownOptions: {
17+
input: resolve(root, 'entry.js'),
18+
},
19+
},
20+
})) as RolldownOutput
21+
22+
const ssrResult = (await build({
23+
root,
24+
logLevel: 'silent',
25+
build: {
26+
write: false,
27+
ssr: resolve(root, 'entry.js'),
28+
},
29+
})) as RolldownOutput
30+
31+
// Extract the worker URL from both builds.
32+
// The entry chunk will contain the worker asset URL as a string.
33+
const clientEntry = clientResult.output.find(
34+
(o): o is OutputChunk => o.type === 'chunk' && o.isEntry,
35+
)!
36+
const ssrEntry = ssrResult.output.find(
37+
(o): o is OutputChunk => o.type === 'chunk' && o.isEntry,
38+
)!
39+
40+
const workerUrlPattern = /assets\/worker-[\w-]+\.js/g
41+
const clientWorkerUrls = clientEntry.code.match(workerUrlPattern) ?? []
42+
const ssrWorkerUrls = ssrEntry.code.match(workerUrlPattern) ?? []
43+
44+
expect(clientWorkerUrls.length).toBeGreaterThan(0)
45+
expect(ssrWorkerUrls.length).toBeGreaterThan(0)
46+
expect(ssrWorkerUrls).toEqual(clientWorkerUrls)
47+
})

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,14 @@ async function bundleWorkerEntry(
232232
'[name]-[hash].[ext]',
233233
),
234234
minify:
235-
config.build.minify === 'oxc'
235+
workerEnvironment.config.build.minify === 'oxc'
236236
? true
237-
: config.build.minify === false
237+
: workerEnvironment.config.build.minify === false
238238
? 'dce-only'
239239
: undefined,
240240
...workerConfig,
241241
format,
242-
sourcemap: config.build.sourcemap,
242+
sourcemap: workerEnvironment.config.build.sourcemap,
243243
})
244244
watchedFiles = (await bundle.watchFiles).map((f) => normalizePath(f))
245245
} catch (e) {

0 commit comments

Comments
 (0)