Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(worker): nested inlined worker always fallbacked to data URI worker instead of using blob worker #17509

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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([${
hughfenghen marked this conversation as resolved.
Show resolved Hide resolved
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