Skip to content

Commit

Permalink
fix(vite): add inline worker test and fix named worker test
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaubee committed Sep 13, 2023
1 parent 170d7d8 commit b98f497
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
14 changes: 11 additions & 3 deletions playground/worker/__tests__/es/es-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('normal', async () => {
})

test('named', async () => {
await untilUpdated(() => page.textContent('.pong-named'), 'pong', true)
await untilUpdated(() => page.textContent('.pong-named'), 'namedWorker', true)
})

test('TS output', async () => {
Expand All @@ -35,7 +35,15 @@ test('inlined', async () => {
})

test('named inlined', async () => {
await untilUpdated(() => page.textContent('.pong-inline-named'), 'pong', true)
await untilUpdated(
() => page.textContent('.pong-inline-named'),
'namedInlineWorker',
true,
)
})

test('import meta url', async () => {
await untilUpdated(() => page.textContent('.pong-inline-url'), /^blob:/, true)
})

test('shared worker', async () => {
Expand Down Expand Up @@ -83,7 +91,7 @@ describe.runIf(isBuild)('build', () => {
)

// worker should have all imports resolved and no exports
expect(workerContent).not.toMatch(`import`)
expect(workerContent).not.toMatch(/import[^.]/)
expect(workerContent).not.toMatch(`export`)
// chunk
expect(content).toMatch(`new Worker("/es/assets`)
Expand Down
8 changes: 6 additions & 2 deletions playground/worker/__tests__/iife/iife-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('normal', async () => {
})

test('named', async () => {
await untilUpdated(() => page.textContent('.pong-named'), 'pong', true)
await untilUpdated(() => page.textContent('.pong-named'), 'namedWorker', true)
})

test('TS output', async () => {
Expand All @@ -38,7 +38,11 @@ test('inlined', async () => {
})

test('named inlined', async () => {
await untilUpdated(() => page.textContent('.pong-inline-named'), 'pong', true)
await untilUpdated(
() => page.textContent('.pong-inline-named'),
'namedInlineWorker',
true,
)
})

test('shared worker', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('normal', async () => {
})

test('named', async () => {
await untilUpdated(() => page.textContent('.pong-named'), 'pong', true)
await untilUpdated(() => page.textContent('.pong-named'), 'namedWorker', true)
})

test('TS output', async () => {
Expand Down
8 changes: 8 additions & 0 deletions playground/worker/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ <h2 class="format-iife">format iife:</h2>
</p>
<code class="pong-inline-named"></code>

<p>
import InlineWorker from '../my-worker?worker&inline'
<span>new InlineWorker()</span>
<span>import.meta.url</span>
<span class="classname">.pong-inline-url</span>
</p>
<code class="pong-inline-url"></code>

<p>
import TSOutputWorker from '../possible-ts-output-worker?worker'
<span class="classname">.pong-ts-output</span>
Expand Down
13 changes: 11 additions & 2 deletions playground/worker/my-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ import { msg as msgFromDep } from '@vitejs/test-dep-to-optimize'
import { mode, msg } from './modules/workerImport.js'
import { bundleWithPlugin } from './modules/test-plugin'
import viteSvg from './vite.svg'
const metaUrl = import.meta.url

self.onmessage = (e) => {
if (e.data === 'ping') {
self.postMessage({ msg, mode, bundleWithPlugin, viteSvg })
self.postMessage({ msg, mode, bundleWithPlugin, viteSvg, metaUrl, name })
}
}
self.postMessage({ msg, mode, bundleWithPlugin, msgFromDep, viteSvg })
self.postMessage({
msg,
mode,
bundleWithPlugin,
msgFromDep,
viteSvg,
metaUrl,
name,
})

// for sourcemap
console.log('my-worker.js')
10 changes: 8 additions & 2 deletions playground/worker/worker/main-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ worker.addEventListener('message', (e) => {
const namedWorker = new myWorker({ name: 'namedWorker' })
namedWorker.postMessage('ping')
namedWorker.addEventListener('message', (e) => {
text('.pong-named', e.data.msg)
text('.pong-named', e.data.name)
})

const inlineWorker = new InlineWorker()
Expand All @@ -36,7 +36,13 @@ inlineWorker.addEventListener('message', (e) => {
const namedInlineWorker = new InlineWorker({ name: 'namedInlineWorker' })
namedInlineWorker.postMessage('ping')
namedInlineWorker.addEventListener('message', (e) => {
text('.pong-inline-named', e.data.msg)
text('.pong-inline-named', e.data.name)
})

const inlineWorkerUrl = new InlineWorker()
inlineWorkerUrl.postMessage('ping')
inlineWorkerUrl.addEventListener('message', (e) => {
text('.pong-inline-url', e.data.metaUrl)
})

const startSharedWorker = () => {
Expand Down

0 comments on commit b98f497

Please # to comment.