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

Function createServer

playground/ssr-conditions/server.js:8–77  ·  view source on GitHub ↗
(
  root = process.cwd(),
  isProd = process.env.NODE_ENV === 'production',
  hmrPort,
)

Source from the content-addressed store, hash-verified

6const isTest = process.env.VITEST
7
8export async function createServer(
9 root = process.cwd(),
10 isProd = process.env.NODE_ENV === 'production',
11 hmrPort,
12) {
13 const resolve = (p) => path.resolve(import.meta.dirname, p)
14
15 const indexProd = isProd
16 ? fs.readFileSync(resolve('dist/client/index.html'), 'utf-8')
17 : ''
18
19 const app = express()
20
21 /**
22 * @type {import('vite').ViteDevServer}
23 */
24 let vite
25 if (!isProd) {
26 vite = await (
27 await import('vite')
28 ).createServer({
29 root,
30 logLevel: isTest ? 'error' : 'info',
31 server: {
32 middlewareMode: true,
33 watch: {
34 // During tests we edit the files too fast and sometimes chokidar
35 // misses change events, so enforce polling for consistency
36 usePolling: true,
37 interval: 100,
38 },
39 hmr: {
40 port: hmrPort,
41 },
42 },
43 appType: 'custom',
44 })
45 app.use(vite.middlewares)
46 } else {
47 app.use(sirv(resolve('dist/client'), { extensions: [] }))
48 }
49
50 app.use('*all', async (req, res) => {
51 try {
52 const url = req.originalUrl
53
54 let template, render
55 if (!isProd) {
56 template = fs.readFileSync(resolve('index.html'), 'utf-8')
57 template = await vite.transformIndexHtml(url, template)
58 render = (await vite.ssrLoadModule('/src/app.js')).render
59 } else {
60 template = indexProd
61 render = (await import('./dist/server/app.js')).render
62 }
63
64 const appHtml = await render(url, import.meta.dirname)
65

Callers 2

server.jsFile · 0.70
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 1

serveFunction · 0.40