Home / File/ server.js — vite Source File

server.js — vite Source File

Architecture documentation for server.js, a javascript file in the vite codebase. 3 imports, 0 dependents.

File javascript ViteCore DevServer 3 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  58806f63_238c_34cc_f316_a3485466a67f["server.js"]
  51e96894_3556_ed5c_1ede_97d449867adf["node:path"]
  58806f63_238c_34cc_f316_a3485466a67f --> 51e96894_3556_ed5c_1ede_97d449867adf
  4f31ff6a_267a_f135_5f71_b3abd33efea1["pug"]
  58806f63_238c_34cc_f316_a3485466a67f --> 4f31ff6a_267a_f135_5f71_b3abd33efea1
  c7183a98_0eee_01f4_23b4_5554e5d0e216["express"]
  58806f63_238c_34cc_f316_a3485466a67f --> c7183a98_0eee_01f4_23b4_5554e5d0e216
  style 58806f63_238c_34cc_f316_a3485466a67f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

// @ts-check
import path from 'node:path'
import pug from 'pug'
import express from 'express'

const isTest = process.env.VITEST

const DYNAMIC_SCRIPTS = `
  <script type="module">
    const p = document.createElement('p');
    p.innerHTML = '✅ Dynamically injected inline script';
    document.body.appendChild(p);
  </script>
  <script type="module" src="/src/app.js"></script>
`

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',
    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) => {
    try {
      let [url] = req.originalUrl.split('?')
      url = url.replace(/\.html$/, '.pug')
      if (url.endsWith('/')) url += 'index.pug'

      const htmlLoc = resolve(`.${url}`)
      let html = pug.renderFile(htmlLoc)
      html = html.replace('</body>', `${DYNAMIC_SCRIPTS}</body>`)
      html = await vite.transformIndexHtml(url, html)

      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 }
}

if (!isTest) {
  createServer().then(({ app }) =>
    app.listen(5173, () => {
      console.log('http://localhost:5173')
    }),
  )
}

Domain

Subdomains

Functions

Dependencies

  • express
  • node:path
  • pug

Frequently Asked Questions

What does server.js do?
server.js is a source file in the vite codebase, written in javascript. It belongs to the ViteCore domain, DevServer subdomain.
What functions are defined in server.js?
server.js defines 1 function(s): createServer.
What does server.js depend on?
server.js imports 3 module(s): express, node:path, pug.
Where is server.js in the architecture?
server.js is located at playground/ssr-pug/server.js (domain: ViteCore, subdomain: DevServer, directory: playground/ssr-pug).

Analyze Your Own Codebase

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

Try Supermodel Free