Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/manidlou/elemon

live-reload Electron application during development
https://github.com/manidlou/elemon

electron live-reload

Last synced: about 2 months ago
JSON representation

live-reload Electron application during development

Lists

README

        

elemon
======

[![npm](https://img.shields.io/npm/v/elemon.svg?style=flat-square)](https://www.npmjs.com/package/elemon)

Standard JavaScript

`elemon` is a tiny module that tries to provide a simple and yet efficient live-reload tool when developing [Electron](https://github.com/electron/electron) applications. You just need to pass the `app` and `BrowserWindows` and the name of the files that are associated with them as a parameter to the `elemon` function **after** your app is `ready`. Please check out the example below to see how you can easily use it to watch your app and cleanly reload it upon any changes. If the changed
file is the main app file, then it `relaunch` the app and `exit` the current instance. If the changed file is a file that is associated with a browser window, then that window will only be reloaded.

In fact, setting up a clean live-reload tool when developing an electron application is super simple by using the [Electron API](https://github.com/electron/electron/tree/master/docs). The api already comes with whatever you need; just add a watcher (like [chokidar](https://github.com/paulmillr/chokidar) or whatever watcher you like) and you are all set.

Install
-------

`npm i elemon --save-dev`.

Usage
-----

**elemon(refs)**

`refs` `{Object}` object that takes references to app and browser windows objects and resources
- `app` `{Object}` main [app](https://github.com/electron/electron/blob/master/docs/api/app.md) object
- `mainFile` `{String}` main file name
- `bws` `{Array}` array of browser window objects and their resources `[{bw:, res: []}]`
- `bw` `{Object}` [BrowserWindow](https://github.com/electron/electron/blob/master/docs/api/browser-window.md) object
- `res` `{Array}` array of any file name that is somehow associated with this browser window
- _if you want to watch all files in dir, or if you want the `bw` to be reloaded on any changes and not necessarily changes on specific file(s), leave the `res` as empty `[]`._

Example
-------

Suppose it is the app file structure:

```
example_proj
|
|__views
| |__win1.html
| |__win2.html
| |__win1.js
| |__win2.js
|
|__stylesheets
| |__style.css
|
|__main.js

```
then, in the main process file where usually app and browser windows are created:

*main.js*

```js

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
const elemon = require('elemon')

let win1, win2

function createWindows () {
win1 = new BrowserWindow({width: 800, height: 600})
win1.loadURL(url.format({
pathname: path.join(__dirname, 'views', 'win1.html'),
protocol: 'file:',
slashes: true
}))
win1.on('closed', () => {
win1 = null
})

win2 = new BrowserWindow({width: 800, height: 600})
win2.loadURL(url.format({
pathname: path.join(__dirname, 'views', 'win2.html'),
protocol: 'file:',
slashes: true
}))
win2.on('closed', () => {
win2 = null
})
}

// ... and other usual stuff ... //

app.on('ready', () => {
createWindows()

// this is all that you have to add to your main app script.
// run your app normally with electron, then it will be reloaded
// based on how you define references here
elemon({
app: app,
mainFile: 'main.js',
bws: [
{bw: win1, res: ['win1.html', 'win1.js', 'style.css']},
{bw: win2, res: ['win2.html', 'win2.js', 'style.css']}
]
})
})
```
If you want to make sure that you don't get undefined error when you build your app, you can use `elemon` along with [electron-is-dev](https://github.com/sindresorhus/electron-is-dev) like this:

`npm i electron-is-dev --save`

```js
const {app, BrowserWindow} = require('electron')
const isDev = require('electron-is-dev')

function createWindows () {
// ...
}

app.on('ready', () => {
createWindows()

if (isDev) {
const elemon = require('elemon') // require elemon if electron is in dev
elemon({
app: app,
mainFile: 'main.js',
bws: [
{bw: win1, res: ['win1.html', 'win1.js', 'style.css']},
{bw: win2, res: ['win2.html', 'win2.js', 'style.css']}
]
})
}
})
```

That's it. Have fun writing your [Electron](https://github.com/electron/electron) applications.