Skip to content

Commit ab12dfa

Browse files
authored
test: simplify playground/ssr-react (#397)
1 parent f099656 commit ab12dfa

File tree

8 files changed

+90
-670
lines changed

8 files changed

+90
-670
lines changed

playground/ssr-react/__tests__/serve.ts

-70
This file was deleted.

playground/ssr-react/__tests__/ssr-react.spec.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import fetch from 'node-fetch'
22
import { expect, test } from 'vitest'
3-
import { port } from './serve'
43
import {
54
browserLogs,
65
editFile,
76
isBuild,
87
page,
98
untilBrowserLogAfter,
109
untilUpdated,
10+
viteTestUrl as url,
1111
} from '~utils'
1212

13-
const url = `http://localhost:${port}`
14-
1513
test('/env', async () => {
1614
await untilBrowserLogAfter(() => page.goto(url + '/env'), 'hydrated')
1715

playground/ssr-react/package.json

+4-11
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@
33
"private": true,
44
"type": "module",
55
"scripts": {
6-
"dev": "node server",
7-
"build": "npm run build:client && npm run build:server",
8-
"build:client": "vite build --outDir dist/client",
9-
"build:server": "vite build --ssr src/entry-server.jsx --outDir dist/server",
10-
"generate": "vite build --outDir dist/static && npm run build:server && node prerender",
11-
"serve": "NODE_ENV=production node server",
12-
"debug": "node --inspect-brk server"
6+
"dev": "vite dev",
7+
"build": "vite build --app",
8+
"preview": "vite preview"
139
},
1410
"dependencies": {
1511
"react": "^18.3.1",
1612
"react-dom": "^18.3.1"
1713
},
1814
"devDependencies": {
19-
"@vitejs/plugin-react": "workspace:*",
20-
"compression": "^1.7.5",
21-
"express": "^4.21.1",
22-
"serve-static": "^1.16.2"
15+
"@vitejs/plugin-react": "workspace:*"
2316
}
2417
}

playground/ssr-react/prerender.js

-33
This file was deleted.

playground/ssr-react/server.js

-97
This file was deleted.

playground/ssr-react/vite.config.js

+70-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,78 @@
1+
import fs from 'node:fs'
2+
import path from 'node:path'
13
import { defineConfig } from 'vite'
24
import react from '@vitejs/plugin-react'
35

6+
process.env.MY_CUSTOM_SECRET = 'API_KEY_qwertyuiop'
7+
48
export default defineConfig({
5-
server: { port: 8907 /* Should be unique */ },
6-
plugins: [react()],
9+
appType: 'custom',
710
build: {
811
minify: false,
912
},
13+
environments: {
14+
client: {
15+
build: {
16+
outDir: 'dist/client',
17+
},
18+
},
19+
ssr: {
20+
build: {
21+
outDir: 'dist/server',
22+
rollupOptions: {
23+
input: 'src/entry-server.jsx',
24+
},
25+
},
26+
},
27+
},
28+
plugins: [
29+
react(),
30+
{
31+
name: 'ssr-middleware',
32+
configureServer(server) {
33+
return () => {
34+
server.middlewares.use(async (req, res, next) => {
35+
const url = req.originalUrl ?? '/'
36+
try {
37+
const { render } = await server.ssrLoadModule(
38+
'/src/entry-server.jsx',
39+
)
40+
const appHtml = render(url)
41+
const template = await server.transformIndexHtml(
42+
url,
43+
fs.readFileSync(path.resolve('index.html'), 'utf-8'),
44+
)
45+
const html = template.replace(`<!--app-html-->`, appHtml)
46+
res.setHeader('content-type', 'text/html').end(html)
47+
} catch (e) {
48+
next(e)
49+
}
50+
})
51+
}
52+
},
53+
async configurePreviewServer(server) {
54+
const template = fs.readFileSync(
55+
path.resolve('dist/client/index.html'),
56+
'utf-8',
57+
)
58+
const { render } = await import(
59+
new URL('./dist/server/entry-server.js', import.meta.url).href
60+
)
61+
return () => {
62+
server.middlewares.use(async (req, res, next) => {
63+
const url = req.originalUrl ?? '/'
64+
try {
65+
const appHtml = render(url)
66+
const html = template.replace(`<!--app-html-->`, appHtml)
67+
res.setHeader('content-type', 'text/html').end(html)
68+
} catch (e) {
69+
next(e)
70+
}
71+
})
72+
}
73+
},
74+
},
75+
],
76+
// tell vitestSetup.ts to use buildApp API
77+
builder: {},
1078
})

playground/vitestSetup.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
} from 'vite'
1515
import {
1616
build,
17+
createBuilder,
1718
createServer,
1819
loadConfigFromFile,
1920
mergeConfig,
@@ -253,6 +254,7 @@ export async function startDefaultServe(): Promise<void> {
253254
viteTestUrl = `http://localhost:${server.config.server.port}${
254255
devBase === '/' ? '' : devBase
255256
}`
257+
setViteUrl(viteTestUrl)
256258
await page.goto(viteTestUrl)
257259
} else {
258260
process.env.VITE_INLINE = 'inline-build'
@@ -267,12 +269,17 @@ export async function startDefaultServe(): Promise<void> {
267269
const testConfig = mergeConfig(options, config || {})
268270
viteConfig = testConfig
269271
process.chdir(rootDir)
270-
const rollupOutput = await build(testConfig)
271-
const isWatch = !!resolvedConfig!.build.watch
272-
// in build watch,call startStaticServer after the build is complete
273-
if (isWatch) {
274-
watcher = rollupOutput as Rollup.RollupWatcher
275-
await notifyRebuildComplete(watcher)
272+
if (testConfig.builder) {
273+
const builder = await createBuilder(testConfig)
274+
await builder.buildApp()
275+
} else {
276+
const rollupOutput = await build(testConfig)
277+
const isWatch = !!resolvedConfig!.build.watch
278+
// in build watch,call startStaticServer after the build is complete
279+
if (isWatch) {
280+
watcher = rollupOutput as Rollup.RollupWatcher
281+
await notifyRebuildComplete(watcher)
282+
}
276283
}
277284
// @ts-ignore
278285
if (config && config.__test__) {
@@ -284,6 +291,8 @@ export async function startDefaultServe(): Promise<void> {
284291
// prevent preview change NODE_ENV
285292
process.env.NODE_ENV = _nodeEnv
286293
viteTestUrl = previewServer.resolvedUrls.local[0]
294+
viteTestUrl = viteTestUrl.replace(/\/+$/, '')
295+
setViteUrl(viteTestUrl)
287296
await page.goto(viteTestUrl)
288297
}
289298
}

0 commit comments

Comments
 (0)