-
-
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
Open
upsuper
wants to merge
17
commits into
vitejs:main
Choose a base branch
from
upsuper-forks:wasm-node
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+309
−20
Open
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
41f6bc3
Add wasm support for test and SSR
upsuper 791ca1b
fixes
upsuper 2a9b355
fix
upsuper b971467
handle windows and unix path
upsuper a68efaf
trying
upsuper 642cfce
fix
upsuper 109d41c
correctly handle ssr build
upsuper df71c19
Merge branch 'main' into wasm-node
upsuper 11375b1
add build tests to verify assets
upsuper 107abfb
Merge branch 'main' into wasm-node
upsuper 9f6cdad
handle wasm init specially
upsuper 4955fd0
Merge branch 'main' into wasm-node
upsuper cccb687
update express in ssr-wasm
upsuper be90786
Merge branch 'main' into wasm-node
upsuper bc042b3
remove vitest tests
upsuper 2dc45f0
update dependencies
upsuper 0480b1b
fix import order
upsuper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| // 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 { build } from 'vite' | ||
| import { hmrPorts, isBuild, ports, rootDir } from '~utils' | ||
|
|
||
| export const port = ports['ssr-wasm'] | ||
|
|
||
| export async function preServe() { | ||
| if (isBuild) { | ||
| await build({ root: rootDir }) | ||
| } | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or 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,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') | ||
| }) |
This file contains hidden or 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,15 @@ | ||
| { | ||
| "name": "@vitejs/test-ssr-wasm", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "node server", | ||
| "build": "vite build", | ||
| "preview": "NODE_ENV=production node server" | ||
| }, | ||
| "dependencies": {}, | ||
| "devDependencies": { | ||
| "express": "^5.1.0" | ||
| } | ||
| } |
This file contains hidden or 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,60 @@ | ||
| import express from 'express' | ||
|
|
||
| const isTest = process.env.VITEST | ||
| const isProduction = process.env.NODE_ENV === 'production' | ||
|
|
||
| export async function createServer(root = process.cwd(), hmrPort) { | ||
| const app = express() | ||
|
|
||
| /** @type {import('vite').ViteDevServer} */ | ||
| let vite | ||
| if (!isProduction) { | ||
| 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) => { | ||
sapphi-red marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| const url = req.originalUrl | ||
| const render = isProduction | ||
| ? (await import('./dist/app.js')).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?.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') | ||
| }), | ||
| ) | ||
| } | ||
This file contains hidden or 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,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() | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| ../../wasm/heavy.wasm |
This file contains hidden or 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 @@ | ||
| ../../wasm/light.wasm |
This file contains hidden or 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,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>` | ||
| } |
This file contains hidden or 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,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>` | ||
| } |
This file contains hidden or 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,9 @@ | ||
| import { defineConfig } from 'vite' | ||
| export default defineConfig({ | ||
| build: { | ||
| // make cannot emit light.wasm | ||
| assetsInlineLimit: 80, | ||
| ssr: './src/app.js', | ||
| ssrEmitAssets: true, | ||
| }, | ||
| }) |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.