Skip to content

Latest commit

 

History

History
133 lines (105 loc) · 4.38 KB

README.md

File metadata and controls

133 lines (105 loc) · 4.38 KB

elemon

npm

Standard JavaScript

elemon is a tiny module that tries to provide a simple and yet efficient live-reload tool when developing 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. The api already comes with whatever you need; just add a watcher (like 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 object
  • mainFile {String} main file name
  • bws {Array<Object>} array of browser window objects and their resources [{bw:, res: []}]
    • bw {Object} BrowserWindow object
    • res {Array<String>} 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

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 like this:

npm i electron-is-dev --save

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 applications.