Skip to content

Commit

Permalink
fix: inline webworker safari support (#3468)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xerios authored May 23, 2021
1 parent b1598ce commit 2671546
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
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

0 comments on commit 2671546

Please sign in to comment.