๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/Electron

[Electron] 0. Electron์ด๋ž€? & ์ดˆ๊ธฐ์„ค์ •

by KASSID 2023. 5. 1.

๋ชฉ์ฐจ

    728x90

    JS ๊ธฐ๋ฐ˜์œผ๋กœ ํฌ๋กœ์Šคํ”Œ๋žซํผ ๋ฐ์Šคํฌํ†ฑ ์•ฑ์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์ธ

    Electron์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž!

     

    1. ์†Œ๊ฐœ

    Electron์€ JS, HTML, CSS๋ฅผ ํ™œ์šฉํ•ด์„œ ํฌ๋กœ์Šคํ”Œ๋žฉํผ ๋ฐ์Šคํฌํ†ฑ ์•ฑ์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.

    2013๋…„ Atom Editor๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์‹œ์ž‘๋œ Atom Shell์—์„œ ์‹œ์ž‘ํ•˜์˜€๊ณ  2015๋…„ ๊ทธ ๋ช…์นญ์ด ํ˜„์žฌ์˜ Electron์œผ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค.

     

    Electron์˜ ์•„ํ‚คํ…์ณ๋Š”

    ๋ฐฑ์—”๋“œ์˜ ๊ฒฝ์šฐ Node.js, ํ”„๋ก ํŠธ์—”๋“œ์˜ ๊ฒฝ์šฐ Chromium ์ด๋ผ๋Š” ๊ตฌ๊ธ€ Chrome ๋ธŒ๋ผ์šฐ์ €์˜ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ์˜คํ”ˆ์†Œ์Šค ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

    ๋”ฐ๋ผ์„œ Web ๊ธฐ๋ฐ˜์˜ ๊ธฐ์ˆ ์€ Chromium์—์„œ ์ฒ˜๋ฆฌํ•˜๊ณ  OS ๊ธฐ๋ฐ˜ ๊ธฐ๋Šฅ์€ Node.js์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค.

     

    ์ด๋ฅผ ์ด์šฉํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ๋Š” Atom Editor, VSCode, Slack, Discord ๋“ฑ์ด ์žˆ๋‹ค!

     

    Electron์˜ ์žฅ์ 

    1) ๊ธฐ์กด์˜ ์›น๊ธฐ์ˆ ์„ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. React, Vue ๋“ฑ ๋‹ค์–‘ํ•œ js๊ธฐ๋ฐ˜ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    2) ํ•œ ๊ฐœ์˜ ์ฝ”๋“œ๋กœ Window, MacOS, Linux ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ํ”Œ๋žซํผ์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•ฑ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

    3) NPM์„ ์ด์šฉํ•ด ๋‹ค์–‘ํ•œ node package๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     


    2. ์ดˆ๊ธฐ์„ค์ •

    1) Node.js ์„ค์น˜

    ์ผ๋ ‰ํŠธ๋ก  ๊ฐœ๋ฐœ์„ ์œ„ํ•ด์„œ๋Š” Node.js๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

    https://nodejs.org/en/download

    node -v
    npm -v

     ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•œ ํ›„ ๋ฒ„์ „ํ™•์ธ์„ ํ†ตํ•ด ์„ค์น˜๋ฅผ ํ™•์ธํ•œ๋‹ค.

     

     

    2) ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ

    ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•ด๋‹น ์œ„์น˜์— ์ ‘๊ทผํ•œ๋‹ค.

     

     

    3) ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

    npm init

    ์œ„์˜ ๋ช…๋ น์–ด๋ฅผ ํ„ฐ๋ฏธ๋„์— ์ž…๋ ฅํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

    ํ”„๋กœ์ ํŠธ๋ช…, ๋ฒ„์ „, ์„ค๋ช…, ์—”ํŠธ๋ฆฌํฌ์ธํŠธ, ์ž‘์„ฑ์ž, ๋ผ์ด์„ผ์Šค ๋“ฑ์„ ์„ค์ •ํ•œ๋‹ค.

     

    ์„ค์ •์„ ํ•˜๋ฉด package.json ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค.

    {
      "name": "electron-app",
      "version": "1.0.0",
      "description": "first-app",
      "main": "main.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "Kassid",
      "license": "ISC"
    }

    main : ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘์ ์„ ์˜๋ฏธํ•˜๋Š” ์—”ํŠธ๋ฆฌํฌ์ธํŠธ์ด๋‹ค. (์ƒ๋Œ€ ๊ฒฝ๋กœ ์ž…๋ ฅ ๊ฐ€๋Šฅ)

     

     

    4) Electron ํŒจํ‚ค์ง€ ์ถ”๊ฐ€

    npm install --save-dev electron

    electron์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ํŒจํ‚ค์ง€๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

     

    ์ž˜ ์„ค์น˜๋œ ๊ฒฝ์šฐ ์•„๋ž˜์˜ ๋‚ด์šฉ์ด ์ถ”๊ฐ€๋œ๋‹ค.

    "devDependencies": {
      "electron": "^24.1.3"
    }

     

    ๋˜ํ•œ package-lock.json ํŒŒ์ผ๋„ ์ƒ์„ฑ๋œ๋‹ค.

     

     

    5) ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํŒŒ์ผ ์ƒ์„ฑ

    .env / .env.development / .env.production ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

     

    - .env

    HOST=localhost
    PORT=3000
    BROWSER=none

     

    - .env.development

    NODE_ENV=development

     

    - .env.production

    NODE_ENV=production
    GENERATE_SOURCEMAP=false

     

     

    6) index.html & main.js ์ƒ์„ฑ

    ์‹คํ–‰ ์Šคํฌ๋ฆฝํŠธ ๋ฐ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

    ํฌ์ŠคํŒ…์—์„œ๋Š” ํŒŒ์ผ์˜ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋”ฐ๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.

    ์ด๋•Œ ์ฃผ์˜ํ•  ์ ์€ ์œ„์—์„œ ์„ธํŒ…ํ–ˆ๋˜ package.json์—์„œ์˜ entry point๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผํ•œ๋‹ค.

    "main": "src/js/main.js",

     

     

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Hello World</title>
    </head>
    <body>
      <h1>Hello World!</h1>
      This is Kassid.
    </body>
    </html>

     

    main.js

    const { app, BrowserWindow } = require('electron')
    const path = require('path')
    const url = require('url')
    
    function createWindow () {
      const win = new BrowserWindow({
        width: 800,
        height: 600,
    
        // webPreferences: {
        //   preload: path.join(__dirname, 'preload.js')
        // }
      })
      // win.loadFile('../html/index.html')
      const startUrl = process.env.ELECTRON_START_URL || url.format({
        pathname: path.join(__dirname,'..', 'html','index.html'),
        protocol: 'file:',
        slashes: true
      });
      
      win.loadURL(startUrl);
    }
    
    app.whenReady().then(() => {
      createWindow()
    
      //for MacOS
      app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
      })
    })
    //for Window, Linux
    //when close 
    app.on('window-all-closed', function () {
      if (process.platform !== 'darwin') app.quit()
    })

    ์œ„์˜ ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ๋œฏ์–ด๋ณด์ž!

     

    (1) ๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ค๊ธฐ

    const { app, BrowserWindow } = require('electron')

    html ํŒŒ์ผ์„ ์‘์šฉํ”„๋กœ๊ทธ๋žจ ์ฐฝ์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ์œ„ํ•œ 2๊ฐ€์ง€ Electron ๋ชจ๋“ˆ์ด๋‹ค.

    - app : ์‘์šฉํ”„๋กœ๊ทธ๋žจ์˜ ์ด๋ฒคํŠธ ์ˆ˜๋ช…์ฃผ๊ธฐ ์ œ์–ด ๋ชจ๋“ˆ

    - BrowserWindow : ์‘์šฉํ”„๋กœ๊ทธ๋žจ ์ฐฝ ์ƒ์„ฑ ๋ฐ ๊ด€๋ฆฌ ๋ชจ๋“ˆ

     

    Node.js์—์„œ ์‹คํ–‰๋˜๋ฏ€๋กœ ์œ„์ฒ˜๋Ÿผ CommonJS ๋ชจ๋“ˆ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

     

    (2) ๋ฉ”์ธ์ฐฝ ์ƒ์„ฑ

    function createWindow () {
      const win = new BrowserWindow({
        // ์ฐฝ ํฌ๊ธฐ ์„ค์ •
        width: 800,
        height: 600,
        
        // ์‚ฌ์ „์— ๋กœ๋“œํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์ด ๋กœ๋“œ๋ฅผ ํ•œ๋‹ค.
        // webPreferences: {
        //   preload: path.join(__dirname, 'preload.js')
        // }
      })
      
      // ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๊ธฐ
      
      // 1. ์ง์ ‘๊ฐ€์ ธ์˜ค๊ธฐ
      // win.loadFile('../html/index.html')
      
      // 2. url๋กœ ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๊ธฐ
        // ํ™˜๊ฒฝ๋ณ€์ˆ˜์— URL ์กด์žฌํ•  ๊ฒฝ์šฐ์™€ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ƒ๋Œ€ ๊ฒฝ๋กœ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
      const startUrl = process.env.ELECTRON_START_URL || url.format({
        pathname: path.join(__dirname,'..', 'html','index.html'),
        protocol: 'file:',
        slashes: true
      });
      
      // ์‹œ์ž‘ ํฌ์ธํŠธ ์‹คํ–‰
      win.loadURL(startUrl);
    }

     

    (3) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ๋ฐ ์ข…๋ฃŒ

    // ์ดˆ๊ธฐํ™”๊ฐ€ ๋๋‚˜๊ฒŒ ๋˜๋ฉด ์‹คํ–‰
    app.on('ready', () => {
      // ๋ฉ”์ธ ์ฐฝ ์ƒ์„ฑ
      createWindow();
    });
    
    // ๋ชจ๋“  ์ฐฝ์ด ๋‹ซํžˆ๋ฉด ์ž๋™์œผ๋กœ ์•ฑ ์ข…๋ฃŒ
    app.on('window-all-closed', () => {
      app.quit();
    });

     

    7) ํŒจํ‚ค์ง•๊ณผ ๋ฐฐํฌ๋ฅผ ์œ„ํ•œ ํŒจํ‚ค์ง€ ์„ค์น˜

    npm install --save-dev @electron-forge/cli
    npx electron-forge import

    ํŒจํ‚ค์ง•๊ณผ ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด์„œ Electron Forge๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์œ„ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด package.json์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

    {
      "name": "electron-app",
      "version": "1.0.0",
      "description": "first-app",
      "main": "src/js/main.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "electron-forge start",
        "package": "electron-forge package",
        "make": "electron-forge make"
      },
      "author": "Kassid",
      "license": "ISC",
      "devDependencies": {
        "@electron-forge/cli": "^6.1.1",
        "@electron-forge/maker-deb": "^6.1.1",
        "@electron-forge/maker-rpm": "^6.1.1",
        "@electron-forge/maker-squirrel": "^6.1.1",
        "@electron-forge/maker-zip": "^6.1.1",
        "electron": "^24.1.3"
      },
      "dependencies": {
        "electron-squirrel-startup": "^1.0.0"
      }
    }

     

     

    8) ์Šคํฌ๋ฆฝํŠธ ์ถ”๊ฐ€

    package.json์˜ scripts์— ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด๋ณด์ž.

    "electron": "electron ."
    "electron:window": "SET ELECTRON_START_URL=http://localhost:3000 && npm electron",
    "electron:xos": "ELECTRON_START_URL=http://localhost:3000 npm electron",
    
    "prebuild": "react-scripts test --watchAll=false",
    "build": "react-scripts build",
    
    "deploy:xos": "electron-builder --mac",
    "deploy:win32": "electron-builder --win --ia32",
    "deploy:win64": "electron-builder --win --x64",
    "deploy:win": "yarn deploy:win32 && yarn deploy:win64",
    
    "test": "react-scripts test",
    
    "package": "electron-forge package",
    "make": "electron-forge make"

    electron ์‹คํ–‰ ๋ช…๋ น์–ด, build ๋ช…๋ น์–ด, OS๋ณ„ builder ๋ช…๋ น์–ด, ํŒจํ‚ค์ง• ๋ฐ ๋ฐฐํฌ์— ๋Œ€ํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

     

     

    9) ์‹คํ–‰

    npm start

    ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋œ๋‹ค.

     

     

     

     

     

    '๐ŸŒŒ | WEB DEV > Electron' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    [Electron] 1. Electron์— React ํƒ‘์žฌํ•˜๊ธฐ!  (0) 2023.05.02

    ๋Œ“๊ธ€