Skip to content

Commit

Permalink
fix(worker): nested inlined worker always fallbacked to data URI work…
Browse files Browse the repository at this point in the history
…er instead of using blob worker (#17509)
  • Loading branch information
hughfenghen authored Jul 13, 2024
1 parent 167006e commit 07bc489
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
workerConstructor === 'Worker'
? `${encodedJs}
const decodeBase64 = (base64) => Uint8Array.from(atob(base64), c => c.charCodeAt(0));
const blob = typeof window !== "undefined" && window.Blob && new Blob([${
const blob = typeof self !== "undefined" && self.Blob && new Blob([${
workerType === 'classic'
? ''
: // `URL` is always available, in `Worker[type="module"]`
Expand All @@ -314,11 +314,11 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
export default function WorkerWrapper(options) {
let objURL;
try {
objURL = blob && (window.URL || window.webkitURL).createObjectURL(blob);
objURL = blob && (self.URL || self.webkitURL).createObjectURL(blob);
if (!objURL) throw ''
const worker = new ${workerConstructor}(objURL, ${workerTypeOption});
worker.addEventListener("error", () => {
(window.URL || window.webkitURL).revokeObjectURL(objURL);
(self.URL || self.webkitURL).revokeObjectURL(objURL);
});
return worker;
} catch(e) {
Expand All @@ -331,7 +331,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
// otherwise the worker fails to run
workerType === 'classic'
? ` finally {
objURL && (window.URL || window.webkitURL).revokeObjectURL(objURL);
objURL && (self.URL || self.webkitURL).revokeObjectURL(objURL);
}`
: ''
}
Expand Down
6 changes: 3 additions & 3 deletions playground/worker/__tests__/es/worker-es.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ describe.runIf(isBuild)('build', () => {
expect(content).toMatch(`new Worker("/es/assets`)
expect(content).toMatch(`new SharedWorker("/es/assets`)
// inlined worker
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
expect(content).toMatch(`window.Blob`)
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
expect(content).toMatch(`self.Blob`)
expect(content).toMatch(
/try\{if\(\w+=\w+&&\(window\.URL\|\|window\.webkitURL\)\.createObjectURL\(\w+\),!\w+\)throw""/,
/try\{if\(\w+=\w+&&\(self\.URL\|\|self\.webkitURL\)\.createObjectURL\(\w+\),!\w+\)throw""/,
)
// inlined shared worker
expect(content).toMatch(
Expand Down
4 changes: 2 additions & 2 deletions playground/worker/__tests__/iife/worker-iife.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ describe.runIf(isBuild)('build', () => {
expect(content).toMatch(`new Worker("/iife/assets`)
expect(content).toMatch(`new SharedWorker("/iife/assets`)
// inlined
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
expect(content).toMatch(`window.Blob`)
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
expect(content).toMatch(`self.Blob`)
})

test('worker emitted and import.meta.url in nested worker (build)', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ describe.runIf(isBuild)('build', () => {
expect(content).toMatch(`new Worker(""+new URL("../worker-entries/`)
expect(content).toMatch(`new SharedWorker(""+new URL("../worker-entries/`)
// inlined
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
expect(content).toMatch(`window.Blob`)
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
expect(content).toMatch(`self.Blob`)
})

test('worker emitted and import.meta.url in nested worker (build)', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ describe.runIf(isBuild)('build', () => {
)

// inlined
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
expect(content).toMatch(`window.Blob`)
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
expect(content).toMatch(`self.Blob`)

expect(workerNestedWorkerContent).toMatch(
`new Worker("/iife-sourcemap-hidden/assets/sub-worker`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ describe.runIf(isBuild)('build', () => {
)

// inlined
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
expect(content).toMatch(`window.Blob`)
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
expect(content).toMatch(`self.Blob`)

expect(workerNestedWorkerContent).toMatch(
`new Worker("/iife-sourcemap-inline/assets/sub-worker`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ describe.runIf(isBuild)('build', () => {
)

// inlined
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`)
expect(content).toMatch(`window.Blob`)
expect(content).toMatch(`(self.URL||self.webkitURL).createObjectURL`)
expect(content).toMatch(`self.Blob`)

expect(workerNestedWorkerContent).toMatch(
`new Worker("/iife-sourcemap/assets/sub-worker`,
Expand Down

0 comments on commit 07bc489

Please sign in to comment.