简体   繁体   English

运行`npm run start`后,create-react-app如何重用现有的Browser选项卡?

[英]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选项卡?

Update 更新

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是什么?

Update 更新

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 脚本 ,那么这里有它的源代码URLopenChrome.applescript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 npm 使用 create-react-app 时启动不运行 - npm start not running when using create-react-app 可以用 Nodejs 开头的 Create-React-App npm 吗? - Can you use Create-React-App npm start with nodejs? create-react-app:如何使用特定浏览器“npm start”? - create-react-app: How do I "npm start" with a specific browser? 使用 create-react-app (npm start) 运行项目时如何在 Chrome 上启用地理定位? - How to enable geolocation on Chrome when running a project with create-react-app (npm start)? 如何在create-react-app创建的应用程序中使用jsx文件(不运行“npm run eject”)? - How to use jsx files in an app created by create-react-app (without run “npm run eject”)? 使用 create-react-app 启动 npm 错误 - npm start error with create-react-app 如何离线运行像 create-react-app 这样的 npm 命令 - How to run npm commands like create-react-app offline 我可以在没有 npm 启动和 npx create-react-app 的情况下使用 React 运行 index.html 吗? - Can I run index.html with React without npm start and npx create-react-app? 在反应应用程序中运行 npm 启动后,我的浏览器上没有显示任何内容 - Nothing is Displaying on my browser after running npm start in react app 使用 create-react-app 创建新的 react 项目后,npm start 失败 - npm start fails after creating a new react project using create-react-app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM