LoginSignup
5
6

More than 5 years have passed since last update.

Electronをいじってみる1

Last updated at Posted at 2017-06-05

はまったけどとりあえず動くようにはなった。
自分が理解できる文献がなかなか見つからないから大変。

前提
1. Node.js, TypeScript, typingsがインストールされていること
2. 使ったバージョンは、Node.js@v6.10.3, TypeScript@2.3.4, typings@2.1.1

手順
1. ソースと型定義のインストール
2. サンプルをコンパイルする
3. 実行する

追記
4. パッケージングする

1. ソースと型定義のインストール

npm install -g electron
typings install dt~electron --global --save

electronのインストールでエラーが出たが

npm cache clear

を実行してからやり直したらうまくいった。

2. サンプルをコンパイルする

main.ts
///<reference path="./typings/index.d.ts"/>

// In the main process.
const { app, BrowserWindow } = require('electron')
app.on('window-all-closed', () => {
    app.quit()
})

app.on('ready', () => {
    let win = new BrowserWindow({ width: 800, height: 600 })
    // load a local HTML file
    win.loadURL(`file:///index.html`)
})
index.html
<!DOCTYPE html>
<html>
<head>
  <title>Hello TypeScript</title>
</head>
<body>
  <h1>Hello TypeScript</h1>
</body>
</html>

コンパイルしたら以下のようなエラーが出てしまった。

Namespace 'NodeJS' has no exported member 'EventEmitter'.

Node.js標準の型定義がないように見えるので

typings install dt~node/v6 --save --global

を実行したところ無事コンパイル完了。

3. 実行する

作っていなかったpackage.jsonを作成してから実行。

npm init -y
electron .

うまくいっていればこんな感じにウィンドウが表示される。
WS000421.JPG

一応ディレクトリ構成も書いておく

.
..
typings
index.html
main.js
main.ts
package.json
typings.json

4. パッケージングする

electron-packagerをインストールしてパッケージング
上でやったのがhelloディレクトリに置かれている

npm install -g electron-packager
electron-packager ./hello hello --platform=all --arch=all --electron-version=1.6.10

とりあえずallでやってしまったが
パッケージを作るためのファイルダウンロードが増えて時間をとられてしまった。
--platform=win32 --arch=x64 でよかったね。

パッケージングしたものを起動してもなぜかうまく動かないので2に続く

5
6
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
5
6