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

Function preview

packages/vite/src/node/preview.ts:127–312  ·  view source on GitHub ↗
(
  inlineConfig: InlineConfig = {},
)

Source from the content-addressed store, hash-verified

125 * Starts the Vite server in preview mode, to simulate a production deployment
126 */
127export async function preview(
128 inlineConfig: InlineConfig = {},
129): Promise<PreviewServer> {
130 // The preview server is a long-running, interactive process whose
131 // responses cannot be replayed from a cache.
132 disableCache()
133
134 const config = await resolveConfig(
135 inlineConfig,
136 'serve',
137 'production',
138 'production',
139 true,
140 )
141
142 const clientOutDir = config.environments.client.build.outDir
143 const distDir = path.resolve(config.root, clientOutDir)
144 if (
145 !fs.existsSync(distDir) &&
146 // error if no plugins implement `configurePreviewServer`
147 config.plugins.every((plugin) => !plugin.configurePreviewServer) &&
148 // error if called in CLI only. programmatic usage could access `httpServer`
149 // and affect file serving
150 process.argv[1]?.endsWith(path.normalize('bin/vite.js')) &&
151 process.argv[2] === 'preview'
152 ) {
153 throw new Error(
154 `The directory "${clientOutDir}" does not exist. Did you build your project?`,
155 )
156 }
157
158 const httpsOptions = await resolveHttpsConfig(config.preview.https)
159 const app = connect() as Connect.Server
160 const httpServer = await resolveHttpServer(app, httpsOptions)
161 setClientErrorHandler(httpServer, config.logger)
162
163 const options = config.preview
164 const logger = config.logger
165
166 const closeHttpServer = createServerCloseFn(httpServer)
167
168 // Promise used by `server.close()` to ensure `closeServer()` is only called once
169 let closeServerPromise: Promise<void> | undefined
170 const closeServer = async () => {
171 teardownSIGTERMListener(closeServerAndExit)
172 await closeHttpServer()
173 server.resolvedUrls = null
174 }
175
176 const server: PreviewServer = {
177 config,
178 middlewares: app,
179 httpServer,
180 async close() {
181 if (!closeServerPromise) {
182 closeServerPromise = closeServer()
183 }
184 return closeServerPromise

Callers 5

shortcuts.spec.tsFile · 0.90
startDefaultServeFunction · 0.90
withBuildFunction · 0.90
cli.tsFile · 0.85

Calls 15

resolveConfigFunction · 0.90
resolveHttpsConfigFunction · 0.90
resolveHttpServerFunction · 0.90
setClientErrorHandlerFunction · 0.90
createServerCloseFnFunction · 0.90
setupSIGTERMListenerFunction · 0.90
hostValidationMiddlewareFunction · 0.90
proxyMiddlewareFunction · 0.90
baseMiddlewareFunction · 0.90
htmlFallbackMiddlewareFunction · 0.90
normalizePathFunction · 0.90
indexHtmlMiddlewareFunction · 0.90

Tested by 2

withBuildFunction · 0.72