Home / Function/ createServer() — vite Function Reference

createServer() — vite Function Reference

Architecture documentation for the createServer() function in server.js from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  ce6ce46b_deb3_3645_c764_1462326da529["createServer()"]
  bd933f60_31aa_171f_7e7d_0e150050bcee["server.js"]
  ce6ce46b_deb3_3645_c764_1462326da529 -->|defined in| bd933f60_31aa_171f_7e7d_0e150050bcee
  style ce6ce46b_deb3_3645_c764_1462326da529 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

playground/css-lightningcss-proxy/server.js lines 16–77

export async function createServer(root = process.cwd(), hmrPort) {
  const resolve = (p) => path.resolve(import.meta.dirname, p)

  const app = express()

  /**
   * @type {import('vite').ViteDevServer}
   */
  const vite = await (
    await import('vite')
  ).createServer({
    root,
    logLevel: isTest ? 'error' : 'info',
    css: {
      transformer: 'lightningcss',
    },
    server: {
      middlewareMode: true,
      watch: {
        // During tests we edit the files too fast and sometimes chokidar
        // misses change events, so enforce polling for consistency
        usePolling: true,
        interval: 100,
      },
      hmr: {
        port: hmrPort,
      },
    },
    appType: 'custom',
  })
  // use vite's connect instance as middleware
  app.use(vite.middlewares)

  app.use('*all', async (req, res, next) => {
    try {
      let [url] = req.originalUrl.split('?')
      if (url.endsWith('/')) url += 'index.html'

      if (url.startsWith('/favicon.ico')) {
        return res.status(404).end('404')
      }

      const htmlLoc = resolve(`.${url}`)
      let template = fs.readFileSync(htmlLoc, 'utf-8')

      template = template.replace('<!--[inline-css]-->', DYNAMIC_STYLES)

      // Force calling transformIndexHtml with url === '/', to simulate
      // usage by ecosystem that was recommended in the SSR documentation
      // as `const url = req.originalUrl`
      const html = await vite.transformIndexHtml('/', template)

      res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
    } catch (e) {
      vite && vite.ssrFixStacktrace(e)
      console.log(e.stack)
      res.status(500).end(e.stack)
    }
  })

  return { app, vite }
}

Domain

Subdomains

Frequently Asked Questions

What does createServer() do?
createServer() is a function in the vite codebase, defined in playground/css-lightningcss-proxy/server.js.
Where is createServer() defined?
createServer() is defined in playground/css-lightningcss-proxy/server.js at line 16.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free