Skip to content

Commit 2671546

Browse files
authored
fix: inline webworker safari support (#3468)
1 parent b1598ce commit 2671546

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

packages/playground/worker/__tests__/worker.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ if (isBuild) {
2828
// chunk
2929
expect(content).toMatch(`new Worker("/assets`)
3030
// inlined
31-
expect(content).toMatch(`new Worker("data:application/javascript`)
31+
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
3232
})
3333
}

packages/vite/src/node/plugins/worker.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
4343
let url: string
4444
if (isBuild) {
4545
if (query.inline != null) {
46-
// bundle the file as entry to support imports and inline as base64
46+
// bundle the file as entry to support imports and inline as blob
4747
// data url
4848
const rollup = require('rollup') as typeof Rollup
4949
const bundle = await rollup.rollup({
@@ -55,9 +55,16 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
5555
format: 'es',
5656
sourcemap: config.build.sourcemap
5757
})
58-
url = `data:application/javascript;base64,${Buffer.from(
59-
output[0].code
60-
).toString('base64')}`
58+
59+
return `const blob = new Blob([atob(\"${Buffer.from(output[0].code).toString('base64')}\")], { type: 'text/javascript;charset=utf-8' });
60+
export default function WorkerWrapper() {
61+
const objURL = (window.URL || window.webkitURL).createObjectURL(blob);
62+
try {
63+
return new Worker(objURL);
64+
} finally {
65+
(window.URL || window.webkitURL).revokeObjectURL(objURL);
66+
}
67+
}`
6168
} finally {
6269
await bundle.close()
6370
}

0 commit comments

Comments
 (0)