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

Function createServer

playground/css-lightningcss-proxy/server.js:16–77  ·  view source on GitHub ↗
(root = process.cwd(), hmrPort)

Source from the content-addressed store, hash-verified

14`
15
16export async function createServer(root = process.cwd(), hmrPort) {
17 const resolve = (p) => path.resolve(import.meta.dirname, p)
18
19 const app = express()
20
21 /**
22 * @type {import('vite').ViteDevServer}
23 */
24 const vite = await (
25 await import('vite')
26 ).createServer({
27 root,
28 logLevel: isTest ? 'error' : 'info',
29 css: {
30 transformer: 'lightningcss',
31 },
32 server: {
33 middlewareMode: true,
34 watch: {
35 // During tests we edit the files too fast and sometimes chokidar
36 // misses change events, so enforce polling for consistency
37 usePolling: true,
38 interval: 100,
39 },
40 hmr: {
41 port: hmrPort,
42 },
43 },
44 appType: 'custom',
45 })
46 // use vite's connect instance as middleware
47 app.use(vite.middlewares)
48
49 app.use('*all', async (req, res, next) => {
50 try {
51 let [url] = req.originalUrl.split('?')
52 if (url.endsWith('/')) url += 'index.html'
53
54 if (url.startsWith('/favicon.ico')) {
55 return res.status(404).end('404')
56 }
57
58 const htmlLoc = resolve(`.${url}`)
59 let template = fs.readFileSync(htmlLoc, 'utf-8')
60
61 template = template.replace('<!--[inline-css]-->', DYNAMIC_STYLES)
62
63 // Force calling transformIndexHtml with url === '/', to simulate
64 // usage by ecosystem that was recommended in the SSR documentation
65 // as `const url = req.originalUrl`
66 const html = await vite.transformIndexHtml('/', template)
67
68 res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
69 } catch (e) {
70 vite && vite.ssrFixStacktrace(e)
71 console.log(e.stack)
72 res.status(500).end(e.stack)
73 }

Callers 2

server.jsFile · 0.70
serveFunction · 0.50

Calls 6

useMethod · 0.80
transformIndexHtmlMethod · 0.80
ssrFixStacktraceMethod · 0.80
logMethod · 0.80
resolveFunction · 0.70
setMethod · 0.45

Tested by 1

serveFunction · 0.40