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

feat: support open browser in preview #1968

Merged
merged 1 commit into from
Feb 11, 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
13 changes: 10 additions & 3 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ cli
.option('--host <host>', `[string] specify hostname`)
.option('--port <port>', `[number] specify port`)
.option('--https', `[boolean] use TLS + HTTP/2`)
.option('--open [browser]', `[boolean | string] open browser on startup`)
.option('--open [path]', `[boolean | string] open browser on startup`)
.option('--cors', `[boolean] enable CORS`)
.option('--strictPort', `[boolean] exit if specified port is already in use`)
.option('-m, --mode <mode>', `[string] set env mode`)
Expand Down Expand Up @@ -186,15 +186,22 @@ cli
cli
.command('preview [root]')
.option('--port <port>', `[number] specify port`)
.option('--open [path]', `[boolean | string] open browser on startup`)
.action(
async (root: string, options: { port?: number } & GlobalCLIOptions) => {
async (
root: string,
options: { port?: number; open?: boolean | string } & GlobalCLIOptions
) => {
try {
const config = await resolveConfig(
{
root,
base: options.base,
configFile: options.config,
logLevel: options.logLevel
logLevel: options.logLevel,
server: {
open: options.open
}
},
'serve',
'development'
Expand Down
12 changes: 9 additions & 3 deletions packages/vite/src/node/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import compression from 'compression'
import { ResolvedConfig } from '.'
import { Connect } from 'types/connect'
import { resolveHttpServer } from './server/http'
import { openBrowser } from './server/openBrowser'

export async function serve(config: ResolvedConfig, port = 5000) {
const app = connect() as Connect.Server
Expand All @@ -27,11 +28,11 @@ export async function serve(config: ResolvedConfig, port = 5000) {
const options = config.server || {}
const hostname = options.host || 'localhost'
const protocol = options.https ? 'https' : 'http'
const info = config.logger.info
const logger = config.logger
const base = config.base

server.listen(port, () => {
info(`\n Build preview server running at:\n`)
logger.info(`\n Build preview server running at:\n`)
const interfaces = os.networkInterfaces()
Object.keys(interfaces).forEach((key) =>
(interfaces[key] || [])
Expand All @@ -46,8 +47,13 @@ export async function serve(config: ResolvedConfig, port = 5000) {
})
.forEach(({ type, host }) => {
const url = `${protocol}://${host}:${chalk.bold(port)}${base}`
info(` > ${type} ${chalk.cyan(url)}`)
logger.info(` > ${type} ${chalk.cyan(url)}`)
})
)

if (options.open) {
const path = typeof options.open === 'string' ? options.open : base
openBrowser(`${protocol}://${hostname}:${port}${path}`, true, logger)
}
})
}