forked from vitejs/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.js
52 lines (47 loc) · 1.14 KB
/
serve.js
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
// @ts-check
// this is automtically detected by scripts/jestPerTestSetup.ts and will replace
// the default e2e test serve behavior
const path = require('path')
const port = (exports.port = 9527)
/**
* @param {string} root
* @param {boolean} _isProd
*/
exports.serve = async function serve(root, _isProd) {
const { build } = require('vite')
await build({
root,
logLevel: 'silent',
build: {
target: 'esnext',
ssr: 'entry-server.js',
outDir: 'dist/server'
}
})
const express = require('express')
const app = express()
app.use('/', async (_req, res) => {
const { render } = require(path.resolve(
root,
'./dist/server/entry-server.js'
))
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)
}
})
}