From a2af2f03b65b116989253dd11b16089a60cb6cbd Mon Sep 17 00:00:00 2001 From: patak Date: Tue, 21 Mar 2023 15:32:52 +0100 Subject: [PATCH] perf: avoid execSync on openBrowser (#12510) --- packages/vite/src/node/server/openBrowser.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/server/openBrowser.ts b/packages/vite/src/node/server/openBrowser.ts index 31d27dc4172180..73332f2a04be2b 100644 --- a/packages/vite/src/node/server/openBrowser.ts +++ b/packages/vite/src/node/server/openBrowser.ts @@ -9,7 +9,7 @@ */ import { join } from 'node:path' -import { execSync } from 'node:child_process' +import { exec } from 'node:child_process' import open from 'open' import spawn from 'cross-spawn' import colors from 'picocolors' @@ -18,25 +18,23 @@ import { VITE_PACKAGE_DIR } from '../constants' /** * 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. */ export function openBrowser( url: string, opt: string | true, logger: Logger, -): boolean { +): void { // The browser executable to open. // See https://github.com/sindresorhus/open#app for documentation. const browser = typeof opt === 'string' ? opt : process.env.BROWSER || '' if (browser.toLowerCase().endsWith('.js')) { - return executeNodeScript(browser, url, logger) + executeNodeScript(browser, url, logger) } else if (browser.toLowerCase() !== 'none') { const browserArgs = process.env.BROWSER_ARGS ? process.env.BROWSER_ARGS.split(' ') : [] - return startBrowserProcess(browser, browserArgs, url) + startBrowserProcess(browser, browserArgs, url) } - return false } function executeNodeScript(scriptPath: string, url: string, logger: Logger) { @@ -70,7 +68,7 @@ const supportedChromiumBrowsers = [ 'Chromium', ] -function startBrowserProcess( +async function startBrowserProcess( browser: string | undefined, browserArgs: string[], url: string, @@ -88,20 +86,19 @@ function startBrowserProcess( if (shouldTryOpenChromeWithAppleScript) { try { - const ps = execSync('ps cax').toString() + const ps = await exec('ps cax').toString() const openedBrowser = preferredOSXBrowser && ps.includes(preferredOSXBrowser) ? preferredOSXBrowser : supportedChromiumBrowsers.find((b) => ps.includes(b)) if (openedBrowser) { // Try our best to reuse existing tab with AppleScript - execSync( + await exec( `osascript openChrome.applescript "${encodeURI( url, )}" "${openedBrowser}"`, { cwd: join(VITE_PACKAGE_DIR, 'bin'), - stdio: 'ignore', }, ) return true