Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(worker): rewrite rollup output.format with worker.format on worker build error #18165

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'dynamic ok'
12 changes: 12 additions & 0 deletions packages/vite/src/node/__tests__/fixtures/worker-dynamic/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
document.querySelector('#app').innerHTML = `
<div>
<h1>Test worker</h1>
<div id="worker">???</div>
</div>
`

const worker = new Worker(new URL('./worker.js', import.meta.url))
worker.onmessage = (e) => {
document.querySelector('#worker').textContent = e.data
}
worker.postMessage('hi')
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
self.onmessage = async () => {
const mod = await import('./dynamic')
self.postMessage('hello from worker: ' + mod.default)
}
13 changes: 12 additions & 1 deletion packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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()
}
Expand Down