-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat(wasm): add SSR support for .wasm?init
#21102
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
41f6bc3
791ca1b
2a9b355
b971467
a68efaf
642cfce
109d41c
df71c19
11375b1
107abfb
9f6cdad
4955fd0
cccb687
be90786
bc042b3
2dc45f0
0480b1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // this is automatically detected by playground/vitestSetup.ts and will replace | ||
| // the default e2e test serve behavior | ||
|
|
||
| import path from 'node:path' | ||
| import kill from 'kill-port' | ||
| import { hmrPorts, ports, rootDir } from '~utils' | ||
|
|
||
| export const port = ports['ssr-wasm'] | ||
|
|
||
| export async function serve(): Promise<{ close(): Promise<void> }> { | ||
| await kill(port) | ||
|
|
||
| const { createServer } = await import(path.resolve(rootDir, 'server.js')) | ||
| const { app, vite } = await createServer(rootDir, hmrPorts['ssr-wasm']) | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| try { | ||
| const server = app.listen(port, () => { | ||
| resolve({ | ||
| // for test teardown | ||
| async close() { | ||
| await new Promise((resolve) => { | ||
| server.close(resolve) | ||
| }) | ||
| if (vite) { | ||
| await vite.close() | ||
| } | ||
| }, | ||
| }) | ||
| }) | ||
| } catch (e) { | ||
| reject(e) | ||
| } | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { expect, test } from 'vitest' | ||
| import { port } from './serve' | ||
| import { page } from '~utils' | ||
|
|
||
| const url = `http://localhost:${port}` | ||
|
|
||
| test('should work when inlined', async () => { | ||
| await page.goto(`${url}/static-light`) | ||
| expect(await page.textContent('.static-light')).toMatch('42') | ||
| }) | ||
|
|
||
| test('should work when output', async () => { | ||
| await page.goto(`${url}/static-heavy`) | ||
| expect(await page.textContent('.static-heavy')).toMatch('24') | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "name": "@vitejs/test-ssr-wasm", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "node server", | ||
| "serve": "NODE_ENV=production node server", | ||
| "debug": "node --inspect-brk server" | ||
| }, | ||
| "dependencies": {}, | ||
| "devDependencies": { | ||
| "express": "^5.1.0" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import express from 'express' | ||
|
|
||
| const isTest = process.env.VITEST | ||
|
|
||
| export async function createServer(root = process.cwd(), hmrPort) { | ||
| const app = express() | ||
|
|
||
| /** | ||
| * @type {import('vite').ViteDevServer} | ||
| */ | ||
| const vite = await ( | ||
| await import('vite') | ||
| ).createServer({ | ||
| root, | ||
| logLevel: isTest ? 'error' : 'info', | ||
| server: { | ||
| middlewareMode: true, | ||
| watch: { | ||
| // During tests we edit the files too fast and sometimes chokidar | ||
| // misses change events, so enforce polling for consistency | ||
| usePolling: true, | ||
| interval: 100, | ||
| }, | ||
| hmr: { | ||
| port: hmrPort, | ||
| }, | ||
| }, | ||
| appType: 'custom', | ||
| }) | ||
| // use vite's connect instance as middleware | ||
| app.use(vite.middlewares) | ||
|
|
||
| app.use('*all', async (req, res, next) => { | ||
| try { | ||
| const url = req.originalUrl | ||
| const render = (await vite.ssrLoadModule('/src/app.js')).render | ||
| const html = await render(url) | ||
| res.status(200).set({ 'Content-Type': 'text/html' }).end(html) | ||
| } catch (e) { | ||
| vite && vite.ssrFixStacktrace(e) | ||
| if (isTest) throw e | ||
| console.log(e.stack) | ||
| res.status(500).end(e.stack) | ||
| } | ||
| }) | ||
|
|
||
| return { app, vite } | ||
| } | ||
|
|
||
| if (!isTest) { | ||
| createServer().then(({ app }) => | ||
| app.listen(5173, () => { | ||
| console.log('http://localhost:5173') | ||
| }), | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export async function render(url) { | ||
| switch (url) { | ||
| case '/static-light': | ||
| return (await import('./static-light')).render() | ||
| case '/static-heavy': | ||
| return (await import('./static-heavy')).render() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../wasm/heavy.wasm |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../wasm/light.wasm |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import heavy from './heavy.wasm?init' | ||
|
|
||
| export async function render() { | ||
| let result | ||
| const { exported_func } = await heavy({ | ||
| imports: { | ||
| imported_func: (res) => (result = res), | ||
| }, | ||
| }).then((i) => i.exports) | ||
| exported_func() | ||
| return `<div class="static-heavy">${result}</div>` | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import light from './light.wasm?init' | ||
|
|
||
| export async function render() { | ||
| let result | ||
| const { exported_func } = await light({ | ||
| imports: { | ||
| imported_func: (res) => (result = res), | ||
| }, | ||
| }).then((i) => i.exports) | ||
| exported_func() | ||
| return `<div class="static-light">${result}</div>` | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { defineConfig } from 'vite' | ||
| export default defineConfig({ | ||
| build: { | ||
| // make cannot emit light.wasm | ||
| assetsInlineLimit: 80, | ||
| }, | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,67 @@ | ||
| import { expect, test } from 'vitest' | ||
| import { describe, expect, test } from 'vitest' | ||
| import { isBuild, page } from '~utils' | ||
|
|
||
| test('should work when inlined', async () => { | ||
| await page.click('.inline-wasm .run') | ||
| await expect | ||
| .poll(() => page.textContent('.inline-wasm .result')) | ||
| .toMatch('42') | ||
| }) | ||
| describe('WASM in browser', () => { | ||
| test('should work when inlined', async () => { | ||
| await page.click('.inline-wasm .run') | ||
| await expect | ||
| .poll(() => page.textContent('.inline-wasm .result')) | ||
| .toMatch('42') | ||
| }) | ||
|
|
||
| test('should work when output', async () => { | ||
| await page.click('.output-wasm .run') | ||
| await expect | ||
| .poll(() => page.textContent('.output-wasm .result')) | ||
| .toMatch('24') | ||
| }) | ||
| test('should work when output', async () => { | ||
| await page.click('.output-wasm .run') | ||
| await expect | ||
| .poll(() => page.textContent('.output-wasm .result')) | ||
| .toMatch('24') | ||
| }) | ||
|
|
||
| test('init function returns WebAssembly.Instance', async () => { | ||
| await page.click('.init-returns-instance .run') | ||
| await expect | ||
| .poll(() => page.textContent('.init-returns-instance .result')) | ||
| .toMatch('true') | ||
| }) | ||
| test('init function returns WebAssembly.Instance', async () => { | ||
| await page.click('.init-returns-instance .run') | ||
| await expect | ||
| .poll(() => page.textContent('.init-returns-instance .result')) | ||
| .toMatch('true') | ||
| }) | ||
|
|
||
| test('?url', async () => { | ||
| expect(await page.textContent('.url')).toMatch( | ||
| isBuild ? 'data:application/wasm' : '/light.wasm', | ||
| ) | ||
| test('?url', async () => { | ||
| expect(await page.textContent('.url')).toMatch( | ||
| isBuild ? 'data:application/wasm' : '/light.wasm', | ||
| ) | ||
| }) | ||
|
|
||
| test('should work when wasm in worker', async () => { | ||
| await expect | ||
| .poll(() => page.textContent('.worker-wasm .result')) | ||
| .toMatch('3') | ||
| }) | ||
| }) | ||
|
|
||
| test('should work when wasm in worker', async () => { | ||
| await expect.poll(() => page.textContent('.worker-wasm .result')).toMatch('3') | ||
| describe('WASM in vitest', () => { | ||
| test('should work when inlined', async () => { | ||
| const { default: light } = await import('../light.wasm?init') | ||
| let result | ||
| const instance = await light({ | ||
| imports: { | ||
| imported_func: (res) => (result = res), | ||
| }, | ||
| }) | ||
| expect(instance instanceof WebAssembly.Instance).toBe(true) | ||
| // @ts-ignore | ||
| instance.exports.exported_func() | ||
| expect(result).toBe(42) | ||
| }) | ||
|
|
||
| test('should work when output', async () => { | ||
| const { default: heavy } = await import('../heavy.wasm?init') | ||
| let result | ||
| const instance = await heavy({ | ||
| imports: { | ||
| imported_func: (res) => (result = res), | ||
| }, | ||
| }) | ||
| expect(instance instanceof WebAssembly.Instance).toBe(true) | ||
| // @ts-ignore | ||
| instance.exports.exported_func() | ||
| expect(result).toBe(24) | ||
| }) | ||
|
||
| }) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.