Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(preview): support custom hostname #3506

Merged
merged 1 commit into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,17 @@ cli

cli
.command('preview [root]')
.option('--host [host]', `[string] specify hostname`)
.option('--port <port>', `[number] specify port`)
.option('--open [path]', `[boolean | string] open browser on startup`)
.action(
async (
root: string,
options: { port?: number; open?: boolean | string } & GlobalCLIOptions
options: {
host?: string
port?: number
open?: boolean | string
} & GlobalCLIOptions
) => {
try {
const config = await resolveConfig(
Expand All @@ -207,7 +212,7 @@ cli
'serve',
'development'
)
await preview(config, options.port)
await preview(config, { host: options.host, port: options.port })
} catch (e) {
createLogger(options.logLevel).error(
chalk.red(`error when starting preview server:\n${e.stack}`)
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { resolveHostname } from './utils'

export async function preview(
config: ResolvedConfig,
port = 5000
serverOptions: { host?: string; port?: number }
): Promise<void> {
const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(
Expand Down Expand Up @@ -46,7 +46,8 @@ export async function preview(
)

const options = config.server
const hostname = resolveHostname(options.host)
const hostname = resolveHostname(serverOptions.host ?? options.host)
const port = serverOptions.port ?? 5000
const protocol = options.https ? 'https' : 'http'
const logger = config.logger
const base = config.base
Expand Down