|
| 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 | +}) |
0 commit comments