Table of contents
2. Building your First App - Setting up your project
Initializing your npm project
initalizing an npm package
Start by creating a folder and initializing an npm package within it withย npm init.
mkdir my-electron-app
cd my-electron-app
npm init
์คํํ๋ฉด
This command will prompt you to configure some fields in your package.json.
package.json์ด ์์ฑ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. (โmainโ: index.js โ main.js๋ก ๋ฐ๊ฟ์ค๋ค.)
AS-IS
TO-BE
install electron โsave-dev
You should also now have aย node_modulesย folder containing the Electron executable, as well as aย package-lock.json
ย lockfile that specifies the exact dependency versions to install.
npm install electron โsave-dev
์คํํ๋ฉด
node_modules, package-lock.json
, package.json
์ฝ๋์ devDependencies๊ฐ ์ถ๊ฐ๋์๋ค!
Running an Electron app
package.json
ํ์ผ์ ๋ฉ์ธ ์คํฌ๋ฆฝํธ๋ ์ฑ์ด ์์๋ ๋ ์คํ๋๋ ์ค์ฌ์ ์ธ ์ฝ๋๋ฅผ ๋ด๊ณ ์์ผ๋ฉฐ, ์ด ์ฝ๋๋ ์ฃผ๋ก ๋ฉ์ธ ํ๋ก์ธ์ค์์ ์ฑ์ ํต์ฌ์ ์ธ ๊ธฐ๋ฅ์ ๋ด๋นํ๋ค. ์ด ํ๋ก์ธ์ค๋ Node.js๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ฉฐ, ์ฑ์ ์ ๋ฐ์ ์ธ ๋์์ ์กฐ์ ํ๊ณ ๊ด๋ฆฌํ๋ ์ญํ ์ ์ํํ๋ค.
๋ฉ์ธ ํ๋ก์ธ์ค ์ง์ ์ ์ด ์ฌ๋ฐ๋ฅด๊ฒ ๊ตฌ์ฑ๋์๋์ง ํ์ธํ๊ธฐ(์๋ต๊ฐ๋ฅ)
Electron์ ๋ฉ์ธ ํ๋ก์ธ์ค๋ ์ผ๋ฐ์ ์ธ Node.js ํ๊ฒฝ๊ณผ ์ ์ฌํ๊ฒ ๋์ํ๋ค. ๋ฐ๋ผ์ electron ๋ช ๋ น์ ์ฌ์ฉํ์ฌ ๋ฉ์ธ ํ๋ก์ธ์ค ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๋ฉด, Node.js์์ ์ฌ์ฉ๋๋ ๋ชจ๋์ด๋ ๊ธฐ๋ฅ๋ค์ Electron ๋ฉ์ธ ํ๋ก์ธ์ค์์๋ ํ์ฉํ ์ ์๋ค.
//main.js
// Create a main.js file in the root folder of your project
console.log('Hello from Electron')
//package.json
//script ์์ start ์ฝ๋๋ฅผ ์ถ๊ฐ.
"scripts": {
"start": "electron .",
"test": "echo \"Error: no test specified\" && exit 1"
},
๊ฐ๋ฐ ๋ชจ๋์์ Electron์ ์คํํ๋ ค๋ฉด ํฐ๋ฏธ๋์์ ๋ค์๊ณผ ๊ฐ์ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉ
npm run start
์คํํ๋ฉด
Loading a web page into a BrowserWindow
Electron์์ ๊ฐ ์ฐฝ์ ๋ก์ปฌ HTML ํ์ผ ๋๋ ์๊ฒฉ ์น ์ฃผ์์์ ๋ก๋๋ ์ ์๋ ์น ํ์ด์ง๋ฅผ ํ์ํ๋ค. ํ๋ก์ ํธ์ ๋ฃจํธ ํด๋์ index.html
ํ์ผ์ ๋ง๋ค์ด ๊ธฐ๋ณธ ์น ํ์ด์ง๋ฅผ ์์ฑํ์! ๊ทธ๋ผ, Electron BrowserWindow์ ๋ก๋ํ ์ ์๋ค.
์ฝ๋ ๋ณด๊ธฐ
//index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self'"
/>
<meta
http-equiv="X-Content-Security-Policy"
content="default-src 'self'; script-src 'self'"
/>
<title>Hello from Electron renderer!</title>
</head>
<body>
<h1>Hello from Electron renderer!</h1>
<p>๐</p>
</body>
</html>
์คํํ๋ฉด
main.js ์์ ํ๊ธฐ
์ฝ๋ ๋ณด๊ธฐ
//main.js
const { app, BrowserWindow } = require('electron')
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600
})
win.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
})
์ฝ๋ ์ค๋ช
Importing two Electron modules with CommonJS module syntax
- app: ์ ๋ฐ์ ์ธ ์ ํ๋ฆฌ์ผ์ด์ ์ด๋ฒคํธ ๋ผ์ดํ์ฌ์ดํด์ ์ ์ด
- BrowserWindow: ์ ํ๋ฆฌ์ผ์ด์ ์ฐฝ์ ์์ฑ๊ณผ ๊ด๋ฆฌ๋ฅผ ๋ด๋น
const { app, BrowserWindow } = require('electron')
Writing a reusable function to instantiate windows (์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ํจ์๋ฅผ ์์ฑํ์ฌ ์ฐฝ์ ์์ฑ)
- createWindow() ํจ์๋ ์๋ก์ด BrowserWindow ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ , ํด๋น ์ฐฝ์ ๊ตฌ์ฑ์ ์ค์ ํ๋ฉฐ(width, height, webPreferences ๋ฑ), index.html ํ์ผ์ ์ฐฝ์ ๋ก๋
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600
})
win.loadFile('index.html')
}
Calling your function when the app is ready
- Electron์์ BrowserWindow์ app ๋ชจ๋์ ready ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ํ์๋ง ์์ฑํ ์ ์๋ค. app.whenReady() API๋ฅผ ์ฌ์ฉํ๊ณ ํด๋น ํ๋ก๋ฏธ์ค๊ฐ ์ดํ๋๋ฉด createWindow()์ ํธ์ถํจ์ผ๋ก์จ ์ด ์ด๋ฒคํธ๋ฅผ ๊ธฐ๋ค๋ฆด ์ ์๋ค.
- ์ ํ๋ฆฌ์ผ์ด์ ์ด ์ด๊ธฐํ๋๊ณ ํ์ํ ์ค์ ์ด ์๋ฃ๋ ํ์ ์ฐฝ์ ์์ฑํ๋๋ก ๋ณด์ฅ
app.whenReady().then(() => {
createWindow()
})
์ด์ , Electron ์ ํ๋ฆฌ์ผ์ด์ ์ ์์ ๋ช ๋ น์ ์คํํ๋ฉด ์ฑ๊ณต์ ์ผ๋ก ์น ํ์ด์ง๊ฐ ํ์๋๋ ์ฐฝ์ด ์ด๋ฆด ๊ฒ์ด๋ค!
์คํํ๋ฉด
Managing your appโs window lifecycle
[Windows ๋ฐ Linux] ์ผ๋ฐ์ ์ผ๋ก ๋ชจ๋ ์ฐฝ์ ๋ซ์ผ๋ฉด ์ ํ๋ฆฌ์ผ์ด์
์ด ์์ ํ ์ข
๋ฃ๋๋ค. Electron ์ฑ์์ ์ด ํจํด์ ๊ตฌํํ๋ ค๋ฉด app ๋ชจ๋์ window-all-closed
์ด๋ฒคํธ๋ฅผ ๊ฐ์งํ๊ณ , ์ฌ์ฉ์๊ฐ macOS์์ ์์
์ค์ด ์๋ ๊ฒฝ์ฐ app.quit()์ ํธ์ถํ์ฌ ์ฑ์ ์ข
๋ฃํ๋๋ก ํ๋ค.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
[macOS] macOS์์๋ ์ฐฝ์ ๋ซ์๋ ์ฑ์ด ์์ ํ ์ข ๋ฃ๋์ง ์๊ณ , macOS ์ฑ์ ์ผ๋ฐ์ ์ผ๋ก ์ฐฝ์ด ์ด๋ ค ์์ง ์์๋ ๊ณ์ ์คํ๋๋ค. ์ฐฝ์ด ์์ ๋ ์ฑ์ ํ์ฑํํ๋ฉด ์ ์ฐฝ์ด ์ด๋ฆฌ๋๋ก ํด์ผํ๋ค. ์ฐฝ์ด ์ค๋น๋๊ธฐ ์ ์๋ ์ฐฝ์ ๋ง๋ค ์ ์์ผ๋ฏ๋ก, ์ฑ์ด ์ด๊ธฐํ๋ ํ์๋ง activate ์ด๋ฒคํธ๋ฅผ ๊ฐ์งํ๋๋ก ํด์ผํ๋ค. ๊ธฐ์กด์ whenReady() ์ฝ๋ฐฑ ๋ด์์๋ง activate ์ด๋ฒคํธ๋ฅผ ์์ ํ๋๋ก ํ๋ค.
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})