Angular 4 + Electron - How to run application and watch for changes (live reload)

流过昼夜 提交于 2019-12-04 07:58:56

You can use electron-reload for hot module reload. It listens on file changes and reloads the electron app.

Simply add it to the devDependencies in your package.json.

Then, you have to add it to your main.ts:

import { app, BrowserWindow, Menu } from 'electron';

let win, serve;
const args = process.argv.slice(1);
serve = args.some(val => val === '--serve');

if (serve) {
  require('electron-reload')(__dirname, {});
}

Then add a command to your package json

"start": "webpack --watch",
"serve": "npm run build:main && electron ./dist --serve",
"build:main": "tsc main.ts --outDir dist && copyfiles package.json dist && cd dist && npm install --prod"

where build:main is your build script to compile your project. This compiles all Typescript files and puts them into the dist folder. Afterwards, it runs npm install to download and install modules from NPM.

You need the module-bundler Webpack for this to run. Install it with

npm install --save-dev webpack

First, in a console run npm start. Afterwards, in a second console executenpm run serve. Now, it listens for changes and recompiles on file changes.

tsc stands for TypeScript compiler. If you're using tsc as a node module, make sure its installed:

npm install -g typescript

I use it currently for a project with the same setup (Angular 4, Electron) and it works perfectly.

I personally hate pretty much all other answers out there on the internet. I chose to do something more off the wall. I do ALL development with just raw angular, and stub out the parts of the application that require electron things, like sharing the desktop, or getting a config file from somewhere like this:

if (!this.electron.isElectronApp) {
  return {
    config:data,
    more:data,
  }
} else {
  do electron things
}

It so far has worked wonderfully. If can anyone think how this is going to bite me in the butt later please share.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!