From 570e0f185203ceec02b89ff53c7a13add1309e77 Mon Sep 17 00:00:00 2001
From: Jay Wang
Date: Mon, 12 Feb 2024 02:34:51 -0500
Subject: [PATCH] fix(worker): support UTF-8 encoding in inline workers (fixes
#12117) (#15866)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Jay Wang
Co-authored-by: 翠 / green
---
packages/vite/src/node/plugins/worker.ts | 5 +++--
playground/worker/__tests__/es/worker-es.spec.ts | 8 ++++++++
playground/worker/index.html | 6 ++++++
playground/worker/my-worker.ts | 10 ++++++++++
playground/worker/worker/main-module.js | 6 ++++++
5 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts
index 62849bb06e7875..522cb7eb16221e 100644
--- a/packages/vite/src/node/plugins/worker.ts
+++ b/packages/vite/src/node/plugins/worker.ts
@@ -289,12 +289,13 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin {
// 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));
const blob = typeof window !== "undefined" && window.Blob && new Blob([${
workerType === 'classic'
? ''
: // `URL` is always available, in `Worker[type="module"]`
- `'URL.revokeObjectURL(import.meta.url);'+`
- }atob(encodedJs)], { type: "text/javascript;charset=utf-8" });
+ `'URL.revokeObjectURL(import.meta.url);',`
+ }decodeBase64(encodedJs)], { type: "text/javascript;charset=utf-8" });
export default function WorkerWrapper(options) {
let objURL;
try {
diff --git a/playground/worker/__tests__/es/worker-es.spec.ts b/playground/worker/__tests__/es/worker-es.spec.ts
index 0031c432c36b7a..48462ad8bc8077 100644
--- a/playground/worker/__tests__/es/worker-es.spec.ts
+++ b/playground/worker/__tests__/es/worker-es.spec.ts
@@ -50,6 +50,14 @@ test('import meta url', async () => {
)
})
+test('unicode inlined', async () => {
+ await untilUpdated(
+ () => page.textContent('.pong-inline-unicode'),
+ '•pong•',
+ true,
+ )
+})
+
test('shared worker', async () => {
await untilUpdated(() => page.textContent('.tick-count'), 'pong', true)
})
diff --git a/playground/worker/index.html b/playground/worker/index.html
index 302c8e9dc43132..3080bc7bfb5a1a 100644
--- a/playground/worker/index.html
+++ b/playground/worker/index.html
@@ -50,6 +50,12 @@
+
+ import InlineWorker from '../my-worker?worker&inline'
+ .pong-inline-unicode
+
+
+
import TSOutputWorker from '../possible-ts-output-worker?worker'
.pong-ts-output
diff --git a/playground/worker/my-worker.ts b/playground/worker/my-worker.ts
index cbf730f0173de2..ea49a402325954 100644
--- a/playground/worker/my-worker.ts
+++ b/playground/worker/my-worker.ts
@@ -8,6 +8,16 @@ self.onmessage = (e) => {
if (e.data === 'ping') {
self.postMessage({ msg, mode, bundleWithPlugin, viteSvg, metaUrl, name })
}
+ if (e.data === 'ping-unicode') {
+ self.postMessage({
+ msg: '•pong•',
+ mode,
+ bundleWithPlugin,
+ viteSvg,
+ metaUrl,
+ name,
+ })
+ }
}
self.postMessage({
msg,
diff --git a/playground/worker/worker/main-module.js b/playground/worker/worker/main-module.js
index 23d8527f300c5c..6a7a9c6f0471ea 100644
--- a/playground/worker/worker/main-module.js
+++ b/playground/worker/worker/main-module.js
@@ -45,6 +45,12 @@ inlineWorkerUrl.addEventListener('message', (e) => {
text('.pong-inline-url', e.data.metaUrl)
})
+const unicodeInlineWorker = new InlineWorker()
+unicodeInlineWorker.postMessage('ping-unicode')
+unicodeInlineWorker.addEventListener('message', (e) => {
+ text('.pong-inline-unicode', e.data.msg)
+})
+
const startSharedWorker = () => {
const sharedWorker = new mySharedWorker()
sharedWorker.port.addEventListener('message', (event) => {