-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Integration with Electron tutorial #1718
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ You can find the most recent version of this guide [here](https://github.com/fac | |
- [Adding Temporary Environment Variables In Your Shell](#adding-temporary-environment-variables-in-your-shell) | ||
- [Adding Development Environment Variables In `.env`](#adding-development-environment-variables-in-env) | ||
- [Can I Use Decorators?](#can-i-use-decorators) | ||
- [Integrating with Electron](#integrating-with-electron) | ||
- [Integrating with an API Backend](#integrating-with-an-api-backend) | ||
- [Node](#node) | ||
- [Ruby on Rails](#ruby-on-rails) | ||
|
@@ -771,6 +772,73 @@ Please refer to these two threads for reference: | |
|
||
Create React App will add decorator support when the specification advances to a stable stage. | ||
|
||
## Integrating with Electron | ||
|
||
[Electron](https://electron.atom.io/) offers an API to create cross platform desktop applications using HTML, CSS and JavaScript in a Node environment. Electron can render a Create React App with minimal changes. | ||
|
||
Start by creating a React app using `create-react-app` command line tool. Then, install the following packages: | ||
- `electron` is the Electron package that includes all necessary modules to create an Electron app | ||
- `electron-packager` is a command line tool necessary to bundle an Electron app | ||
- `concurrently` is a command line tool that runs two processes concurrently | ||
- `wait-on` is a command line tool that waits for a file, port, socket, or http resource to be available | ||
|
||
At the root of the React application folder you will need to create an Electron script for initializing the Electron app. [Electron's quick start](https://github.com/electron/electron-quick-start) `main.js` is a good starting point. Extra logic needs to be added to the window creation in order to render the React application in both development and bundled environments. On easy way to distinguish development from bundled environment is to use an environment variable. Here is an example implementation using `ELECTRON_DEV` environment variable. | ||
|
||
```javascript | ||
// Create the browser window. | ||
mainWindow = new BrowserWindow({width: 800, height: 600}) | ||
|
||
// and load the index.html of the app. | ||
mainWindow.loadURL( | ||
process.env.ELECTRON_DEV | ||
? 'http://localhost:3000' // Dev server ran by react-scripts | ||
: `file://${path.join(__dirname, '/build/index.html')}` // Bundled application | ||
); | ||
``` | ||
|
||
Once this file is created, the `package.json` needs to be edited: | ||
- Set the Electron script (here `main.js`) as `main` | ||
- Set application's root (`./`) as `homepage` | ||
- Rename `start` and `build` scripts as `react-start` and `react-build` | ||
- Prefix `react-start` script with `BROWSER=none` to prevent the script from opening a browser window on start | ||
- Add `electron-start` script that runs Electron in development mode: `ELECTRON_DEV=1 electron .` | ||
- Add `electron-build` script that bundles the Electron app: `electron-packager ./` | ||
- Add high level `start` script that runs concurrently the React development server and the Electron app once the React app is ready to be rendered: `concurrently 'yarn react-start' 'wait-on http://localhost:3000/ && yarn electron-start'` | ||
- Add high level `build` script that bundles both the React app and the Electron app: `yarn react-build && yarn electron-build` | ||
|
||
The `package.json` should be as follow: | ||
|
||
```JSON | ||
{ | ||
"name": "my-electron-app", | ||
"version": "0.1.0", | ||
"private": true, | ||
"devDependencies": { | ||
"concurrently": "^3.4.0", | ||
"electron": "^1.6.1", | ||
"electron-packager": "^8.5.2", | ||
"react-scripts": "0.9.3", | ||
"wait-on": "^2.0.2" | ||
}, | ||
"dependencies": { | ||
"react": "^15.4.2", | ||
"react-dom": "^15.4.2" | ||
}, | ||
"main": "main.js", | ||
"homepage": "./", | ||
"scripts": { | ||
"react-start": "BROWSER=none react-scripts start", | ||
"electron-start": "ELECTRON_DEV=1 electron .", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably ask user to put these in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #1712 (comment) though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The What do you think of |
||
"start": "concurrently 'yarn react-start' 'wait-on http://localhost:3000/ && yarn electron-start'", | ||
"react-build": "react-scripts build", | ||
"electron-build": "electron-packager ./ --overwrite", | ||
"build": "yarn react-build && yarn electron-build", | ||
"test": "react-scripts test --env=jsdom", | ||
"eject": "react-scripts eject" | ||
} | ||
} | ||
``` | ||
|
||
## Integrating with an API Backend | ||
|
||
These tutorials will help you to integrate your app with an API backend running on another port, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nb
electron-is-dev
should be in package.json somewhereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it needs to be in
dependencies