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

perf: Use only one ps exec to find a Chromium browser opened on Mac OS #10588

Merged
merged 2 commits into from Nov 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 36 additions & 35 deletions packages/vite/src/node/server/openBrowser.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -59,45 +56,49 @@ 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 preferredOSXBrowser =
browser === 'google chrome' ? 'Google Chrome' : browser
const shouldTryOpenChromeWithAppleScript =
process.platform === 'darwin' && (browser === '' || browser === OSX_CHROME)
process.platform === 'darwin' &&
(!preferredOSXBrowser ||
supportedChromiumBrowsers.includes(preferredOSXBrowser))

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 =
preferredOSXBrowser && ps.includes(preferredOSXBrowser)
? preferredOSXBrowser
: 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
}
}

Expand Down