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 BROWSER and BROWSER_ARGS in env file #11513

Merged
merged 2 commits into from
Jan 4, 2023
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
4 changes: 3 additions & 1 deletion docs/config/preview-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ The value can also be an [options object](https://nodejs.org/api/https.html#http
- **Type:** `boolean | string`
- **Default:** [`server.open`](./server-options#server-open)

Automatically open the app in the browser on server start. When the value is a string, it will be used as the URL's pathname. If you want to open the server in a specific browser you like, you can set the env `process.env.BROWSER` (e.g. `firefox`). See [the `open` package](https://github.com/sindresorhus/open#app) for more details.
Automatically open the app in the browser on server start. When the value is a string, it will be used as the URL's pathname. If you want to open the server in a specific browser you like, you can set the env `process.env.BROWSER` (e.g. `firefox`). You can also set `process.env.BROWSER_ARGS` to pass additional arguments (e.g. `--incognito`).

`BROWSER` and `BROWSER_ARGS` are also special environment variables you can set in the `.env` file to configure it. See [the `open` package](https://github.com/sindresorhus/open#app) for more details.

## preview.proxy

Expand Down
4 changes: 3 additions & 1 deletion docs/config/server-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ A valid certificate is needed. For a basic setup, you can add [@vitejs/plugin-ba

- **Type:** `boolean | string`

Automatically open the app in the browser on server start. When the value is a string, it will be used as the URL's pathname. If you want to open the server in a specific browser you like, you can set the env `process.env.BROWSER` (e.g. `firefox`). See [the `open` package](https://github.com/sindresorhus/open#app) for more details.
Automatically open the app in the browser on server start. When the value is a string, it will be used as the URL's pathname. If you want to open the server in a specific browser you like, you can set the env `process.env.BROWSER` (e.g. `firefox`). You can also set `process.env.BROWSER_ARGS` to pass additional arguments (e.g. `--incognito`).

`BROWSER` and `BROWSER_ARGS` are also special environment variables you can set in the `.env` file to configure it. See [the `open` package](https://github.com/sindresorhus/open#app) for more details.

**Example:**

Expand Down
7 changes: 7 additions & 0 deletions packages/vite/src/node/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export function loadEnv(
if (parsed.NODE_ENV && process.env.VITE_USER_NODE_ENV === undefined) {
process.env.VITE_USER_NODE_ENV = parsed.NODE_ENV
}
// support BROWSER and BROWSER_ARGS env variables
if (parsed.BROWSER && process.env.BROWSER === undefined) {
process.env.BROWSER = parsed.BROWSER
}
if (parsed.BROWSER_ARGS && process.env.BROWSER_ARGS === undefined) {
process.env.BROWSER_ARGS = parsed.BROWSER_ARGS
}

try {
// let environment variables use each other
Expand Down
15 changes: 12 additions & 3 deletions packages/vite/src/node/server/openBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export function openBrowser(
if (browser.toLowerCase().endsWith('.js')) {
return executeNodeScript(browser, url, logger)
} else if (browser.toLowerCase() !== 'none') {
return startBrowserProcess(browser, url)
const browserArgs = process.env.BROWSER_ARGS
? process.env.BROWSER_ARGS.split(' ')
: []
return startBrowserProcess(browser, browserArgs, url)
}
return false
}
Expand Down Expand Up @@ -67,7 +70,11 @@ const supportedChromiumBrowsers = [
'Chromium',
]

function startBrowserProcess(browser: string | undefined, url: string) {
function startBrowserProcess(
browser: string | undefined,
browserArgs: string[],
url: string,
) {
// If we're on OS X, the user hasn't specifically
// requested a different browser, we can try opening
// a Chromium browser with AppleScript. This lets us reuse an
Expand Down Expand Up @@ -115,7 +122,9 @@ function startBrowserProcess(browser: string | undefined, url: string) {
// Fallback to open
// (It will always open new tab)
try {
const options: open.Options = browser ? { app: { name: browser } } : {}
const options: open.Options = browser
? { app: { name: browser, arguments: browserArgs } }
: {}
open(url, options).catch(() => {}) // Prevent `unhandledRejection` error.
return true
} catch (err) {
Expand Down