Help us understand the problem. What is going on with this article?

Angular7でElectron

More than 1 year has passed since last update.

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用にパッケージされた実行可能ファイルが出来上がる。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした