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

perf(worker): inline worker without base64 #18752

Merged
merged 1 commit into from
Nov 25, 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
15 changes: 6 additions & 9 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,18 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
urlCode = 'self.location.href'
} else if (inlineRE.test(id)) {
const chunk = await bundleWorkerEntry(config, id)
const encodedJs = `const encodedJs = "${Buffer.from(
chunk.code,
).toString('base64')}";`
const jsContent = `const jsContent = ${JSON.stringify(chunk.code)};`

const code =
// Using blob URL for SharedWorker results in multiple instances of a same worker
workerConstructor === 'Worker'
? `${encodedJs}
const decodeBase64 = (base64) => Uint8Array.from(atob(base64), c => c.charCodeAt(0));
? `${jsContent}
const blob = typeof self !== "undefined" && self.Blob && new Blob([${
workerType === 'classic'
? ''
: // `URL` is always available, in `Worker[type="module"]`
`'URL.revokeObjectURL(import.meta.url);',`
}decodeBase64(encodedJs)], { type: "text/javascript;charset=utf-8" });
}jsContent], { type: "text/javascript;charset=utf-8" });
export default function WorkerWrapper(options) {
let objURL;
try {
Expand All @@ -349,7 +346,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
return worker;
} catch(e) {
return new ${workerConstructor}(
"data:text/javascript;base64," + encodedJs,
'data:text/javascript;charset=utf-8,' + encodeURIComponent(jsContent),
${workerTypeOption}
);
}${
Expand All @@ -362,10 +359,10 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
: ''
}
}`
: `${encodedJs}
: `${jsContent}
export default function WorkerWrapper(options) {
return new ${workerConstructor}(
"data:text/javascript;base64," + encodedJs,
'data:text/javascript;charset=utf-8,' + encodeURIComponent(jsContent),
${workerTypeOption}
);
}
Expand Down
2 changes: 1 addition & 1 deletion playground/worker/__tests__/es/worker-es.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe.runIf(isBuild)('build', () => {
)
// inlined shared worker
expect(content).toMatch(
`return new SharedWorker("data:text/javascript;base64,"+`,
`return new SharedWorker("data:text/javascript;charset=utf-8,"+`,
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe.runIf(isBuild)('build', () => {
expect(content).toMatch(
`new Worker("/iife-sourcemap-hidden/assets/my-worker`,
)
expect(content).toMatch(`new Worker("data:text/javascript;base64`)
expect(content).toMatch(`new Worker("data:text/javascript;charset=utf-8,"+`)
expect(content).toMatch(
`new Worker("/iife-sourcemap-hidden/assets/possible-ts-output-worker`,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe.runIf(isBuild)('build', () => {
expect(content).toMatch(
`new Worker("/iife-sourcemap-inline/assets/my-worker`,
)
expect(content).toMatch(`new Worker("data:text/javascript;base64`)
expect(content).toMatch(`new Worker("data:text/javascript;charset=utf-8,"+`)
expect(content).toMatch(
`new Worker("/iife-sourcemap-inline/assets/possible-ts-output-worker`,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe.runIf(isBuild)('build', () => {

// chunk
expect(content).toMatch(`new Worker("/iife-sourcemap/assets/my-worker`)
expect(content).toMatch(`new Worker("data:text/javascript;base64`)
expect(content).toMatch(`new Worker("data:text/javascript;charset=utf-8,"+`)
expect(content).toMatch(
`new Worker("/iife-sourcemap/assets/possible-ts-output-worker`,
)
Expand All @@ -117,8 +117,9 @@ describe.runIf(isBuild)('build', () => {
})

function getSourceMapUrl(code: string): string {
const regex = /\/\/[#@]\ssource(?:Mapping)?URL=\s*(\S+)/
const results = regex.exec(code)
const regex = /\/\/[#@]\ssource(?:Mapping)?URL=\s*(\S+)/g
const matches = [...code.matchAll(regex)]
const results = matches.at(-1)

if (results && results.length >= 2) {
return results[1]
Expand Down