-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(build): support for firefox/safari workers
- Loading branch information
Showing
9 changed files
with
184 additions
and
42 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
packages/playground/worker-code-split/__tests__/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,49 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { untilUpdated, isBuild, testDir } from '../../testUtils' | ||
|
||
test('normal', async () => { | ||
await page.click('.ping') | ||
await untilUpdated(() => page.textContent('.pong'), 'pong') | ||
await untilUpdated( | ||
() => page.textContent('.mode'), | ||
isBuild ? 'production' : 'development' | ||
) | ||
}) | ||
|
||
test('legacy', async () => { | ||
await page.click('.ping-inline') | ||
await untilUpdated(() => page.textContent('.pong-inline'), 'pong') | ||
}) | ||
|
||
if (isBuild) { | ||
// assert correct files | ||
test('inlined code generation', async () => { | ||
const assetsDir = path.resolve(testDir, 'dist/assets') | ||
const files = fs.readdirSync(assetsDir) | ||
// should have 1 worker chunk for module and legacy | ||
expect(files.length).toBe(6) | ||
expect(files[5]).toMatch('workerImport.') | ||
|
||
const index = files.find((f) => f.includes('index')) | ||
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8') | ||
// chunk | ||
expect(content).toMatch(`new Worker("/assets`) | ||
// not inlined | ||
expect(content).not.toMatch(`new Worker("data:application/javascript`) | ||
|
||
const workers = files.filter((f) => f.includes('my-worker-1')) | ||
expect(workers.length).toBe(2) | ||
const moduleContent = fs.readFileSync( | ||
path.resolve(assetsDir, workers[0]), | ||
'utf-8' | ||
) | ||
const scriptContent = fs.readFileSync( | ||
path.resolve(assetsDir, workers[1]), | ||
'utf-8' | ||
) | ||
// module worker | ||
expect(moduleContent).toMatch(/import{.+}from".\/workerImport/) | ||
expect(scriptContent).not.toMatch('import') | ||
}) | ||
} |
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,31 @@ | ||
<button class="ping">Ping</button> | ||
<div> | ||
Response from worker: <span class="pong"></span><span class="mode"></span> | ||
</div> | ||
|
||
<button class="ping-inline">Ping Inline Worker</button> | ||
<div>Response from inline worker: <span class="pong-inline"></span></div> | ||
|
||
<script type="module"> | ||
import Worker1 from './my-worker-1?worker' | ||
import Worker2 from './my-worker-2?worker' | ||
|
||
const worker1 = new Worker1() | ||
worker1.addEventListener('message', (e) => { | ||
document.querySelector('.pong').textContent = e.data.msg | ||
document.querySelector('.mode').textContent = e.data.mode | ||
}) | ||
|
||
document.querySelector('.ping').addEventListener('click', () => { | ||
worker1.postMessage('ping') | ||
}) | ||
|
||
const worker2 = new Worker2() | ||
worker2.addEventListener('message', (e) => { | ||
document.querySelector('.pong-inline').textContent = e.data.msg | ||
}) | ||
|
||
document.querySelector('.ping-inline').addEventListener('click', () => { | ||
worker2.postMessage('ping') | ||
}) | ||
</script> |
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 { msg, mode } from './workerImport' | ||
|
||
self.onmessage = (e) => { | ||
if (e.data === 'ping') { | ||
self.postMessage({ msg, mode }) | ||
} | ||
} |
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 { msg, mode } from './workerImport' | ||
|
||
self.onmessage = (e) => { | ||
if (e.data === 'ping') { | ||
self.postMessage({ msg, mode }) | ||
} | ||
} |
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,11 @@ | ||
{ | ||
"name": "test-worker-code-split", | ||
"private": true, | ||
"version": "0.0.0", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"debug": "node --inspect-brk ../../vite/bin/vite", | ||
"serve": "vite preview" | ||
} | ||
} |
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 const msg = 'pong' | ||
export const mode = process.env.NODE_ENV |
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
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