@@ -3,7 +3,7 @@ import sirv from 'sirv'
33import connect from 'connect'
44import compression from 'compression'
55import { Server } from 'http'
6- import { ResolvedConfig , ServerOptions } from '.'
6+ import { resolveConfig , InlineConfig , ResolvedConfig } from '.'
77import { Connect } from 'types/connect'
88import {
99 resolveHttpsConfig ,
@@ -14,6 +14,22 @@ import { openBrowser } from './server/openBrowser'
1414import corsMiddleware from 'cors'
1515import { proxyMiddleware } from './server/middlewares/proxy'
1616import { resolveHostname } from './utils'
17+ import { printHttpServerUrls } from './logger'
18+
19+ export interface PreviewServer {
20+ /**
21+ * The resolved vite config object
22+ */
23+ config : ResolvedConfig
24+ /**
25+ * native Node http server instance
26+ */
27+ httpServer : Server
28+ /**
29+ * Print server urls
30+ */
31+ printUrls : ( ) => void
32+ }
1733
1834/**
1935 * Starts the Vite server in preview mode, to simulate a production deployment
@@ -22,9 +38,10 @@ import { resolveHostname } from './utils'
2238 * @experimental
2339 */
2440export async function preview (
25- config : ResolvedConfig ,
26- serverOptions : Pick < ServerOptions , 'port' | 'host' >
27- ) : Promise < Server > {
41+ inlineConfig : InlineConfig
42+ ) : Promise < PreviewServer > {
43+ const config = await resolveConfig ( inlineConfig , 'serve' , 'production' )
44+
2845 const app = connect ( ) as Connect . Server
2946 const httpServer = await resolveHttpServer (
3047 config . server ,
@@ -56,8 +73,8 @@ export async function preview(
5673 )
5774
5875 const options = config . server
59- const hostname = resolveHostname ( serverOptions . host ?? options . host )
60- const port = serverOptions . port ?? 5000
76+ const hostname = resolveHostname ( options . host )
77+ const port = options . port ?? 5000
6178 const protocol = options . https ? 'https' : 'http'
6279 const logger = config . logger
6380 const base = config . base
@@ -80,5 +97,11 @@ export async function preview(
8097 )
8198 }
8299
83- return httpServer
100+ return {
101+ config,
102+ httpServer,
103+ printUrls ( ) {
104+ printHttpServerUrls ( httpServer , config )
105+ }
106+ }
84107}
0 commit comments