Skip to content

Commit 4edb336

Browse files
authored
refactor!: align preview api (#5407)
1 parent 2136771 commit 4edb336

File tree

4 files changed

+65
-43
lines changed

4 files changed

+65
-43
lines changed

packages/vite/src/node/cli.ts

+16-21
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import chalk from 'chalk'
33
import { performance } from 'perf_hooks'
44
import { BuildOptions } from './build'
55
import { ServerOptions } from './server'
6-
import { createLogger, LogLevel, printHttpServerUrls } from './logger'
6+
import { createLogger, LogLevel } from './logger'
77
import { resolveConfig } from '.'
88
import { preview } from './preview'
99

@@ -218,9 +218,9 @@ cli
218218
.command('preview [root]')
219219
.option('--host [host]', `[string] specify hostname`)
220220
.option('--port <port>', `[number] specify port`)
221+
.option('--strictPort', `[boolean] exit if specified port is already in use`)
221222
.option('--https', `[boolean] use TLS + HTTP/2`)
222223
.option('--open [path]', `[boolean | string] open browser on startup`)
223-
.option('--strictPort', `[boolean] exit if specified port is already in use`)
224224
.action(
225225
async (
226226
root: string,
@@ -233,25 +233,20 @@ cli
233233
} & GlobalCLIOptions
234234
) => {
235235
try {
236-
const config = await resolveConfig(
237-
{
238-
root,
239-
base: options.base,
240-
configFile: options.config,
241-
logLevel: options.logLevel,
242-
server: {
243-
host: options.host,
244-
open: options.open,
245-
strictPort: options.strictPort,
246-
https: options.https
247-
}
248-
},
249-
'serve',
250-
'production'
251-
)
252-
const server = await preview(config, cleanOptions(options))
253-
254-
printHttpServerUrls(server, config)
236+
const server = await preview({
237+
root,
238+
base: options.base,
239+
configFile: options.config,
240+
logLevel: options.logLevel,
241+
server: {
242+
host: options.host,
243+
port: options.port ?? 5000,
244+
strictPort: options.strictPort,
245+
https: options.https,
246+
open: options.open
247+
}
248+
})
249+
server.printUrls()
255250
} catch (e) {
256251
createLogger(options.logLevel).error(
257252
chalk.red(`error when starting preview server:\n${e.stack}`),

packages/vite/src/node/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export type {
2525
LibraryFormats,
2626
ResolvedBuildOptions
2727
} from './build'
28+
export type {
29+
PreviewServer
30+
} from './preview'
2831
export type {
2932
DepOptimizationMetadata,
3033
DepOptimizationOptions

packages/vite/src/node/preview.ts

+30-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sirv from 'sirv'
33
import connect from 'connect'
44
import compression from 'compression'
55
import { Server } from 'http'
6-
import { ResolvedConfig, ServerOptions } from '.'
6+
import { resolveConfig, InlineConfig, ResolvedConfig } from '.'
77
import { Connect } from 'types/connect'
88
import {
99
resolveHttpsConfig,
@@ -14,6 +14,22 @@ import { openBrowser } from './server/openBrowser'
1414
import corsMiddleware from 'cors'
1515
import { proxyMiddleware } from './server/middlewares/proxy'
1616
import { 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
*/
2440
export 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
}

packages/vite/src/node/server/index.ts

+16-15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ export { searchForWorkspaceRoot } from './searchRoot'
6262
export interface ServerOptions {
6363
host?: string | boolean
6464
port?: number
65+
/**
66+
* If enabled, vite will exit if specified port is already in use
67+
*/
68+
strictPort?: boolean
6569
/**
6670
* Enable TLS + HTTP/2.
6771
* Note: this downgrades to TLS only when the proxy option is also used.
@@ -71,19 +75,6 @@ export interface ServerOptions {
7175
* Open browser window on startup
7276
*/
7377
open?: boolean | string
74-
/**
75-
* Force dep pre-optimization regardless of whether deps have changed.
76-
*/
77-
force?: boolean
78-
/**
79-
* Configure HMR-specific options (port, host, path & protocol)
80-
*/
81-
hmr?: HmrOptions | boolean
82-
/**
83-
* chokidar watch options
84-
* https://github.com/paulmillr/chokidar#api
85-
*/
86-
watch?: WatchOptions
8778
/**
8879
* Configure custom proxy rules for the dev server. Expects an object
8980
* of `{ key: options }` pairs.
@@ -114,10 +105,20 @@ export interface ServerOptions {
114105
* using an object.
115106
*/
116107
cors?: CorsOptions | boolean
108+
117109
/**
118-
* If enabled, vite will exit if specified port is already in use
110+
* Force dep pre-optimization regardless of whether deps have changed.
119111
*/
120-
strictPort?: boolean
112+
force?: boolean
113+
/**
114+
* Configure HMR-specific options (port, host, path & protocol)
115+
*/
116+
hmr?: HmrOptions | boolean
117+
/**
118+
* chokidar watch options
119+
* https://github.com/paulmillr/chokidar#api
120+
*/
121+
watch?: WatchOptions
121122
/**
122123
* Create Vite dev server to be used as a middleware in an existing server
123124
*/

0 commit comments

Comments
 (0)