Skip to content

Commit a1fd676

Browse files
committed
refactor: clean code
1 parent 0991090 commit a1fd676

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,10 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
296296
chunk.code,
297297
).toString('base64')}";`
298298

299-
const blobCode = `${encodedJs}
299+
const code =
300+
// Using blob URL for SharedWorker results in multiple instances of a same worker
301+
workerConstructor === 'Worker'
302+
? `${encodedJs}
300303
const blob = typeof window !== "undefined" && window.Blob && new Blob([atob(encodedJs)], { type: "text/javascript;charset=utf-8" });
301304
export default function WorkerWrapper() {
302305
let objURL;
@@ -310,16 +313,14 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
310313
objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL);
311314
}
312315
}`
313-
314-
const base64Code = `${encodedJs}
316+
: `${encodedJs}
315317
export default function WorkerWrapper() {
316318
return new ${workerConstructor}("data:application/javascript;base64," + encodedJs${workerOptions});
317319
}
318320
`
319321

320322
return {
321-
// Using blob URL for SharedWorker results in multiple instances of a same worker
322-
code: workerConstructor === 'Worker' ? blobCode : base64Code,
323+
code,
323324
// Empty sourcemap to suppress Rollup warning
324325
map: { mappings: '' },
325326
}

0 commit comments

Comments
 (0)