在终端中运行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,但是我不确定在哪里。
最佳答案
我刚刚测试了osascript /path/to/openChrome.applescript "http://localhost:8080"
,它可以按预期工作。
如果您没有 openChrome.applescript 脚本,请在此处获取其源代码URL:openChrome.applescript