forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose unstable_browserLauncher option (facebook#39162)
Summary: Pull Request resolved: facebook#39162 Expose a `unstable_browserLauncher` option to `createDevMiddleware()`. This allows integrators to provide a custom implementation for opening URLs in a web browser, initially a `launchDebuggerAppWindow` method. Customising the browser launcher implementation can be useful in cases where the dev server is running remotely and not on the developer's local machine. Changelog: [Internal] Reviewed By: motiz88 Differential Revision: D48647750 fbshipit-source-id: fddd56724d0e674777198100e0fd57fbbf2896fa
- Loading branch information
1 parent
3c87455
commit 60c5825
Showing
6 changed files
with
119 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
/** | ||
* Represents a launched web browser instance. | ||
*/ | ||
export type LaunchedBrowser = { | ||
kill: () => void | Promise<void>, | ||
... | ||
}; | ||
|
||
/** | ||
* An interface for integrators to provide a custom implementation for | ||
* opening URLs in a web browser. | ||
*/ | ||
export interface BrowserLauncher { | ||
/** | ||
* Attempt to open a debugger frontend URL in a browser app window, | ||
* optionally returning an object to control the launched browser instance. | ||
* The browser used should be capable of running Chrome DevTools. | ||
*/ | ||
launchDebuggerAppWindow: (url: string) => Promise<LaunchedBrowser | void>; | ||
} |
76 changes: 76 additions & 0 deletions
76
packages/dev-middleware/src/utils/DefaultBrowserLauncher.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
import type {BrowserLauncher, LaunchedBrowser} from '../types/BrowserLauncher'; | ||
|
||
import {promises as fs} from 'fs'; | ||
import path from 'path'; | ||
import osTempDir from 'temp-dir'; | ||
|
||
const ChromeLauncher = require('chrome-launcher'); | ||
const {Launcher: EdgeLauncher} = require('chromium-edge-launcher'); | ||
|
||
/** | ||
* Default `BrowserLauncher` implementation which opens URLs on the host | ||
* machine. | ||
*/ | ||
const DefaultBrowserLauncher: BrowserLauncher = { | ||
/** | ||
* Attempt to open the debugger frontend in a Google Chrome or Microsoft Edge | ||
* app window. | ||
*/ | ||
launchDebuggerAppWindow: async (url: string): Promise<LaunchedBrowser> => { | ||
let browserType = 'chrome'; | ||
let chromePath; | ||
|
||
try { | ||
// Locate Chrome installation path, will throw if not found | ||
chromePath = ChromeLauncher.getChromePath(); | ||
} catch (e) { | ||
browserType = 'edge'; | ||
chromePath = EdgeLauncher.getFirstInstallation(); | ||
|
||
if (chromePath == null) { | ||
throw new Error( | ||
'Unable to find a browser on the host to open the debugger. ' + | ||
'Supported browsers: Google Chrome, Microsoft Edge.\n' + | ||
url, | ||
); | ||
} | ||
} | ||
|
||
const userDataDir = await createTempDir( | ||
`react-native-debugger-frontend-${browserType}`, | ||
); | ||
const launchedChrome = await ChromeLauncher.launch({ | ||
chromePath, | ||
chromeFlags: [ | ||
`--app=${url}`, | ||
`--user-data-dir=${userDataDir}`, | ||
'--window-size=1200,600', | ||
], | ||
}); | ||
|
||
return { | ||
kill: async () => launchedChrome.kill(), | ||
}; | ||
}, | ||
}; | ||
|
||
async function createTempDir(dirName: string): Promise<string> { | ||
const tempDir = path.join(osTempDir, dirName); | ||
|
||
await fs.mkdir(tempDir, {recursive: true}); | ||
|
||
return tempDir; | ||
} | ||
|
||
export default DefaultBrowserLauncher; |
70 changes: 0 additions & 70 deletions
70
packages/dev-middleware/src/utils/launchDebuggerAppWindow.js
This file was deleted.
Oops, something went wrong.