Last active
August 20, 2018 03:18
-
-
Save iddan/a305251b3cdbeae2b1793e0ee20cad49 to your computer and use it in GitHub Desktop.
Connect Electron to Create React App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create-react-app $NAME | |
npm install --save electron | |
npm run eject | |
replace scripts/start.js with this start.js | |
replace config/webpack.config.dev.js with this webpack.config.dev.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
process.env.NODE_ENV = 'development'; | |
// Load environment variables from .env file. Suppress warnings using silent | |
// if this file is missing. dotenv will never modify any environment variables | |
// that have already been set. | |
// https://github.com/motdotla/dotenv | |
require('dotenv').config({silent: true}); | |
var chalk = require('chalk'); | |
var webpack = require('webpack'); | |
var WebpackDevServer = require('webpack-dev-server'); | |
var historyApiFallback = require('connect-history-api-fallback'); | |
var httpProxyMiddleware = require('http-proxy-middleware'); | |
var detect = require('detect-port'); | |
var clearConsole = require('react-dev-utils/clearConsole'); | |
var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); | |
var formatWebpackMessages = require('react-dev-utils/formatWebpackMessages'); | |
var getProcessForPort = require('react-dev-utils/getProcessForPort'); | |
var openBrowser = require('react-dev-utils/openBrowser'); | |
var prompt = require('react-dev-utils/prompt'); | |
var fs = require('fs'); | |
var config = require('../config/webpack.config.dev'); | |
var paths = require('../config/paths'); | |
var spawn = require('child_process').spawn; | |
var electron = require('electron'); | |
var useYarn = fs.existsSync(paths.yarnLockFile); | |
var cli = useYarn ? 'yarn' : 'npm'; | |
var isInteractive = process.stdout.isTTY; | |
// Warn and crash if required files are missing | |
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { | |
process.exit(1); | |
} | |
// Tools like Cloud9 rely on this. | |
var DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000; | |
var compiler; | |
var handleCompile; | |
// You can safely remove this after ejecting. | |
// We only use this block for testing of Create React App itself: | |
var isSmokeTest = process.argv.some(arg => arg.indexOf('--smoke-test') > -1); | |
if (isSmokeTest) { | |
handleCompile = function (err, stats) { | |
if (err || stats.hasErrors() || stats.hasWarnings()) { | |
process.exit(1); | |
} else { | |
process.exit(0); | |
} | |
}; | |
} | |
var hasStartedElectron = false; | |
function setupCompiler(host, port, protocol) { | |
// "Compiler" is a low-level interface to Webpack. | |
// It lets us listen to some events and provide our own custom messages. | |
compiler = webpack(config, handleCompile); | |
// "invalid" event fires when you have changed a file, and Webpack is | |
// recompiling a bundle. WebpackDevServer takes care to pause serving the | |
// bundle, so if you refresh, it'll wait instead of serving the old one. | |
// "invalid" is short for "bundle invalidated", it doesn't imply any errors. | |
compiler.plugin('invalid', function() { | |
if (isInteractive) { | |
clearConsole(); | |
} | |
console.log('Compiling...'); | |
}); | |
var isFirstCompile = true; | |
// "done" event fires when Webpack has finished recompiling the bundle. | |
// Whether or not you have warnings or errors, you will get this event. | |
compiler.plugin('done', function(stats) { | |
if (isInteractive) { | |
clearConsole(); | |
} | |
// We have switched off the default Webpack output in WebpackDevServer | |
// options so we are going to "massage" the warnings and errors and present | |
// them in a readable focused way. | |
var messages = formatWebpackMessages(stats.toJson({}, true)); | |
var isSuccessful = !messages.errors.length && !messages.warnings.length; | |
var showInstructions = isSuccessful && (isInteractive || isFirstCompile); | |
if (isSuccessful) { | |
console.log(chalk.green('Compiled successfully!')); | |
} | |
if (!hasStartedElectron) { | |
spawn(electron, ['.']); | |
hasStartedElectron = true; | |
} | |
if (showInstructions) { | |
console.log(); | |
console.log('The app is running at:'); | |
console.log(); | |
console.log(' ' + chalk.cyan(protocol + '://' + host + ':' + port + '/')); | |
console.log(); | |
console.log('Note that the development build is not optimized.'); | |
console.log('To create a production build, use ' + chalk.cyan(cli + ' run build') + '.'); | |
console.log(); | |
isFirstCompile = false; | |
} | |
// If errors exist, only show errors. | |
if (messages.errors.length) { | |
console.log(chalk.red('Failed to compile.')); | |
console.log(); | |
messages.errors.forEach(message => { | |
console.log(message); | |
console.log(); | |
}); | |
return; | |
} | |
// Show warnings if no errors were found. | |
if (messages.warnings.length) { | |
console.log(chalk.yellow('Compiled with warnings.')); | |
console.log(); | |
messages.warnings.forEach(message => { | |
console.log(message); | |
console.log(); | |
}); | |
// Teach some ESLint tricks. | |
console.log('You may use special comments to disable some warnings.'); | |
console.log('Use ' + chalk.yellow('// eslint-disable-next-line') + ' to ignore the next line.'); | |
console.log('Use ' + chalk.yellow('/* eslint-disable */') + ' to ignore all warnings in a file.'); | |
} | |
}); | |
} | |
// We need to provide a custom onError function for httpProxyMiddleware. | |
// It allows us to log custom error messages on the console. | |
function onProxyError(proxy) { | |
return function(err, req, res){ | |
var host = req.headers && req.headers.host; | |
console.log( | |
chalk.red('Proxy error:') + ' Could not proxy request ' + chalk.cyan(req.url) + | |
' from ' + chalk.cyan(host) + ' to ' + chalk.cyan(proxy) + '.' | |
); | |
console.log( | |
'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' + | |
chalk.cyan(err.code) + ').' | |
); | |
console.log(); | |
// And immediately send the proper error response to the client. | |
// Otherwise, the request will eventually timeout with ERR_EMPTY_RESPONSE on the client side. | |
if (res.writeHead && !res.headersSent) { | |
res.writeHead(500); | |
} | |
res.end('Proxy error: Could not proxy request ' + req.url + ' from ' + | |
host + ' to ' + proxy + ' (' + err.code + ').' | |
); | |
} | |
} | |
function addMiddleware(devServer) { | |
// `proxy` lets you to specify a fallback server during development. | |
// Every unrecognized request will be forwarded to it. | |
var proxy = require(paths.appPackageJson).proxy; | |
devServer.use(historyApiFallback({ | |
// Paths with dots should still use the history fallback. | |
// See https://github.com/facebookincubator/create-react-app/issues/387. | |
disableDotRule: true, | |
// For single page apps, we generally want to fallback to /index.html. | |
// However we also want to respect `proxy` for API calls. | |
// So if `proxy` is specified, we need to decide which fallback to use. | |
// We use a heuristic: if request `accept`s text/html, we pick /index.html. | |
// Modern browsers include text/html into `accept` header when navigating. | |
// However API calls like `fetch()` won’t generally accept text/html. | |
// If this heuristic doesn’t work well for you, don’t use `proxy`. | |
htmlAcceptHeaders: proxy ? | |
['text/html'] : | |
['text/html', '*/*'] | |
})); | |
if (proxy) { | |
if (typeof proxy !== 'string') { | |
console.log(chalk.red('When specified, "proxy" in package.json must be a string.')); | |
console.log(chalk.red('Instead, the type of "proxy" was "' + typeof proxy + '".')); | |
console.log(chalk.red('Either remove "proxy" from package.json, or make it a string.')); | |
process.exit(1); | |
} | |
// Otherwise, if proxy is specified, we will let it handle any request. | |
// There are a few exceptions which we won't send to the proxy: | |
// - /index.html (served as HTML5 history API fallback) | |
// - /*.hot-update.json (WebpackDevServer uses this too for hot reloading) | |
// - /sockjs-node/* (WebpackDevServer uses this for hot reloading) | |
// Tip: use https://jex.im/regulex/ to visualize the regex | |
var mayProxy = /^(?!\/(index\.html$|.*\.hot-update\.json$|sockjs-node\/)).*$/; | |
// Pass the scope regex both to Express and to the middleware for proxying | |
// of both HTTP and WebSockets to work without false positives. | |
var hpm = httpProxyMiddleware(pathname => mayProxy.test(pathname), { | |
target: proxy, | |
logLevel: 'silent', | |
onProxyReq: function(proxyReq) { | |
// Browers may send Origin headers even with same-origin | |
// requests. To prevent CORS issues, we have to change | |
// the Origin to match the target URL. | |
if (proxyReq.getHeader('origin')) { | |
proxyReq.setHeader('origin', proxy); | |
} | |
}, | |
onError: onProxyError(proxy), | |
secure: false, | |
changeOrigin: true, | |
ws: true, | |
xfwd: true | |
}); | |
devServer.use(mayProxy, hpm); | |
// Listen for the websocket 'upgrade' event and upgrade the connection. | |
// If this is not done, httpProxyMiddleware will not try to upgrade until | |
// an initial plain HTTP request is made. | |
devServer.listeningApp.on('upgrade', hpm.upgrade); | |
} | |
// Finally, by now we have certainly resolved the URL. | |
// It may be /index.html, so let the dev server try serving it again. | |
devServer.use(devServer.middleware); | |
} | |
function runDevServer(host, port, protocol) { | |
var devServer = new WebpackDevServer(compiler, { | |
// Enable gzip compression of generated files. | |
compress: true, | |
// Silence WebpackDevServer's own logs since they're generally not useful. | |
// It will still show compile warnings and errors with this setting. | |
clientLogLevel: 'none', | |
// By default WebpackDevServer serves physical files from current directory | |
// in addition to all the virtual build products that it serves from memory. | |
// This is confusing because those files won’t automatically be available in | |
// production build folder unless we copy them. However, copying the whole | |
// project directory is dangerous because we may expose sensitive files. | |
// Instead, we establish a convention that only files in `public` directory | |
// get served. Our build script will copy `public` into the `build` folder. | |
// In `index.html`, you can get URL of `public` folder with %PUBLIC_URL%: | |
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> | |
// In JavaScript code, you can access it with `process.env.PUBLIC_URL`. | |
// Note that we only recommend to use `public` folder as an escape hatch | |
// for files like `favicon.ico`, `manifest.json`, and libraries that are | |
// for some reason broken when imported through Webpack. If you just want to | |
// use an image, put it in `src` and `import` it from JavaScript instead. | |
contentBase: paths.appPublic, | |
// Enable hot reloading server. It will provide /sockjs-node/ endpoint | |
// for the WebpackDevServer client so it can learn when the files were | |
// updated. The WebpackDevServer client is included as an entry point | |
// in the Webpack development configuration. Note that only changes | |
// to CSS are currently hot reloaded. JS changes will refresh the browser. | |
hot: true, | |
// It is important to tell WebpackDevServer to use the same "root" path | |
// as we specified in the config. In development, we always serve from /. | |
publicPath: config.output.publicPath, | |
// WebpackDevServer is noisy by default so we emit custom message instead | |
// by listening to the compiler events with `compiler.plugin` calls above. | |
quiet: true, | |
// Reportedly, this avoids CPU overload on some systems. | |
// https://github.com/facebookincubator/create-react-app/issues/293 | |
watchOptions: { | |
ignored: /node_modules/ | |
}, | |
// Enable HTTPS if the HTTPS environment variable is set to 'true' | |
https: protocol === "https", | |
host: host | |
}); | |
// Our custom middleware proxies requests to /index.html or a remote API. | |
addMiddleware(devServer); | |
// Launch WebpackDevServer. | |
devServer.listen(port, err => { | |
if (err) { | |
return console.log(err); | |
} | |
if (isInteractive) { | |
clearConsole(); | |
} | |
console.log(chalk.cyan('Starting the development server...')); | |
console.log(); | |
openBrowser(protocol + '://' + host + ':' + port + '/'); | |
}); | |
} | |
function run(port) { | |
var protocol = process.env.HTTPS === 'true' ? "https" : "http"; | |
var host = process.env.HOST || 'localhost'; | |
setupCompiler(host, port, protocol); | |
runDevServer(host, port, protocol); | |
} | |
// We attempt to use the default port but if it is busy, we offer the user to | |
// run on a different port. `detect()` Promise resolves to the next free port. | |
detect(DEFAULT_PORT).then(port => { | |
if (port === DEFAULT_PORT) { | |
run(port); | |
return; | |
} | |
if (isInteractive) { | |
clearConsole(); | |
var existingProcess = getProcessForPort(DEFAULT_PORT); | |
var question = | |
chalk.yellow('Something is already running on port ' + DEFAULT_PORT + '.' + | |
((existingProcess) ? ' Probably:\n ' + existingProcess : '')) + | |
'\n\nWould you like to run the app on another port instead?'; | |
prompt(question, true).then(shouldChangePort => { | |
if (shouldChangePort) { | |
run(port); | |
} | |
}); | |
} else { | |
console.log(chalk.red('Something is already running on port ' + DEFAULT_PORT + '.')); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var autoprefixer = require('autoprefixer'); | |
var webpack = require('webpack'); | |
var HtmlWebpackPlugin = require('html-webpack-plugin'); | |
var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin'); | |
var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin'); | |
var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin'); | |
var getClientEnvironment = require('./env'); | |
var paths = require('./paths'); | |
// Webpack uses `publicPath` to determine where the app is being served from. | |
// In development, we always serve from the root. This makes config easier. | |
var publicPath = '/'; | |
// `publicUrl` is just like `publicPath`, but we will provide it to our app | |
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript. | |
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz. | |
var publicUrl = ''; | |
// Get environment variables to inject into our app. | |
var env = getClientEnvironment(publicUrl); | |
// This is the development configuration. | |
// It is focused on developer experience and fast rebuilds. | |
// The production configuration is different and lives in a separate file. | |
module.exports = { | |
// You may want 'eval' instead if you prefer to see the compiled output in DevTools. | |
// See the discussion in https://github.com/facebookincubator/create-react-app/issues/343. | |
devtool: 'cheap-module-source-map', | |
// These are the "entry points" to our application. | |
// This means they will be the "root" imports that are included in JS bundle. | |
// The first two entry points enable "hot" CSS and auto-refreshes for JS. | |
entry: [ | |
// Include an alternative client for WebpackDevServer. A client's job is to | |
// connect to WebpackDevServer by a socket and get notified about changes. | |
// When you save a file, the client will either apply hot updates (in case | |
// of CSS changes), or refresh the page (in case of JS changes). When you | |
// make a syntax error, this client will display a syntax error overlay. | |
// Note: instead of the default WebpackDevServer client, we use a custom one | |
// to bring better experience for Create React App users. You can replace | |
// the line below with these two lines if you prefer the stock client: | |
// require.resolve('webpack-dev-server/client') + '?/', | |
// require.resolve('webpack/hot/dev-server'), | |
require.resolve('react-dev-utils/webpackHotDevClient'), | |
// We ship a few polyfills by default: | |
require.resolve('./polyfills'), | |
// Finally, this is your app's code: | |
paths.appIndexJs | |
// We include the app code last so that if there is a runtime error during | |
// initialization, it doesn't blow up the WebpackDevServer client, and | |
// changing JS code would still trigger a refresh. | |
], | |
output: { | |
// Next line is not used in dev but WebpackDevServer crashes without it: | |
path: paths.appBuild, | |
// Add /* filename */ comments to generated require()s in the output. | |
pathinfo: true, | |
// This does not produce a real file. It's just the virtual path that is | |
// served by WebpackDevServer in development. This is the JS bundle | |
// containing code from all our entry points, and the Webpack runtime. | |
filename: 'static/js/bundle.js', | |
// This is the URL that app is served from. We use "/" in development. | |
publicPath: publicPath | |
}, | |
resolve: { | |
// This allows you to set a fallback for where Webpack should look for modules. | |
// We read `NODE_PATH` environment variable in `paths.js` and pass paths here. | |
// We use `fallback` instead of `root` because we want `node_modules` to "win" | |
// if there any conflicts. This matches Node resolution mechanism. | |
// https://github.com/facebookincubator/create-react-app/issues/253 | |
fallback: paths.nodePaths, | |
// These are the reasonable defaults supported by the Node ecosystem. | |
// We also include JSX as a common component filename extension to support | |
// some tools, although we do not recommend using it, see: | |
// https://github.com/facebookincubator/create-react-app/issues/290 | |
extensions: ['.js', '.json', '.jsx', ''], | |
alias: { | |
// Support React Native Web | |
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/ | |
'react-native': 'react-native-web' | |
} | |
}, | |
module: { | |
// First, run the linter. | |
// It's important to do this before Babel processes the JS. | |
preLoaders: [ | |
{ | |
test: /\.(js|jsx)$/, | |
loader: 'eslint', | |
include: paths.appSrc, | |
} | |
], | |
loaders: [ | |
// ** ADDING/UPDATING LOADERS ** | |
// The "url" loader handles all assets unless explicitly excluded. | |
// The `exclude` list *must* be updated with every change to loader extensions. | |
// When adding a new loader, you must add its `test` | |
// as a new entry in the `exclude` list for "url" loader. | |
// "url" loader embeds assets smaller than specified size as data URLs to avoid requests. | |
// Otherwise, it acts like the "file" loader. | |
{ | |
exclude: [ | |
/\.html$/, | |
// We have to write /\.(js|jsx)(\?.*)?$/ rather than just /\.(js|jsx)$/ | |
// because you might change the hot reloading server from the custom one | |
// to Webpack's built-in webpack-dev-server/client?/, which would not | |
// get properly excluded by /\.(js|jsx)$/ because of the query string. | |
// Webpack 2 fixes this, but for now we include this hack. | |
// https://github.com/facebookincubator/create-react-app/issues/1713 | |
/\.(js|jsx)(\?.*)?$/, | |
/\.css$/, | |
/\.json$/, | |
/\.svg$/ | |
], | |
loader: 'url', | |
query: { | |
limit: 10000, | |
name: 'static/media/[name].[hash:8].[ext]' | |
} | |
}, | |
// Process JS with Babel. | |
{ | |
test: /\.(js|jsx)$/, | |
include: paths.appSrc, | |
loader: 'babel', | |
query: { | |
// This is a feature of `babel-loader` for webpack (not Babel itself). | |
// It enables caching results in ./node_modules/.cache/babel-loader/ | |
// directory for faster rebuilds. | |
cacheDirectory: true | |
} | |
}, | |
// "postcss" loader applies autoprefixer to our CSS. | |
// "css" loader resolves paths in CSS and adds assets as dependencies. | |
// "style" loader turns CSS into JS modules that inject <style> tags. | |
// In production, we use a plugin to extract that CSS to a file, but | |
// in development "style" loader enables hot editing of CSS. | |
{ | |
test: /\.css$/, | |
loader: 'style!css?importLoaders=1!postcss' | |
}, | |
// JSON is not enabled by default in Webpack but both Node and Browserify | |
// allow it implicitly so we also enable it. | |
{ | |
test: /\.json$/, | |
loader: 'json' | |
}, | |
// "file" loader for svg | |
{ | |
test: /\.svg$/, | |
loader: 'file', | |
query: { | |
name: 'static/media/[name].[hash:8].[ext]' | |
} | |
} | |
// ** STOP ** Are you adding a new loader? | |
// Remember to add the new extension(s) to the "url" loader exclusion list. | |
] | |
}, | |
target: 'electron-renderer', | |
// We use PostCSS for autoprefixing only. | |
postcss: function() { | |
return [ | |
autoprefixer({ | |
browsers: [ | |
'>1%', | |
'last 4 versions', | |
'Firefox ESR', | |
'not ie < 9', // React doesn't support IE8 anyway | |
] | |
}), | |
]; | |
}, | |
plugins: [ | |
// Makes some environment variables available in index.html. | |
// The public URL is available as %PUBLIC_URL% in index.html, e.g.: | |
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> | |
// In development, this will be an empty string. | |
new InterpolateHtmlPlugin(env.raw), | |
// Generates an `index.html` file with the <script> injected. | |
new HtmlWebpackPlugin({ | |
inject: true, | |
template: paths.appHtml, | |
}), | |
// Makes some environment variables available to the JS code, for example: | |
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`. | |
new webpack.DefinePlugin(env.stringified), | |
// This is necessary to emit hot updates (currently CSS only): | |
new webpack.HotModuleReplacementPlugin(), | |
// Watcher doesn't work well if you mistype casing in a path so we use | |
// a plugin that prints an error when you attempt to do this. | |
// See https://github.com/facebookincubator/create-react-app/issues/240 | |
new CaseSensitivePathsPlugin(), | |
// If you require a missing module and then `npm install` it, you still have | |
// to restart the development server for Webpack to discover it. This plugin | |
// makes the discovery automatic so you don't have to restart. | |
// See https://github.com/facebookincubator/create-react-app/issues/186 | |
new WatchMissingNodeModulesPlugin(paths.appNodeModules) | |
] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment