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

feat: reuse opening tab in chromium browsers when start dev server #10485

Merged
merged 1 commit into from Oct 18, 2022
Merged
Show file tree
Hide file tree
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
55 changes: 32 additions & 23 deletions packages/vite/bin/openChrome.applescript
Expand Up @@ -9,12 +9,19 @@ https://github.com/facebookincubator/create-react-app/blob/master/LICENSE
property targetTab: null
property targetTabIndex: -1
property targetWindow: null
property theProgram: "Google Chrome"

on run argv
set theURL to item 1 of argv

with timeout of 2 seconds
tell application "Chrome"
-- Allow requested program to be optional,
-- default to Google Chrome
if (count of argv) > 1 then
set theProgram to item 2 of argv
end if

using terms from application "Google Chrome"
tell application theProgram

if (count every window) = 0 then
make new window
Expand Down Expand Up @@ -51,36 +58,38 @@ on run argv
make new tab with properties {URL:theURL}
end tell
end tell
end timeout
end using terms from
end run

-- Function:
-- Lookup tab with given url
-- if found, store tab, index, and window in properties
-- (properties were declared on top of file)
on lookupTabWithUrl(lookupUrl)
tell application "Chrome"
-- Find a tab with the given url
set found to false
set theTabIndex to -1
repeat with theWindow in every window
set theTabIndex to 0
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
if (theTab's URL as string) contains lookupUrl then
-- assign tab, tab index, and window to properties
set targetTab to theTab
set targetTabIndex to theTabIndex
set targetWindow to theWindow
set found to true
using terms from application "Google Chrome"
tell application theProgram
-- Find a tab with the given url
set found to false
set theTabIndex to -1
repeat with theWindow in every window
set theTabIndex to 0
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
if (theTab's URL as string) contains lookupUrl then
-- assign tab, tab index, and window to properties
set targetTab to theTab
set targetTabIndex to theTabIndex
set targetWindow to theWindow
set found to true
exit repeat
end if
end repeat

if found then
exit repeat
end if
end repeat

if found then
exit repeat
end if
end repeat
end tell
end tell
end using terms from
return found
end lookupTabWithUrl
41 changes: 30 additions & 11 deletions packages/vite/src/node/server/openBrowser.ts
Expand Up @@ -68,17 +68,36 @@ function startBrowserProcess(browser: string | undefined, url: string) {
process.platform === 'darwin' && (browser === '' || browser === OSX_CHROME)

if (shouldTryOpenChromeWithAppleScript) {
try {
// Try our best to reuse existing tab
// on OS X Google Chrome with AppleScript
execSync('ps cax | grep "Google Chrome"')
execSync('osascript openChrome.applescript "' + encodeURI(url) + '"', {
cwd: join(VITE_PACKAGE_DIR, 'bin'),
stdio: 'ignore'
})
return true
} catch (err) {
// Ignore errors
// 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'
bluwy marked this conversation as resolved.
Show resolved Hide resolved
]

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
}
}
}

Expand Down