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): support UTF-8 encoding in inline workers (fixes #12117) #15866

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => new TextDecoder().decode(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);'+`
xiaohk marked this conversation as resolved.
Show resolved Hide resolved
}atob(encodedJs)], { type: "text/javascript;charset=utf-8" });
}decodeBase64(encodedJs)], { type: "text/javascript;charset=utf-8" });
export default function WorkerWrapper(options) {
let objURL;
try {
Expand Down
8 changes: 8 additions & 0 deletions playground/worker/__tests__/es/worker-es.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
6 changes: 6 additions & 0 deletions playground/worker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ <h2 class="format-iife">format iife:</h2>
</p>
<code class="pong-inline-url"></code>

<p>
import InlineWorker from '../my-worker?worker&inline'
<span class="classname">.pong-inline-unicode</span>
</p>
<code class="pong-inline-unicode"></code>

<p>
import TSOutputWorker from '../possible-ts-output-worker?worker'
<span class="classname">.pong-ts-output</span>
Expand Down
10 changes: 10 additions & 0 deletions playground/worker/my-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions playground/worker/worker/main-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Loading