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

Function createServer

playground/ssr/server.js:7–65  ·  view source on GitHub ↗
(
  root = process.cwd(),
  hmrPort,
  customLogger,
)

Source from the content-addressed store, hash-verified

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

Callers 3

server.jsFile · 0.70
serveFunction · 0.50
serveFunction · 0.50

Calls 8

useMethod · 0.80
transformIndexHtmlMethod · 0.80
ssrLoadModuleMethod · 0.80
ssrFixStacktraceMethod · 0.80
logMethod · 0.80
resolveFunction · 0.70
renderFunction · 0.50
setMethod · 0.45

Tested by 2

serveFunction · 0.40
serveFunction · 0.40