MCPcopy
hub / github.com/vitejs/vite / startBrowserProcess

Function startBrowserProcess

packages/vite/src/node/server/openBrowser.ts:72–137  ·  view source on GitHub ↗
(
  browser: string | undefined,
  browserArgs: string[],
  url: string,
  logger: Logger,
)

Source from the content-addressed store, hash-verified

70]
71
72async function startBrowserProcess(
73 browser: string | undefined,
74 browserArgs: string[],
75 url: string,
76 logger: Logger,
77) {
78 // If we're on OS X, the user hasn't specifically
79 // requested a different browser, we can try opening
80 // a Chromium browser with JXA. This lets us reuse an
81 // existing tab when possible instead of creating a new one.
82 const preferredOSXBrowser =
83 browser === 'google chrome' ? 'Google Chrome' : browser
84 const shouldTryOpenChromeWithJXA =
85 process.platform === 'darwin' &&
86 (!preferredOSXBrowser ||
87 supportedChromiumBrowsers.includes(preferredOSXBrowser))
88
89 if (shouldTryOpenChromeWithJXA) {
90 try {
91 const ps = await execAsync('ps cax')
92 const openedBrowser =
93 preferredOSXBrowser && ps.includes(preferredOSXBrowser)
94 ? preferredOSXBrowser
95 : supportedChromiumBrowsers.find((b) => ps.includes(b))
96 if (openedBrowser) {
97 // Try our best to reuse existing tab with JXA
98 await execAsync(`osascript openChrome.js "${url}" "${openedBrowser}"`, {
99 cwd: join(VITE_PACKAGE_DIR, 'bin'),
100 })
101 return true
102 }
103 } catch {
104 // Ignore errors
105 }
106 }
107
108 // Another special case: on OS X, check if BROWSER has been set to "open".
109 // In this case, instead of passing the string `open` to `open` function (which won't work),
110 // just ignore it (thus ensuring the intended behavior, i.e. opening the system browser):
111 // https://github.com/react/create-react-app/pull/1690#issuecomment-283518768
112 if (process.platform === 'darwin' && browser === 'open') {
113 browser = undefined
114 }
115
116 // Fallback to open
117 // (It will always open new tab)
118 try {
119 const options: Options = browser
120 ? { app: { name: browser, arguments: browserArgs } }
121 : {}
122
123 new Promise((_, reject) => {
124 open(url, options)
125 .then((subprocess) => {
126 subprocess.on('error', reject)
127 })
128 .catch(reject)
129 }).catch((err) => {

Callers 1

openBrowserFunction · 0.85

Calls 3

execAsyncFunction · 0.85
onMethod · 0.65
errorMethod · 0.65

Tested by

no test coverage detected