From 33cbf3311848d431663d1fe7cc7e395d3b8c3c45 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 29 Dec 2022 01:10:30 +0800 Subject: [PATCH 1/2] feat: support BROWSER and BROWSER_ARGS in env file --- packages/vite/src/node/env.ts | 7 +++++++ packages/vite/src/node/server/openBrowser.ts | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/env.ts b/packages/vite/src/node/env.ts index f7db1f6bc5be42..95589b9b1e55b1 100644 --- a/packages/vite/src/node/env.ts +++ b/packages/vite/src/node/env.ts @@ -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 diff --git a/packages/vite/src/node/server/openBrowser.ts b/packages/vite/src/node/server/openBrowser.ts index 915328ce9efb83..31d27dc4172180 100644 --- a/packages/vite/src/node/server/openBrowser.ts +++ b/packages/vite/src/node/server/openBrowser.ts @@ -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 } @@ -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 @@ -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) { From f1f37c95fc9e084b03d5142a70e700c80bf48125 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 29 Dec 2022 01:15:51 +0800 Subject: [PATCH 2/2] docs: update docs --- docs/config/preview-options.md | 4 +++- docs/config/server-options.md | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/config/preview-options.md b/docs/config/preview-options.md index 2b4b9841b46bda..4b76acadf0ef9c 100644 --- a/docs/config/preview-options.md +++ b/docs/config/preview-options.md @@ -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 diff --git a/docs/config/server-options.md b/docs/config/server-options.md index ebcbcbe95a3521..0f08375c1013d0 100644 --- a/docs/config/server-options.md +++ b/docs/config/server-options.md @@ -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:**