LoginSignup
0
0

More than 3 years have passed since last update.

Electronでデスクトップアプリケーションのパッケージングまで

Posted at

Electronでデスクトップアプリを配布できるようにするパッケージングまでmacでやっていきたいと思います。

Node.jsはインストールしている状態で始めるので、あらかじめインストールしておいてください。

必要なファイル、ディレクトリの作成

まずアプリを作成するために必要なファイル、ディレクトリを準備します。

アプリを作成したいディレクトリに移動したのち、以下のコマンドを実行します。

ターミナル
$ mkdir hoge
$ cd hoge

これでhogeのディレクトリ内に入りました。

次に以下のコマンドを実行します。

ターミナル
$ npm install -D electron

実行するとnode_nodulespackage-lock.jsonが作成されました。

次に以下のコマンドを実行します。

ターミナル
$ npm init

このコマンドを実行するとpackage name: (hoge)のように表示されると思います。
ここで設定を決めることができますが、後でも変更することができるので、いまはEnterを押して進めてください。

進めるとpackage.jsonというファイルが作成されたと思います。

次にsrcというディレクトリとその中身を作成していきます。

まずディレクトリを作成して移動します。

ターミナル
$ mkdir src
$ cd src

srcの中にmain.jsindex.htmlpackage.jsonを作成します。

ターミナル
$ touch main.js index.html
$ npm init

これでアプリを実行するためのファイルが用意できました。
以下のようなファイルがあればOKです。

スクリーンショット 2020-07-11 21.43.10.png

アプリケーションを起動する

アプリを起動できるように設定します。

まずはsrcディレクトリ内にあるpackage.jsonを編集します。

"main":を以下のように編集してください。

package.json
{
  "main": "main.js"
}

次にmain.jsindex.htmlに以下を追加してください。

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

let mainWindow;

function createWindow() {
  mainWindow = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true,
    },
    width: 800, height: 600,
  });

  mainWindow.loadFile('index.html');

  mainWindow.webContents.openDevTools();

  mainWindow.on('closed', () => {
    mainWindow = null;
  });
}

app.on('ready', createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
app.on('activate', () => {
  if (mainWindow === null) {
    createWindow();
  }
});
index.html
<html>
<head>
  <meta charset="UTF-8">
  <title>Hello World!</title>
</head>

<body>
  <h1>初めてのElectron</h1>
  We are using node <script>document.write(process.versions.node)</script>,
  Chrome <script>document.write(process.versions.chrome)</script>,
  and Electron <script>document.write(process.versions.electron)</script>.
</body>
</html>

これで表示するためのコードを書き終えたので、アプリを実行してみます。

hogeのディレクトリに移動して以下のコマンドを実行してください

ターミナル
$ npx electron src

スクリーンショット 2020-07-11 22.02.29.png

このように表示されたら成功です。

終了するためにはcontrol + cを押してください。

パッケージングする

アプリの起動を確認できたら次にパッケージングをします。

パッケージングをするためにelectron-packagerをインストールします。
インストールする場合はhogeディレクトリで実行してください。
srcディレクトリで実行しないようにしてください。

ターミナル
$ npm i -D electron-packager

インストールが終了したら以下のコマンドを実行します。

ターミナル
$ npx electron-packager src hoge --platform=darwin --arch=x64 --overwrite

パッケージングに成功すると、以下のようにファイルが作成されます。

スクリーンショット 2020-07-11 22.18.14.png

hogeと書いてあるアプリをクリックして、先ほど表示した画面と同じ画面が表示されたら成功です。

開発用にコマンドを簡略化する

ターミナルでアプリを起動する際にnpx electron srcというコマンドを実行しましたが、hogeディレクトリのpackage.jsonファイルのscriptsの項目を編集するとnpm startで起動できるようになります。

"scripts": {}の中に記載されているコードを以下のように書き換えてください。

package.json
"scripts": {
  "start": "electron ./src"
}

ターミナルで以下のコードを実行してアプリが起動できたら成功です。

ターミナル
$ npm start

参考サイト

0
0
0

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
0
0