18
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Angular7でElectron

Last updated at Posted at 2018-11-07

AngularでElectronアプリを開発する準備

Angular7版の情報が少なかったので、メモ。

プロジェクトを作ってElectronをインストール


npm i -g @angular/cli

ng new electron-sample

cd electron-sample

npm install --save-dev electron@latest

npm install --save-dev electron-packager@latest

npm install --save-dev ngx-build-plus@latest

webpackを拡張するための ngx-build-plus をインストールしているのがポイント。

いくつかのファイルを作ったり編集したり

main.jsを作る

main.jsファイルをプロジェクトルート直下に作って、
Electronのチュートリアルページのコードをコピって、
12行目のwin.loadFile(filename)の箇所を自分のアプリ用に編集する。


const { app, BrowserWindow } = require('electron');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win;

function createWindow() {
  // Create the browser window.
  win = new BrowserWindow({ width: 800, height: 600 });

  // and load the index.html of the app.
  win.loadFile('dist/electron-sample/index.html');

  // Open the DevTools.
  win.webContents.openDevTools();

  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null;
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow();
  }
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

index.htmlを編集する

src/index.htmlのhead内のbaseタグを次のように変える。

<base href="/"> → <base href=".">

webpack.extra.jsを作る

Angular CLIのバージョン6以降ではng ejectが使えず、webpackを編集できない。
なので、代わりにngx-build-plusを使う。

webpack.extra.jsファイルをプロジェクトルート直下に作って、次のように書いておく。


const webpack = require('webpack');

module.exports = {
  'target': 'electron-renderer',
};

これをやらないと、nodeのモジュール(fsとかpathとか)が全然使えない。

angular.jsonを編集する

serveやbuild時に、@angular-devkit/build-angularではなく前述のngx-build-plusを使うようにする。


~略~
"architect": {
  "build": {
    "builder": "ngx-build-plus:build",
~略~
  "serve": {
    "builder": "ngx-build-plus:dev-server",

package.jsonを編集する

mainmain.jsを指定する行を追加し、
scriptsにビルドしてElectronでアプリを起動およびパッケージするコマンドを追加する。


{
  "name": "electron-sample",
  "version": "0.0.0",
  "main": "main.js",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "electron": "ng build --extraWebpackConfig webpack.extra.js && electron ."
    "package": "electron-packager ."
  },
  ~略~

ng build --extraWebpackConfig webpack.extra.jsとすることで、前述のwebpack.extra.jsが反映される。

動かしてみる

npm run electron

で、Electronアプリとして起動される。

npm run package

で、WindowsやMac用にパッケージされた実行可能ファイルが出来上がる。

18
19
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?