Skip to content

Commit a2af2f0

Browse files
authoredMar 21, 2023
perf: avoid execSync on openBrowser (#12510)
1 parent aecb9b1 commit a2af2f0

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed
 

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import { join } from 'node:path'
12-
import { execSync } from 'node:child_process'
12+
import { exec } from 'node:child_process'
1313
import open from 'open'
1414
import spawn from 'cross-spawn'
1515
import colors from 'picocolors'
@@ -18,25 +18,23 @@ import { VITE_PACKAGE_DIR } from '../constants'
1818

1919
/**
2020
* Reads the BROWSER environment variable and decides what to do with it.
21-
* Returns true if it opened a browser or ran a node.js script, otherwise false.
2221
*/
2322
export function openBrowser(
2423
url: string,
2524
opt: string | true,
2625
logger: Logger,
27-
): boolean {
26+
): void {
2827
// The browser executable to open.
2928
// See https://github.com/sindresorhus/open#app for documentation.
3029
const browser = typeof opt === 'string' ? opt : process.env.BROWSER || ''
3130
if (browser.toLowerCase().endsWith('.js')) {
32-
return executeNodeScript(browser, url, logger)
31+
executeNodeScript(browser, url, logger)
3332
} else if (browser.toLowerCase() !== 'none') {
3433
const browserArgs = process.env.BROWSER_ARGS
3534
? process.env.BROWSER_ARGS.split(' ')
3635
: []
37-
return startBrowserProcess(browser, browserArgs, url)
36+
startBrowserProcess(browser, browserArgs, url)
3837
}
39-
return false
4038
}
4139

4240
function executeNodeScript(scriptPath: string, url: string, logger: Logger) {
@@ -70,7 +68,7 @@ const supportedChromiumBrowsers = [
7068
'Chromium',
7169
]
7270

73-
function startBrowserProcess(
71+
async function startBrowserProcess(
7472
browser: string | undefined,
7573
browserArgs: string[],
7674
url: string,
@@ -88,20 +86,19 @@ function startBrowserProcess(
8886

8987
if (shouldTryOpenChromeWithAppleScript) {
9088
try {
91-
const ps = execSync('ps cax').toString()
89+
const ps = await exec('ps cax').toString()
9290
const openedBrowser =
9391
preferredOSXBrowser && ps.includes(preferredOSXBrowser)
9492
? preferredOSXBrowser
9593
: supportedChromiumBrowsers.find((b) => ps.includes(b))
9694
if (openedBrowser) {
9795
// Try our best to reuse existing tab with AppleScript
98-
execSync(
96+
await exec(
9997
`osascript openChrome.applescript "${encodeURI(
10098
url,
10199
)}" "${openedBrowser}"`,
102100
{
103101
cwd: join(VITE_PACKAGE_DIR, 'bin'),
104-
stdio: 'ignore',
105102
},
106103
)
107104
return true

0 commit comments

Comments
 (0)
Please sign in to comment.