Skip to content

Commit f199e90

Browse files
authored
perf: Use only one ps exec to find a Chromium browser opened on Mac OS (#10588)
1 parent c53ffec commit f199e90

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

packages/vite/src/node/server/openBrowser.ts

+36-35
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import colors from 'picocolors'
1616
import type { Logger } from '../logger'
1717
import { VITE_PACKAGE_DIR } from '../constants'
1818

19-
// https://github.com/sindresorhus/open#app
20-
const OSX_CHROME = 'google chrome'
21-
2219
/**
2320
* Reads the BROWSER environment variable and decides what to do with it.
2421
* Returns true if it opened a browser or ran a node.js script, otherwise false.
@@ -59,45 +56,49 @@ function executeNodeScript(scriptPath: string, url: string, logger: Logger) {
5956
return true
6057
}
6158

59+
const supportedChromiumBrowsers = [
60+
'Google Chrome Canary',
61+
'Google Chrome Dev',
62+
'Google Chrome Beta',
63+
'Google Chrome',
64+
'Microsoft Edge',
65+
'Brave Browser',
66+
'Vivaldi',
67+
'Chromium'
68+
]
69+
6270
function startBrowserProcess(browser: string | undefined, url: string) {
6371
// If we're on OS X, the user hasn't specifically
6472
// requested a different browser, we can try opening
65-
// Chrome with AppleScript. This lets us reuse an
73+
// a Chromium browser with AppleScript. This lets us reuse an
6674
// existing tab when possible instead of creating a new one.
75+
const preferredOSXBrowser =
76+
browser === 'google chrome' ? 'Google Chrome' : browser
6777
const shouldTryOpenChromeWithAppleScript =
68-
process.platform === 'darwin' && (browser === '' || browser === OSX_CHROME)
78+
process.platform === 'darwin' &&
79+
(!preferredOSXBrowser ||
80+
supportedChromiumBrowsers.includes(preferredOSXBrowser))
6981

7082
if (shouldTryOpenChromeWithAppleScript) {
71-
// Will use the first open browser found from list
72-
const supportedChromiumBrowsers = [
73-
'Google Chrome Canary',
74-
'Google Chrome Dev',
75-
'Google Chrome Beta',
76-
'Google Chrome',
77-
'Microsoft Edge',
78-
'Brave Browser',
79-
'Vivaldi',
80-
'Chromium'
81-
]
82-
83-
for (const chromiumBrowser of supportedChromiumBrowsers) {
84-
try {
85-
// Try our best to reuse existing tab
86-
// on OS X Google Chrome with AppleScript
87-
execSync(`ps cax | grep "${chromiumBrowser}"`)
88-
execSync(
89-
`osascript openChrome.applescript "${encodeURI(
90-
url
91-
)}" "${chromiumBrowser}"`,
92-
{
93-
cwd: join(VITE_PACKAGE_DIR, 'bin'),
94-
stdio: 'ignore'
95-
}
96-
)
97-
return true
98-
} catch (err) {
99-
// Ignore errors
100-
}
83+
try {
84+
const ps = execSync('ps cax').toString()
85+
const openedBrowser =
86+
preferredOSXBrowser && ps.includes(preferredOSXBrowser)
87+
? preferredOSXBrowser
88+
: supportedChromiumBrowsers.find((b) => ps.includes(b))
89+
// Try our best to reuse existing tab with AppleScript
90+
execSync(
91+
`osascript openChrome.applescript "${encodeURI(
92+
url
93+
)}" "${openedBrowser}"`,
94+
{
95+
cwd: join(VITE_PACKAGE_DIR, 'bin'),
96+
stdio: 'ignore'
97+
}
98+
)
99+
return true
100+
} catch (err) {
101+
// Ignore errors
101102
}
102103
}
103104

0 commit comments

Comments
 (0)