-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
serve.ts
68 lines (60 loc) · 2.18 KB
/
serve.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// this is automatically detected by playground/vitestSetup.ts and will replace
// the default e2e test serve behavior
import path from 'node:path'
import { ports, rootDir } from '~utils'
export const port = ports['legacy/client-and-ssr']
export async function serve(): Promise<{ close(): Promise<void> }> {
const { build } = await import('vite')
// In a CLI app it is possible that you may run `build` several times one after another
// For example, you may want to override an option specifically for the SSR build
// And you may have a CLI app built for that purpose to make a more concise API
// An unexpected behaviour is for the plugin-legacy to override the process.env.NODE_ENV value
// And any build after the first client build that called plugin-legacy will misbehave and
// build with process.env.NODE_ENV=production, rather than your CLI's env: NODE_ENV=myWhateverEnv my-cli-app build
// The issue is with plugin-legacy's index.ts file not explicitly passing mode: process.env.NODE_ENV to vite's build function
// This causes vite to call resolveConfig with defaultMode = 'production' and mutate process.env.NODE_ENV to 'production'
await build({
mode: process.env.NODE_ENV,
root: rootDir,
logLevel: 'silent',
build: {
target: 'esnext',
outDir: 'dist/client'
}
})
await build({
mode: process.env.NODE_ENV,
root: rootDir,
logLevel: 'silent',
build: {
target: 'esnext',
ssr: 'entry-server-sequential.js',
outDir: 'dist/server'
}
})
const { default: express } = await import('express')
const app = express()
app.use('/', async (_req, res) => {
const { render } = await import(
path.resolve(rootDir, './dist/server/entry-server-sequential.mjs')
)
const html = await render()
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
})
return new Promise((resolve, reject) => {
try {
const server = app.listen(port, () => {
resolve({
// for test teardown
async close() {
await new Promise((resolve) => {
server.close(resolve)
})
}
})
})
} catch (e) {
reject(e)
}
})
}