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: inline webworker safari support #3468

Merged
merged 5 commits into from
May 23, 2021
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
2 changes: 1 addition & 1 deletion packages/playground/worker/__tests__/worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ if (isBuild) {
// chunk
expect(content).toMatch(`new Worker("/assets`)
// inlined
expect(content).toMatch(`new Worker("data:application/javascript`)
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
})
}
15 changes: 11 additions & 4 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
let url: string
if (isBuild) {
if (query.inline != null) {
// bundle the file as entry to support imports and inline as base64
// bundle the file as entry to support imports and inline as blob
// data url
const rollup = require('rollup') as typeof Rollup
const bundle = await rollup.rollup({
Expand All @@ -55,9 +55,16 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
format: 'es',
sourcemap: config.build.sourcemap
})
url = `data:application/javascript;base64,${Buffer.from(
output[0].code
).toString('base64')}`

return `const blob = new Blob([atob(\"${Buffer.from(output[0].code).toString('base64')}\")], { type: 'text/javascript;charset=utf-8' });
export default function WorkerWrapper() {
const objURL = (window.URL || window.webkitURL).createObjectURL(blob);
try {
return new Worker(objURL);
} finally {
(window.URL || window.webkitURL).revokeObjectURL(objURL);
}
}`
} finally {
await bundle.close()
}
Expand Down