Skip to content

Commit

Permalink
refactor!: align preview api (#5407)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Oct 27, 2021
1 parent 2136771 commit 4edb336
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 43 deletions.
37 changes: 16 additions & 21 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import chalk from 'chalk'
import { performance } from 'perf_hooks'
import { BuildOptions } from './build'
import { ServerOptions } from './server'
import { createLogger, LogLevel, printHttpServerUrls } from './logger'
import { createLogger, LogLevel } from './logger'
import { resolveConfig } from '.'
import { preview } from './preview'

Expand Down Expand Up @@ -218,9 +218,9 @@ cli
.command('preview [root]')
.option('--host [host]', `[string] specify hostname`)
.option('--port <port>', `[number] specify port`)
.option('--strictPort', `[boolean] exit if specified port is already in use`)
.option('--https', `[boolean] use TLS + HTTP/2`)
.option('--open [path]', `[boolean | string] open browser on startup`)
.option('--strictPort', `[boolean] exit if specified port is already in use`)
.action(
async (
root: string,
Expand All @@ -233,25 +233,20 @@ cli
} & GlobalCLIOptions
) => {
try {
const config = await resolveConfig(
{
root,
base: options.base,
configFile: options.config,
logLevel: options.logLevel,
server: {
host: options.host,
open: options.open,
strictPort: options.strictPort,
https: options.https
}
},
'serve',
'production'
)
const server = await preview(config, cleanOptions(options))

printHttpServerUrls(server, config)
const server = await preview({
root,
base: options.base,
configFile: options.config,
logLevel: options.logLevel,
server: {
host: options.host,
port: options.port ?? 5000,
strictPort: options.strictPort,
https: options.https,
open: options.open
}
})
server.printUrls()
} catch (e) {
createLogger(options.logLevel).error(
chalk.red(`error when starting preview server:\n${e.stack}`),
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export type {
LibraryFormats,
ResolvedBuildOptions
} from './build'
export type {
PreviewServer
} from './preview'
export type {
DepOptimizationMetadata,
DepOptimizationOptions
Expand Down
37 changes: 30 additions & 7 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sirv from 'sirv'
import connect from 'connect'
import compression from 'compression'
import { Server } from 'http'
import { ResolvedConfig, ServerOptions } from '.'
import { resolveConfig, InlineConfig, ResolvedConfig } from '.'
import { Connect } from 'types/connect'
import {
resolveHttpsConfig,
Expand All @@ -14,6 +14,22 @@ import { openBrowser } from './server/openBrowser'
import corsMiddleware from 'cors'
import { proxyMiddleware } from './server/middlewares/proxy'
import { resolveHostname } from './utils'
import { printHttpServerUrls } from './logger'

export interface PreviewServer {
/**
* The resolved vite config object
*/
config: ResolvedConfig
/**
* native Node http server instance
*/
httpServer: Server
/**
* Print server urls
*/
printUrls: () => void
}

/**
* Starts the Vite server in preview mode, to simulate a production deployment
Expand All @@ -22,9 +38,10 @@ import { resolveHostname } from './utils'
* @experimental
*/
export async function preview(
config: ResolvedConfig,
serverOptions: Pick<ServerOptions, 'port' | 'host'>
): Promise<Server> {
inlineConfig: InlineConfig
): Promise<PreviewServer> {
const config = await resolveConfig(inlineConfig, 'serve', 'production')

const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(
config.server,
Expand Down Expand Up @@ -56,8 +73,8 @@ export async function preview(
)

const options = config.server
const hostname = resolveHostname(serverOptions.host ?? options.host)
const port = serverOptions.port ?? 5000
const hostname = resolveHostname(options.host)
const port = options.port ?? 5000
const protocol = options.https ? 'https' : 'http'
const logger = config.logger
const base = config.base
Expand All @@ -80,5 +97,11 @@ export async function preview(
)
}

return httpServer
return {
config,
httpServer,
printUrls() {
printHttpServerUrls(httpServer, config)
}
}
}
31 changes: 16 additions & 15 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export { searchForWorkspaceRoot } from './searchRoot'
export interface ServerOptions {
host?: string | boolean
port?: number
/**
* If enabled, vite will exit if specified port is already in use
*/
strictPort?: boolean
/**
* Enable TLS + HTTP/2.
* Note: this downgrades to TLS only when the proxy option is also used.
Expand All @@ -71,19 +75,6 @@ export interface ServerOptions {
* Open browser window on startup
*/
open?: boolean | string
/**
* Force dep pre-optimization regardless of whether deps have changed.
*/
force?: boolean
/**
* Configure HMR-specific options (port, host, path & protocol)
*/
hmr?: HmrOptions | boolean
/**
* chokidar watch options
* https://github.com/paulmillr/chokidar#api
*/
watch?: WatchOptions
/**
* Configure custom proxy rules for the dev server. Expects an object
* of `{ key: options }` pairs.
Expand Down Expand Up @@ -114,10 +105,20 @@ export interface ServerOptions {
* using an object.
*/
cors?: CorsOptions | boolean

/**
* If enabled, vite will exit if specified port is already in use
* Force dep pre-optimization regardless of whether deps have changed.
*/
strictPort?: boolean
force?: boolean
/**
* Configure HMR-specific options (port, host, path & protocol)
*/
hmr?: HmrOptions | boolean
/**
* chokidar watch options
* https://github.com/paulmillr/chokidar#api
*/
watch?: WatchOptions
/**
* Create Vite dev server to be used as a middleware in an existing server
*/
Expand Down

0 comments on commit 4edb336

Please sign in to comment.