-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
208 additions
and
11 deletions.
There are no files selected for viewing
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 @@ | ||
import { expect, test } from "@playwright/test"; | ||
import { createEditor, testNoJs } from "../../../e2e/helper"; | ||
|
||
testNoJs("ssr", async ({ page }) => { | ||
await page.goto("/"); | ||
await page.getByText("hydrated: false").click(); | ||
}); | ||
|
||
test("csr", async ({ page }) => { | ||
await page.goto("/"); | ||
await page.getByText("hydrated: true").click(); | ||
}); | ||
|
||
test("hmr", async ({ page, request }) => { | ||
await page.goto("/"); | ||
await page.getByText("hydrated: true").click(); | ||
|
||
await page.getByRole("button", { name: "Count: 0" }).click(); | ||
|
||
using file = createEditor("./src/app.tsx"); | ||
file.edit((s) => s.replace("Count:", "Count-EDIT:")); | ||
|
||
await page.getByRole("button", { name: "Count-EDIT: 1" }).click(); | ||
|
||
file.edit((s) => s.replace("Count-EDIT:", "Count-EDIT-EDIT:")); | ||
await page.getByRole("button", { name: "Count-EDIT-EDIT: 2" }).click(); | ||
|
||
// server module is also invalidated | ||
const res = await request.get("/"); | ||
expect(await res.text()).toContain("Count-EDIT-EDIT"); | ||
}); |
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,17 @@ | ||
{ | ||
"name": "@hiogawa/viteroll-example-ssr", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"test-e2e": "playwright test" | ||
}, | ||
"dependencies": { | ||
"react": "latest", | ||
"react-dom": "latest" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "latest", | ||
"@types/react-dom": "latest" | ||
} | ||
} |
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 { default } from "../../playwright.config.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,21 @@ | ||
import React from "react"; | ||
|
||
export function App() { | ||
const [count, setCount] = React.useState(0); | ||
return ( | ||
<div> | ||
<h1>Rolldown SSR</h1> | ||
<Hydrated /> | ||
<button onClick={() => setCount((c) => c + 1)}>Count: {count}</button> | ||
</div> | ||
); | ||
} | ||
|
||
function Hydrated() { | ||
const hydrated = React.useSyncExternalStore( | ||
React.useCallback(() => () => {}, []), | ||
() => true, | ||
() => false, | ||
); | ||
return <p>hydrated: {String(hydrated)}</p>; | ||
} |
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 React from "react"; | ||
import ReactDOMClient from "react-dom/client"; | ||
import { App } from "./app"; | ||
|
||
React.startTransition(() => { | ||
ReactDOMClient.hydrateRoot(document.getElementById("root")!, <App />); | ||
}); |
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,24 @@ | ||
import type { Connect } from "vite"; | ||
|
||
const handler: Connect.SimpleHandleFunction = (req, res) => { | ||
const url = new URL(req.url ?? "/", "https://vite.dev"); | ||
console.log(`[SSR] ${req.method} ${url.pathname}`); | ||
if (url.pathname === "/crash-ssr") { | ||
throw new Error("crash-ssr"); | ||
} | ||
res.setHeader("content-type", "text/html"); | ||
// TODO: transformIndexHtml? | ||
res.end(`\ | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
</head> | ||
<body> | ||
Test SSR | ||
</body> | ||
</html> | ||
`); | ||
}; | ||
|
||
export default handler; |
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,16 @@ | ||
{ | ||
"include": ["src", "*.ts"], | ||
"compilerOptions": { | ||
"strict": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"verbatimModuleSyntax": true, | ||
"allowImportingTsExtensions": true, | ||
"moduleResolution": "Bundler", | ||
"module": "ESNext", | ||
"target": "ESNext", | ||
"lib": ["ESNext", "DOM"], | ||
"jsx": "react-jsx", | ||
"noEmit": true | ||
} | ||
} |
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,54 @@ | ||
import { defineConfig } from "vite"; | ||
import { RolldownEnvironment, viteroll } from "../../viteroll"; | ||
|
||
process.setSourceMapsEnabled(true); | ||
|
||
export default defineConfig({ | ||
environments: { | ||
client: { | ||
build: { | ||
outDir: "dist/client", | ||
rollupOptions: { | ||
input: "./src/entry-client", | ||
}, | ||
}, | ||
}, | ||
ssr: { | ||
build: { | ||
outDir: "dist/server", | ||
rollupOptions: { | ||
input: { | ||
index: "./src/entry-server", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
plugins: [ | ||
viteroll({ | ||
reactRefresh: true, | ||
ssrModuleRunner: true, | ||
}), | ||
{ | ||
name: "ssr-middleware", | ||
config() { | ||
return { | ||
appType: "custom", | ||
}; | ||
}, | ||
configureServer(server) { | ||
return () => { | ||
const devEnv = server.environments.ssr as RolldownEnvironment; | ||
server.middlewares.use(async (req, res, next) => { | ||
try { | ||
const mod = (await devEnv.import("src/entry-server.tsx")) as any; | ||
await mod.default(req, res); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}); | ||
}; | ||
}, | ||
}, | ||
], | ||
}); |
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