Skip to content

Commit 90c66c9

Browse files
authored
perf(worker): inline worker without base64 (#18752)
1 parent ce50bd4 commit 90c66c9

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-15
lines changed

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,18 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
322322
urlCode = 'self.location.href'
323323
} else if (inlineRE.test(id)) {
324324
const chunk = await bundleWorkerEntry(config, id)
325-
const encodedJs = `const encodedJs = "${Buffer.from(
326-
chunk.code,
327-
).toString('base64')}";`
325+
const jsContent = `const jsContent = ${JSON.stringify(chunk.code)};`
328326

329327
const code =
330328
// Using blob URL for SharedWorker results in multiple instances of a same worker
331329
workerConstructor === 'Worker'
332-
? `${encodedJs}
333-
const decodeBase64 = (base64) => Uint8Array.from(atob(base64), c => c.charCodeAt(0));
330+
? `${jsContent}
334331
const blob = typeof self !== "undefined" && self.Blob && new Blob([${
335332
workerType === 'classic'
336333
? ''
337334
: // `URL` is always available, in `Worker[type="module"]`
338335
`'URL.revokeObjectURL(import.meta.url);',`
339-
}decodeBase64(encodedJs)], { type: "text/javascript;charset=utf-8" });
336+
}jsContent], { type: "text/javascript;charset=utf-8" });
340337
export default function WorkerWrapper(options) {
341338
let objURL;
342339
try {
@@ -349,7 +346,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
349346
return worker;
350347
} catch(e) {
351348
return new ${workerConstructor}(
352-
"data:text/javascript;base64," + encodedJs,
349+
'data:text/javascript;charset=utf-8,' + encodeURIComponent(jsContent),
353350
${workerTypeOption}
354351
);
355352
}${
@@ -362,10 +359,10 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
362359
: ''
363360
}
364361
}`
365-
: `${encodedJs}
362+
: `${jsContent}
366363
export default function WorkerWrapper(options) {
367364
return new ${workerConstructor}(
368-
"data:text/javascript;base64," + encodedJs,
365+
'data:text/javascript;charset=utf-8,' + encodeURIComponent(jsContent),
369366
${workerTypeOption}
370367
);
371368
}

playground/worker/__tests__/es/worker-es.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ describe.runIf(isBuild)('build', () => {
116116
)
117117
// inlined shared worker
118118
expect(content).toMatch(
119-
`return new SharedWorker("data:text/javascript;base64,"+`,
119+
`return new SharedWorker("data:text/javascript;charset=utf-8,"+`,
120120
)
121121
})
122122

playground/worker/__tests__/sourcemap-hidden/worker-sourcemap-hidden.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe.runIf(isBuild)('build', () => {
9494
expect(content).toMatch(
9595
`new Worker("/iife-sourcemap-hidden/assets/my-worker`,
9696
)
97-
expect(content).toMatch(`new Worker("data:text/javascript;base64`)
97+
expect(content).toMatch(`new Worker("data:text/javascript;charset=utf-8,"+`)
9898
expect(content).toMatch(
9999
`new Worker("/iife-sourcemap-hidden/assets/possible-ts-output-worker`,
100100
)

playground/worker/__tests__/sourcemap-inline/worker-sourcemap-inline.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe.runIf(isBuild)('build', () => {
7575
expect(content).toMatch(
7676
`new Worker("/iife-sourcemap-inline/assets/my-worker`,
7777
)
78-
expect(content).toMatch(`new Worker("data:text/javascript;base64`)
78+
expect(content).toMatch(`new Worker("data:text/javascript;charset=utf-8,"+`)
7979
expect(content).toMatch(
8080
`new Worker("/iife-sourcemap-inline/assets/possible-ts-output-worker`,
8181
)

playground/worker/__tests__/sourcemap/worker-sourcemap.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe.runIf(isBuild)('build', () => {
9595

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

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

123124
if (results && results.length >= 2) {
124125
return results[1]

0 commit comments

Comments
 (0)