設定 - WebStormとの連携

提供:MochiuWiki - SUSE, Electronic Circuit, PCB
ナビゲーションに移動 検索に移動

概要

WebStormを使用して、ワークフローを設定する方法、コーディング支援を有効にする方法、
Electronソフトウェアを実行およびデバッグする手順を記載する。


事前準備

以下のソフトウェアがインストールされているものとする。

  • Node.js
  • WebStorm



Electronのインストール(WebStormの使用)

  1. WebStormを起動して、[Create New Project]を選択する。
  2. [New Project]画面左にある[Node.js]を選択して、[Location:]項目にプロジェクト名を入力する。
    [Node Interpreter:]プルダウンと[Package Manager:]プルダウンには、任意のバージョンのNodeとNPMを選択する。>
    ※プロジェクト名には、大文字のアルファベットは使用できないので注意すること。
  3. [Create]ボタンを押下して、プロジェクトを生成する。

  4. プロジェクト画面から、[File] - [Settings...]を選択して、設定画面を開く。
  5. 設定画面左から、[Language & Frameworks] - [Node.js and NPM]を選択する。
  6. 画面左の下部にある[+]ボタンを押下して、[Available Packages]画面を開く。
  7. [Available Packages]画面上にある検索欄に"electron"と入力して、一覧に表示された"electron"を選択する。
  8. [Available Packages]画面下にある[INSTALL PACKAGE]ボタンを押下して、electronをインストールする。
  9. "Package 'electron' installed successfully"と表示されて、インストールが完了する。

  10. package.jsonファイル内にdependenciesが自動的に追記されて、electronという項目が記述される。
    同様に、node_modulesディレクトリにelectronが追加されていることが確認できる。



コード補完機能の有効化

Electronのコーディング支援機能は、Electron 1.6.9以降、electron.d.tsファイル(TypeScript定義ファイル)を介して提供される。
このファイルは、<Electronのプロジェクトディレクトリ>/node_modules/electronディレクトリに存在する。

コード補完機能を有効にするには、以下の手順を行う。

  1. プロジェクト画面から、[File] - [Settings...]を選択して、設定画面を開く。
  2. 設定画面左から、[Language & Frameworks] - [Node.js and NPM]を選択する。
  3. [Enable coding assistance]チェックボックスにチェックを入力する。



package.jsonの設定

Electronをインストールした場合、package.jsonファイル内には、エントリポイントがindex.jsと記述されているが、
Electronの公式Webサイトでは、main.jsが標準のようなので、この記述を編集する。

 # package.jsonファイル
 
 # 変更前
 "main": "index.js",
 
 # 変更後
 "main": "main.js",


次に、package.jsonファイル内の"scripts"項目において、以下の設定を追記する。

# Electronをローカルインストールしている場合
"run": "./node_modules/.bin/electron .",

# Electronをグローバルインストールしている場合
"run":"electron ."



サンプルコード

index.htmlファイルとmain.jsファイルを作成する。

Electronの公式Webサイトから、基本となるindex.htmlファイルの内容とmain.jsファイルの内容をコピーする。
以下のサンプルコードは、Writing Your First Electron Appから引用している。

 // main.jsファイル
 
 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,
       webPreferences:
       {
          nodeIntegration: true
       }
    })
 
    // and load the index.html of the app.
    win.loadFile('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ファイル
 
 <!DOCTYPE html>
 <html>
    <head>
       <meta charset="UTF-8">
       <title>Hello World!</title>
    </head>
    <body>
       <h1>Hello World!</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>



デバッグの設定(不要)

  1. WebStormのプロジェクト画面右上にある[ADD CONFIGURATION...]プルダウンから、[Run/Debug Configurations]を選択する。
  2. [Run/Debug Configurations]画面左にある[Templates] - [Node.js]を選択する。

  3. 画面右に表示されている以下の項目を設定する。
    • [Node interpreter]項目の右にある[...]を選択する。
      [Node.js interpreter]画面左下にある[+]ボタンを押下して、[Add Local...]を選択する。
      プロジェクトディレクトリにあるnode_moduleディレクトリから、electronファイル(Windowsの場合は、electron.cmd)を選択する。
    • [Node parameters]項目に、.を入力する。
    • [Working directory]項目は変更しない。
    • [JavaScript file]項目に、main.jsを入力する。
    • [Application parameters]項目に、--remote-debugging-port=9222を入力する。
    • [Environment variables]項目は変更しない。

  4. [OK]ボタンを押下して、[Run/Debug Configurations]画面を閉じる。


プロジェクト画面右上にある虫アイコンを選択して、デバッグを実行する。