Skip to content

Commit

Permalink
fix: check for Blob before creating worker URL (close vitejs#4462)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasg123 committed Aug 21, 2021
1 parent 632a50a commit 73daf51
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/vite/src/node/plugins/worker.ts
Expand Up @@ -71,15 +71,14 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
const content = Buffer.from(code)
if (query.inline != null) {
// inline as blob data url
return `const blob = new Blob([atob(\"${content.toString(
'base64'
)}\")], { type: 'text/javascript;charset=utf-8' });
return `const encodedJs = "${content.toString('base64')}";
const blob = typeof window !== "undefined" && window.Blob && new Blob([atob(encodedJs)], { type: "text/javascript;charset=utf-8" });
export default function WorkerWrapper() {
const objURL = (window.URL || window.webkitURL).createObjectURL(blob);
const objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob);
try {
return new Worker(objURL);
return objURL ? new Worker(objURL) : new Worker("data:application/javascript;base64," + encodedJs, {type: "module"});
} finally {
(window.URL || window.webkitURL).revokeObjectURL(objURL);
objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL);
}
}`
} else {
Expand Down

0 comments on commit 73daf51

Please sign in to comment.