From dc823347bb857a9f63eee7e027a52236d7e331e0 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Wed, 16 Oct 2024 11:17:35 +0900 Subject: [PATCH] fix(worker): rewrite rollup `output.format` with `worker.format` on worker build error (#18165) --- .../vite/src/node/__tests__/build.spec.ts | 20 +++++++++++++++++++ .../fixtures/worker-dynamic/dynamic.js | 1 + .../__tests__/fixtures/worker-dynamic/main.js | 12 +++++++++++ .../fixtures/worker-dynamic/worker.js | 4 ++++ packages/vite/src/node/plugins/worker.ts | 13 +++++++++++- 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 packages/vite/src/node/__tests__/fixtures/worker-dynamic/dynamic.js create mode 100644 packages/vite/src/node/__tests__/fixtures/worker-dynamic/main.js create mode 100644 packages/vite/src/node/__tests__/fixtures/worker-dynamic/worker.js diff --git a/packages/vite/src/node/__tests__/build.spec.ts b/packages/vite/src/node/__tests__/build.spec.ts index c5e12fb7d5b2f5..29d63c2ed03f3e 100644 --- a/packages/vite/src/node/__tests__/build.spec.ts +++ b/packages/vite/src/node/__tests__/build.spec.ts @@ -707,6 +707,26 @@ test('default sharedConfigBuild true on build api', async () => { expect(counter).toBe(1) }) +test('adjust worker build error for worker.format', async () => { + try { + await build({ + root: resolve(__dirname, 'fixtures/worker-dynamic'), + build: { + rollupOptions: { + input: { + index: '/main.js', + }, + }, + }, + }) + } catch (e) { + expect(e.message).toContain('worker.format') + expect(e.message).not.toContain('output.format') + return + } + expect.unreachable() +}) + /** * for each chunks in output1, if there's a chunk in output2 with the same fileName, * ensure that the chunk code is the same. if not, the chunk hash should have changed. diff --git a/packages/vite/src/node/__tests__/fixtures/worker-dynamic/dynamic.js b/packages/vite/src/node/__tests__/fixtures/worker-dynamic/dynamic.js new file mode 100644 index 00000000000000..78a294a403c1e1 --- /dev/null +++ b/packages/vite/src/node/__tests__/fixtures/worker-dynamic/dynamic.js @@ -0,0 +1 @@ +export default 'dynamic ok' diff --git a/packages/vite/src/node/__tests__/fixtures/worker-dynamic/main.js b/packages/vite/src/node/__tests__/fixtures/worker-dynamic/main.js new file mode 100644 index 00000000000000..b3141b8e59f0c3 --- /dev/null +++ b/packages/vite/src/node/__tests__/fixtures/worker-dynamic/main.js @@ -0,0 +1,12 @@ +document.querySelector('#app').innerHTML = ` +
+

Test worker

+
???
+
+` + +const worker = new Worker(new URL('./worker.js', import.meta.url)) +worker.onmessage = (e) => { + document.querySelector('#worker').textContent = e.data +} +worker.postMessage('hi') diff --git a/packages/vite/src/node/__tests__/fixtures/worker-dynamic/worker.js b/packages/vite/src/node/__tests__/fixtures/worker-dynamic/worker.js new file mode 100644 index 00000000000000..7a03fb88a18e53 --- /dev/null +++ b/packages/vite/src/node/__tests__/fixtures/worker-dynamic/worker.js @@ -0,0 +1,4 @@ +self.onmessage = async () => { + const mod = await import('./dynamic') + self.postMessage('hello from worker: ' + mod.default) +} diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index dd6cb9b8a3e0a4..a480752e3f7451 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -1,6 +1,6 @@ import path from 'node:path' import MagicString from 'magic-string' -import type { OutputChunk } from 'rollup' +import type { OutputChunk, RollupError } from 'rollup' import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' import { ENV_ENTRY, ENV_PUBLIC_PATH } from '../constants' @@ -126,6 +126,17 @@ async function bundleWorkerEntry( }) } }) + } catch (e) { + // adjust rollup format error + if ( + e instanceof Error && + e.name === 'RollupError' && + (e as RollupError).code === 'INVALID_OPTION' && + e.message.includes('"output.format"') + ) { + e.message = e.message.replace('output.format', 'worker.format') + } + throw e } finally { await bundle.close() }