在终端中运行open http://localhost:8080
每次都会打开一个新标签。
如果可用,create-react-app如何重用现有的“浏览器”标签?
看起来here是神奇发生的地方:
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;
}
}
但是,我仍然不知道这是如何运作的。我在OS X上,我确实有osascript
二进制文件。但我不确定如何在终端内使用它。
我已尝试osascript openChrome.applescript "localhost:8080"
,但我收到以下错误:
osascript: openChrome.applescript: No such file or directory
正确使用osascript
命令在当前标签中打开http://localhost:8080
有什么用途?
它看起来像openChrome.applescript文件is somewhere included within create-react-app,但我不知道在哪里。
答案 0 :(得分:1)
我刚刚测试了osascript /path/to/openChrome.applescript "http://localhost:8080"
,它按预期工作。
如果您没有 openChrome.applescript 脚本,那么这里的源代码网址:openChrome.applescript