Commit 37009b1f authored by Dan Abramov's avatar Dan Abramov Committed by GitHub
Browse files

Attempt to reuse Chrome tab on OS X (#62)

Fixes #38
parent b941ad2d
Showing with 77 additions and 5 deletions
+77 -5
...@@ -46,7 +46,8 @@ prompt('Are you sure you want to eject? This action is permanent. [y/N]', functi ...@@ -46,7 +46,8 @@ prompt('Are you sure you want to eject? This action is permanent. [y/N]', functi
path.join('config', 'webpack.config.dev.js'), path.join('config', 'webpack.config.dev.js'),
path.join('config', 'webpack.config.prod.js'), path.join('config', 'webpack.config.prod.js'),
path.join('scripts', 'build.js'), path.join('scripts', 'build.js'),
path.join('scripts', 'start.js') path.join('scripts', 'start.js'),
path.join('scripts', 'openChrome.applescript')
]; ];
// Ensure that the host folder is clean and we won't override any files // Ensure that the host folder is clean and we won't override any files
...@@ -68,9 +69,13 @@ prompt('Are you sure you want to eject? This action is permanent. [y/N]', functi ...@@ -68,9 +69,13 @@ prompt('Are you sure you want to eject? This action is permanent. [y/N]', functi
files.forEach(function(file) { files.forEach(function(file) {
console.log('Copying ' + file + ' to ' + hostPath); console.log('Copying ' + file + ' to ' + hostPath);
var content = fs.readFileSync(path.join(selfPath, file), 'utf8'); content = fs
// Remove license header .readFileSync(path.join(selfPath, file), 'utf8')
content = content.replace(/^\/\*\*(\*(?!\/)|[^*])*\*\//, '').trim() + '\n'; // Remove license header from JS
.replace(/^\/\*\*(\*(?!\/)|[^*])*\*\//, '')
// Remove license header from AppleScript
.replace(/^--.*\n/gm, '')
.trim() + '\n';
fs.writeFileSync(path.join(hostPath, file), content); fs.writeFileSync(path.join(hostPath, file), content);
}); });
console.log(); console.log();
......
-- Copyright (c) 2015-present, Facebook, Inc.
-- All rights reserved.
--
-- This source code is licensed under the BSD-style license found in the
-- LICENSE file in the root directory of this source tree. An additional grant
-- of patent rights can be found in the PATENTS file in the same directory.
on run argv
set theURL to item 1 of argv
tell application "Chrome"
if (count every window) = 0 then
make new window
end if
-- Find a tab currently running the debugger
set found to false
set theTabIndex to -1
repeat with theWindow in every window
set theTabIndex to 0
repeat with theTab in every tab of theWindow
set theTabIndex to theTabIndex + 1
if theTab's URL is theURL then
set found to true
exit repeat
end if
end repeat
if found then
exit repeat
end if
end repeat
if found then
tell theTab to reload
set index of theWindow to 1
set theWindow's active tab index to theTabIndex
else
tell window 1
activate
make new tab with properties {URL:theURL}
end tell
end if
end tell
end run
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
process.env.NODE_ENV = 'development'; process.env.NODE_ENV = 'development';
var path = require('path');
var chalk = require('chalk'); var chalk = require('chalk');
var webpack = require('webpack'); var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server'); var WebpackDevServer = require('webpack-dev-server');
...@@ -117,6 +118,27 @@ compiler.plugin('done', function (stats) { ...@@ -117,6 +118,27 @@ compiler.plugin('done', function (stats) {
} }
}); });
function openBrowser() {
if (process.platform === 'darwin') {
try {
// Try our best to reuse existing tab
// on OS X Google Chrome with AppleScript
execSync('ps cax | grep "Google Chrome"');
execSync(
'osascript ' +
path.resolve(__dirname, './openChrome.applescript') +
' http://localhost:3000/'
);
return;
} catch (err) {
// Ignore errors.
}
}
// Fallback to opn
// (It will always open new tab)
opn('http://localhost:3000/');
}
new WebpackDevServer(compiler, { new WebpackDevServer(compiler, {
historyApiFallback: true, historyApiFallback: true,
hot: true, // Note: only CSS is currently hot reloaded hot: true, // Note: only CSS is currently hot reloaded
...@@ -128,5 +150,5 @@ new WebpackDevServer(compiler, { ...@@ -128,5 +150,5 @@ new WebpackDevServer(compiler, {
} }
console.log('Starting the development server...'); console.log('Starting the development server...');
opn('http://localhost:3000/'); openBrowser();
}); });
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment