Running and building for Desktop with Electron
October 21, 2017 ยท View on GitHub
Electron makes it pretty easy to run angular2-instagram as desktop app. To create a standalone executable we're using two packages:
electron-builderelectron-packager
We're using both for educational purposes, a single one of those would suffice.
Configure Electron
To configure electron we need to create an entry file that will be run when calling the electron command. You can find it here on /src/app/electron/electron.js.
Running Electron on development and production mode
npm run electron:devwill startwebpack-dev-serveralong withelectronwith hot reloading and devtools activatednpm run electron:prodwill first build the app following the standard production webpack pipeline, then it will copy/package.jsonand/src/app/electron/electron.jsto the output/publicdirectory and finally runelectron. Webpack is not running in--watchmode so changes won't be rebuilt
Creating the standalone executable
This project uses both electron-packager and electron-builder to create the standalone app. Consider that you might need additional tools in order to package the app for multiple platforms using a single OS (eg. to package for Windows using Linux you will need Wine).
Building and packaging with electron-builder
By running npm run electron:builder the app will be built in production mode with webpack and the resulting files under the /public folder will be packaged to create the standalone electron app. If no other parameter is set, the app will be packaged for the current OS and architecture (eg. macOS 64bit) and will be found under the /dist folder.
If you want, for example, pack the app for Windows 32bit you could do npm run electron:builder -- -w --ia32. If you want to build for all 64bit platforms you should run npm run electron:builder -- -lwm --x64.
The configuration that electron-builder uses to create the package is in the build section of package.json.
Following are some useful information to keep in mind:
appIdis a general name in the form ofcom.electron.app-namecategoryis the category in which the app can be identified, here is a list of possible categoriesbuildResourcesis the folder where backgrounds and icons are stored: abackground.pngused as macOS DMG background, app icons in the form oficon.icns(macOS app icon) andicon.ico(Windows app icon) and the same icons in.pngformat named24x24.png,32x32.png,48x48.png,64x64.png,128x128.png,256x256.pngeach with the corresponding dimensionsfilesis a list of files and folders to be packaged in the final app, defining them here also won't copy all thenode_modulesandsrcfileslinuxis just an example to show you can define specific target images/formats for the packaged app
There is much more to it, for full documentation and examples here are some useful links:
Building with electron-packager
By running npm run electron:packager the following steps will happen:
- the app will be built in
productionmode withwebpack - the
electron.jsconfiguration file will be copied with its folder structure inside/publicfolder electron-packagerwill package all the contents of the/publicfolder and create an executable file under the/dist/...folder
If no other parameter is set, the app will be packaged for the current OS and architecture (eg. macOS 64bit). To packager for multiple OS and architecture you can use the --platform=<platform> and --arch=<arch> respectively.
- For a full documentation please refer to the
electron-packagerGithub page. - For a detailed list of all the available parameters click here
Publishing the executable on Github, Amazon S3...
We are using electron-builder via the script npm run electron:publish command to create a release in the angular2-instagram repository and attach the standalone desktop app.
This is the step by step process we follow:
- first created a Github token with repo access
- locally set the env variable with eg.
export GH_TOKEN="<TOKEN_HERE>" - update the
versioninpackage.json, this will also be the name (prepended with aveg.0.0.2will becomev0.0.2) of thereleasethat will be created in Github - run
npm run build:electronif theelectronproductionversion has not been built yet - run
npm run electron:publishto push the package to Github - go to the Github release section of the repository, edit the description and title of the release and publish it, since it was initially created by
electron-builderas adraft
For more info on programmatically publishing the executable on other sources you can have a look at the official documentation on electron-builder.