Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions packages/react-scripts/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Copy link

@sebm sebm Apr 23, 2017

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 somewhere

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

"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 .",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably ask user to put these in .env file because this syntax is not cross-platform.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #1712 (comment) though.

Copy link
Author

@poksme poksme Mar 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The BROWSER env variable can easily be moved to a .env file but the ELECTRON_DEV flag can be problematic because for the user it means changing the env variable value when building or having multiple .env files and some loading logic to switch between prod and build.

What do you think of electron-is-dev instead of the ELECTRON_DEV flag?

"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,
Expand Down