[英]How does create-react-app re-use an existing Browser tab after running `npm run start`?
Running open http://localhost:8080
in Terminal will open a new tab each time. 在终端中运行
open http://localhost:8080
将每次打开一个新选项卡。
How does create-react-app re-use an existing Browser tab, when available? create-react-app如何在可用时重用现有的Browser选项卡?
It looks like here is the place where the magic happens: 看起来这里是神奇发生的地方:
function startBrowserProcess(browser, url) {
// If we're on OS X, the user hasn't specifically
// requested a different browser, we can try opening
// Chrome with AppleScript. This lets us reuse an
// existing tab when possible instead of creating a new one.
const shouldTryOpenChromeWithAppleScript =
process.platform === 'darwin' &&
(typeof browser !== 'string' || 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: __dirname,
stdio: 'ignore',
});
return true;
} catch (err) {
// Ignore errors.
}
}
// Another special case: on OS X, check if BROWSER has been set to "open".
// In this case, instead of passing `open` to `opn` (which won't work),
// just ignore it (thus ensuring the intended behavior, i.e. opening the system browser):
// https://github.com/facebook/create-react-app/pull/1690#issuecomment-283518768
if (process.platform === 'darwin' && browser === 'open') {
browser = undefined;
}
// Fallback to opn
// (It will always open new tab)
try {
var options = { app: browser };
opn(url, options).catch(() => {}); // Prevent `unhandledRejection` error.
return true;
} catch (err) {
return false;
}
}
However, I still don't know how this is working. 但是,我仍然不知道这是如何工作的。 I am on OS X, and I do have the
osascript
binary, surprisingly. 我在OS X上,我确实有
osascript
二进制文件。 But I'm not sure how to use it within Terminal. 但我不确定如何在终端内使用它。
I've tried osascript openChrome.applescript "localhost:8080"
, but I'm getting the following error: 我已经尝试过
osascript openChrome.applescript "localhost:8080"
,但是我收到以下错误:
osascript: openChrome.applescript: No such file or directory
What is the proper use of the osascript
command to open http://localhost:8080
in the current tab, if it exists? 正确使用
osascript
命令在当前选项卡中打开http://localhost:8080
是什么?
It looks like the openChrome.applescript file is somewhere included within create-react-app , but I'm not sure where. 看起来openChrome.applescript文件包含在create-react-app中 ,但我不知道在哪里。
I just tested osascript /path/to/openChrome.applescript "http://localhost:8080"
and it works as expected. 我刚刚测试了
osascript /path/to/openChrome.applescript "http://localhost:8080"
,它按预期工作。
If you don't have the openChrome.applescript script , then here its source code URL : openChrome.applescript 如果你没有openChrome.applescript 脚本 ,那么这里有它的源代码URL : openChrome.applescript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.