05-06 06:36
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[WebApp / Electron] Creating Desktop Apps with Electron 본문

Programming/Web App

[WebApp / Electron] Creating Desktop Apps with Electron

cinema4dr12 2016. 6. 29. 00:18

이번 글은 node.js(nodejs.org) 기반 Cross Platform Desktop App을 만드는 도구인 Electron(http://electron.atom.io)을 이용하여 기본앱(Hello World)을 만드는 방법에 대하여 알아보도록 하겠습니다.


우선 App을 만들 폴더를 하나 만듭니다(비어 있어야 합니다).

Terminal에 다음 명령을 실행하여 App 초기화를 합니다(node.js가 설치되어 있어야 합니다).


$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (example-01)
version: (1.0.0)
description: electron example
entry point: (index.js)
test command:
git repository:
keywords:
author: cinema4dr12
license: (ISC)
About to write to {YOUR_ELECTRON_APP_PATH}/package.json:

{
  "name": "helloWorld",
  "version": "1.0.0",
  "description": "electron example",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "cinema4dr12",
  "license": "ISC"
}


Is this ok? (yes)


App 폴더에 package.json 파일이 생성됩니다. 이제 Node Package Manager(npm)를 통해 Electron App의 Node.js 패키지를 설치합니다.


$ sudo npm i electron-prebuilt --save-dev


이제 App의 진입접이 될 JaveScript 파일명을 package.json에 입력합니다. 기존의 "scripts" > "test"는 삭제합니다.


{ "name": "helloWorld", "version": "1.0.0", "description": "electron example", "main": "index.js", "scripts": { "start": "electron main.js" }, "author": "cinema4dr12", "license": "ISC", "devDependencies": { "electron-prebuilt": "^1.2.5" } }


"main.js" 파일을 다음과 같이 작성하되, 이 파일은 App Project 폴더의 Root 경로에 저장해야 합니다.


main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
 
// 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 mainWindow
 
function createWindow () {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 800, height: 600})
 
  // and load the index.html of the app.
  mainWindow.loadURL('file://' + __dirname + '/index.html')
 
  // Open the DevTools.
  mainWindow.webContents.openDevTools()
 
  // Emitted when the window is closed.
  mainWindow.on('closed'function () {
    // 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.
    mainWindow = 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'function () {
  // On OS X 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'function () {
  // On OS X 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 (mainWindow === 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.
cs


Line 16에서 로딩할 URL을 지정하는데 __dirname은 node.js의 App의 Root 경로를 의미하는 예약어(keyword)입니다. 따라서, Line 16은 Electron App의 Root 경로에 있는 index.html 파일을 로딩하는 것입니다. Line 19는 브라우저의 단축키 Ctrl + Shift + I를  눌렀을 때 나오는 개발자 툴을 여는 명령입니다. 배포 단계에서는 이를 주석처리하거나 삭제하도록 합니다. 이 파일을 다음과 같이 작성합니다.


index.html

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Hello World!</title>
    </head>
 
    <body>
        <h1>Hello World!</h1>
    </body>
</html>
cs


Terminal에 다음과 같이 명령어를 입력하면 Electron App이 실행됩니다.


$ npm start

> helloWorld@1.0.0 start {YOUR_ELECTRON_APP_PATH}/helloWorld
> electron main.js


Comments