-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: errors in worker handling (#7236)
- Loading branch information
Showing
35 changed files
with
438 additions
and
196 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
packages/playground/worker/__tests__/es/es-worker.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { untilUpdated, isBuild, testDir } from '../../../testUtils' | ||
import type { Page } from 'playwright-chromium' | ||
|
||
test('normal', async () => { | ||
await page.click('.ping') | ||
await untilUpdated(() => page.textContent('.pong'), 'pong') | ||
await untilUpdated( | ||
() => page.textContent('.mode'), | ||
isBuild ? 'production' : 'development' | ||
) | ||
await untilUpdated( | ||
() => page.textContent('.bundle-with-plugin'), | ||
'worker bundle with plugin success!' | ||
) | ||
}) | ||
|
||
test('TS output', async () => { | ||
await page.click('.ping-ts-output') | ||
await untilUpdated(() => page.textContent('.pong-ts-output'), 'pong') | ||
}) | ||
|
||
test('inlined', async () => { | ||
await page.click('.ping-inline') | ||
await untilUpdated(() => page.textContent('.pong-inline'), 'pong') | ||
}) | ||
|
||
const waitSharedWorkerTick = ( | ||
(resolvedSharedWorkerCount: number) => async (page: Page) => { | ||
await untilUpdated(async () => { | ||
const count = await page.textContent('.tick-count') | ||
// ignore the initial 0 | ||
return count === '1' ? 'page loaded' : '' | ||
}, 'page loaded') | ||
// test.concurrent sequential is not guaranteed | ||
// force page to wait to ensure two pages overlap in time | ||
resolvedSharedWorkerCount++ | ||
if (resolvedSharedWorkerCount < 2) return | ||
|
||
await untilUpdated(() => { | ||
return resolvedSharedWorkerCount === 2 ? 'all pages loaded' : '' | ||
}, 'all pages loaded') | ||
} | ||
)(0) | ||
|
||
test.concurrent.each([[true], [false]])('shared worker', async (doTick) => { | ||
if (doTick) { | ||
await page.click('.tick-shared') | ||
} | ||
await waitSharedWorkerTick(page) | ||
}) | ||
|
||
test('worker emitted', async () => { | ||
await untilUpdated(() => page.textContent('.nested-worker'), 'pong') | ||
}) | ||
|
||
if (isBuild) { | ||
const assetsDir = path.resolve(testDir, 'dist/es/assets') | ||
// assert correct files | ||
test('inlined code generation', async () => { | ||
const files = fs.readdirSync(assetsDir) | ||
expect(files.length).toBe(20) | ||
const index = files.find((f) => f.includes('main-module')) | ||
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8') | ||
const worker = files.find((f) => f.includes('my-worker')) | ||
const workerContent = fs.readFileSync( | ||
path.resolve(assetsDir, worker), | ||
'utf-8' | ||
) | ||
|
||
// worker should have all imports resolved and no exports | ||
expect(workerContent).not.toMatch(`import`) | ||
expect(workerContent).not.toMatch(`export`) | ||
// chunk | ||
expect(content).toMatch(`new Worker("/es/assets`) | ||
expect(content).toMatch(`new SharedWorker("/es/assets`) | ||
// inlined | ||
expect(content).toMatch(`(window.URL||window.webkitURL).createObjectURL`) | ||
expect(content).toMatch(`window.Blob`) | ||
}) | ||
} | ||
|
||
test('module worker', async () => { | ||
expect(await page.textContent('.shared-worker-import-meta-url')).toMatch( | ||
'A string' | ||
) | ||
}) | ||
|
||
test('classic worker', async () => { | ||
expect(await page.textContent('.classic-worker')).toMatch('A classic') | ||
expect(await page.textContent('.classic-shared-worker')).toMatch('A classic') | ||
}) | ||
|
||
test('emit chunk', async () => { | ||
expect(await page.textContent('.emti-chunk-worker')).toMatch( | ||
'{"msg1":"module1","msg2":"module2","msg3":"module3"}' | ||
) | ||
expect(await page.textContent('.emti-chunk-dynamic-import-worker')).toMatch( | ||
'"A string/es/"' | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../../vite.config-es') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...nd/worker/newUrl/classic-shared-worker.js → ...layground/worker/classic-shared-worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,5 @@ | ||
// prettier-ignore | ||
function text(el, text) { | ||
document.querySelector(el).textContent = text | ||
} | ||
importScripts(`/${self.location.pathname.split("/")[1]}/classic.js`) | ||
|
||
const classicWorker = new Worker( | ||
new URL('./newUrl/classic-worker.js', import.meta.url) /* , */ , | ||
// test comment | ||
|
||
) | ||
|
||
classicWorker.addEventListener('message', ({ data }) => { | ||
text('.classic-worker', data) | ||
}) | ||
classicWorker.postMessage('ping') | ||
|
||
const classicSharedWorker = new SharedWorker( | ||
new URL('./newUrl/classic-shared-worker.js', import.meta.url), | ||
{ | ||
type: 'classic' | ||
} | ||
) | ||
classicSharedWorker.port.addEventListener('message', (ev) => { | ||
text( | ||
'.classic-shared-worker', | ||
ev.data | ||
) | ||
self.addEventListener('message', () => { | ||
self.postMessage(self.constant) | ||
}) | ||
classicSharedWorker.port.start() |
3 changes: 3 additions & 0 deletions
3
packages/playground/worker/emit-chunk-dynamic-import-worker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import('./modules/module').then((module) => { | ||
self.postMessage(module.default + import.meta.env.BASE_URL) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import SubWorker from './emit-chunk-sub-worker?worker' | ||
|
||
const subWorker = new SubWorker() | ||
|
||
subWorker.onmessage = (event) => { | ||
self.postMessage(event.data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Promise.all([import('./modules/module2'), import('./modules/module3')]).then( | ||
(data) => { | ||
const _data = { ...data[0], ...data[1] } | ||
self.postMessage(_data) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const msg1 = 'module1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './module' | ||
export * from './module1' | ||
export const msg2 = 'module2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './module' | ||
export const msg3 = 'module3' |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.