From 4edb3363119b20f3056c38f1e844554e26cce79f Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 27 Oct 2021 22:22:23 +0200 Subject: [PATCH] refactor!: align preview api (#5407) --- packages/vite/src/node/cli.ts | 37 +++++++++++--------------- packages/vite/src/node/index.ts | 3 +++ packages/vite/src/node/preview.ts | 37 +++++++++++++++++++++----- packages/vite/src/node/server/index.ts | 31 ++++++++++----------- 4 files changed, 65 insertions(+), 43 deletions(-) diff --git a/packages/vite/src/node/cli.ts b/packages/vite/src/node/cli.ts index e322d7b31c2001..15c581d719b0f7 100644 --- a/packages/vite/src/node/cli.ts +++ b/packages/vite/src/node/cli.ts @@ -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' @@ -218,9 +218,9 @@ cli .command('preview [root]') .option('--host [host]', `[string] specify hostname`) .option('--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, @@ -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}`), diff --git a/packages/vite/src/node/index.ts b/packages/vite/src/node/index.ts index 2835c658e013b0..1c0f74e608f7ad 100644 --- a/packages/vite/src/node/index.ts +++ b/packages/vite/src/node/index.ts @@ -25,6 +25,9 @@ export type { LibraryFormats, ResolvedBuildOptions } from './build' +export type { + PreviewServer +} from './preview' export type { DepOptimizationMetadata, DepOptimizationOptions diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index 707c492858ec1c..0f48f1b2541477 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -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, @@ -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 @@ -22,9 +38,10 @@ import { resolveHostname } from './utils' * @experimental */ export async function preview( - config: ResolvedConfig, - serverOptions: Pick -): Promise { + inlineConfig: InlineConfig +): Promise { + const config = await resolveConfig(inlineConfig, 'serve', 'production') + const app = connect() as Connect.Server const httpServer = await resolveHttpServer( config.server, @@ -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 @@ -80,5 +97,11 @@ export async function preview( ) } - return httpServer + return { + config, + httpServer, + printUrls() { + printHttpServerUrls(httpServer, config) + } + } } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 9083c6a101fb31..90f6af4ad00939 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -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. @@ -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. @@ -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 */