From c42b1f375f4d85447fc01b8c7b66c135eb29692e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Fri, 21 Oct 2022 23:45:00 +0200 Subject: [PATCH 1/2] perf: Use only one ps exec to find a Chromium browser opened on Max OS --- packages/vite/src/node/server/openBrowser.ts | 68 ++++++++++---------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/packages/vite/src/node/server/openBrowser.ts b/packages/vite/src/node/server/openBrowser.ts index 396bde3117d069..c9bc7b8cb7a1b3 100644 --- a/packages/vite/src/node/server/openBrowser.ts +++ b/packages/vite/src/node/server/openBrowser.ts @@ -16,9 +16,6 @@ import colors from 'picocolors' import type { Logger } from '../logger' import { VITE_PACKAGE_DIR } from '../constants' -// https://github.com/sindresorhus/open#app -const OSX_CHROME = 'google chrome' - /** * Reads the BROWSER environment variable and decides what to do with it. * Returns true if it opened a browser or ran a node.js script, otherwise false. @@ -59,45 +56,46 @@ function executeNodeScript(scriptPath: string, url: string, logger: Logger) { return true } +const supportedChromiumBrowsers = [ + 'Google Chrome Canary', + 'Google Chrome Dev', + 'Google Chrome Beta', + 'Google Chrome', + 'Microsoft Edge', + 'Brave Browser', + 'Vivaldi', + 'Chromium' +] + function startBrowserProcess(browser: string | undefined, url: string) { // If we're on OS X, the user hasn't specifically // requested a different browser, we can try opening - // Chrome with AppleScript. This lets us reuse an + // a Chromium browser with AppleScript. This lets us reuse an // existing tab when possible instead of creating a new one. const shouldTryOpenChromeWithAppleScript = - process.platform === 'darwin' && (browser === '' || browser === OSX_CHROME) + process.platform === 'darwin' && + (!browser || supportedChromiumBrowsers.includes(browser)) if (shouldTryOpenChromeWithAppleScript) { - // Will use the first open browser found from list - const supportedChromiumBrowsers = [ - 'Google Chrome Canary', - 'Google Chrome Dev', - 'Google Chrome Beta', - 'Google Chrome', - 'Microsoft Edge', - 'Brave Browser', - 'Vivaldi', - 'Chromium' - ] - - for (const chromiumBrowser of supportedChromiumBrowsers) { - try { - // Try our best to reuse existing tab - // on OS X Google Chrome with AppleScript - execSync(`ps cax | grep "${chromiumBrowser}"`) - execSync( - `osascript openChrome.applescript "${encodeURI( - url - )}" "${chromiumBrowser}"`, - { - cwd: join(VITE_PACKAGE_DIR, 'bin'), - stdio: 'ignore' - } - ) - return true - } catch (err) { - // Ignore errors - } + try { + const ps = execSync('ps cax').toString() + const openedBrowser = + browser && ps.includes(browser) + ? browser + : supportedChromiumBrowsers.find((b) => ps.includes(b)) + // Try our best to reuse existing tab with AppleScript + execSync( + `osascript openChrome.applescript "${encodeURI( + url + )}" "${openedBrowser}"`, + { + cwd: join(VITE_PACKAGE_DIR, 'bin'), + stdio: 'ignore' + } + ) + return true + } catch (err) { + // Ignore errors } } From 2beef1b0711680a24e17578876644d3feede23f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Wed, 2 Nov 2022 10:38:32 +0100 Subject: [PATCH 2/2] fix: backward compatibility for 'google chrome' --- packages/vite/src/node/server/openBrowser.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/server/openBrowser.ts b/packages/vite/src/node/server/openBrowser.ts index c9bc7b8cb7a1b3..72fe339937a624 100644 --- a/packages/vite/src/node/server/openBrowser.ts +++ b/packages/vite/src/node/server/openBrowser.ts @@ -72,16 +72,19 @@ function startBrowserProcess(browser: string | undefined, url: string) { // requested a different browser, we can try opening // a Chromium browser with AppleScript. This lets us reuse an // existing tab when possible instead of creating a new one. + const preferredOSXBrowser = + browser === 'google chrome' ? 'Google Chrome' : browser const shouldTryOpenChromeWithAppleScript = process.platform === 'darwin' && - (!browser || supportedChromiumBrowsers.includes(browser)) + (!preferredOSXBrowser || + supportedChromiumBrowsers.includes(preferredOSXBrowser)) if (shouldTryOpenChromeWithAppleScript) { try { const ps = execSync('ps cax').toString() const openedBrowser = - browser && ps.includes(browser) - ? browser + preferredOSXBrowser && ps.includes(preferredOSXBrowser) + ? preferredOSXBrowser : supportedChromiumBrowsers.find((b) => ps.includes(b)) // Try our best to reuse existing tab with AppleScript execSync(