MCPcopy
hub / github.com/vitejs/vite / createServer

Function createServer

playground/ssr-wasm/server.js:6–52  ·  view source on GitHub ↗
(root = process.cwd(), hmrPort)

Source from the content-addressed store, hash-verified

4const isProduction = process.env.NODE_ENV === 'production'
5
6export async function createServer(root = process.cwd(), hmrPort) {
7 const app = express()
8
9 /** @type {import('vite').ViteDevServer} */
10 let vite
11 if (!isProduction) {
12 vite = await (
13 await import('vite')
14 ).createServer({
15 root,
16 logLevel: isTest ? 'error' : 'info',
17 server: {
18 middlewareMode: true,
19 watch: {
20 // During tests we edit the files too fast and sometimes chokidar
21 // misses change events, so enforce polling for consistency
22 usePolling: true,
23 interval: 100,
24 },
25 hmr: {
26 port: hmrPort,
27 },
28 },
29 appType: 'custom',
30 })
31 // use vite's connect instance as middleware
32 app.use(vite.middlewares)
33 }
34
35 app.use('*all', async (req, res, next) => {
36 try {
37 const url = req.originalUrl
38 const render = isProduction
39 ? (await import('./dist/app.js')).render
40 : (await vite.ssrLoadModule('/src/app.js')).render
41 const html = await render(url)
42 res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
43 } catch (e) {
44 vite?.ssrFixStacktrace(e)
45 if (isTest) throw e
46 console.log(e.stack)
47 res.status(500).end(e.stack)
48 }
49 })
50
51 return { app, vite }
52}
53
54if (!isTest) {
55 createServer().then(({ app }) =>

Callers 2

server.jsFile · 0.70
serveFunction · 0.50

Calls 6

useMethod · 0.80
ssrLoadModuleMethod · 0.80
ssrFixStacktraceMethod · 0.80
logMethod · 0.80
renderFunction · 0.50
setMethod · 0.45

Tested by 1

serveFunction · 0.40