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

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

by KASSID 2023. 5. 2.

๋ชฉ์ฐจ

    728x90

    2023.05.01 - [๐ŸŒŒ | WEB DEV/Electron] - [Electron] 0. Electron์ด๋ž€? & ์ดˆ๊ธฐ์„ค์ •

    Electron์€ JS๊ธฐ๋ฐ˜ ํ”„๋ ˆ์ž„์›Œํฌ์ด๊ธฐ ๋•Œ๋ฌธ์— React ์—ญ์‹œ ํ•จ๊ป˜ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์ €๋ฒˆ ํฌ์ŠคํŠธ์—์„œ ์ง„ํ–‰ํ•œ ํ”„๋กœ์ ํŠธ์— React๋ฅผ ํƒ‘์žฌํ•ด๋ณด์ž!

     

    1. React ํŒจํ‚ค์ง€ ์ถ”๊ฐ€ํ•˜๊ธฐ

    npm install --save react react-dom

     

    ์œ„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋œ ๊ฒฝ์šฐ package.json์—๋Š” ์ด์ฒ˜๋Ÿผ ์ถ”๊ฐ€๊ฐ€ ๋œ๋‹ค.

    "dependencies": {
      "react": "^18.2.0",
      "react-dom": "^18.2.0",
    }

     

     

    2. ์†Œ์ŠคํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

    main_react๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ jsํŒŒ์ผ์„ ์ƒ์„ฑํ•˜์˜€๋‹ค. (์ด๋ฆ„์€ ์ž„์˜ ์ž‘์„ฑ)

     

    main_react.js

    import React from 'react';
    import ReactDom from 'react-dom';
    
    ReactDom.render(<h1>Hello React App</h1>, document.getElementById('root'));

     

     

    3. ์›นํŒฉ์šฉ ํŒจํ‚ค์ง€ ์ถ”๊ฐ€

    npm install --save-dev @babel/core @babel/preset-env @babel/preset-react babel-loader css-loader style-loader sass-loader sass webpack webpack-cli

    ๋ฆฌ์•กํŠธ๋ฅผ ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ ์›นํŒฉ์—์„œ ๊ตฌ๋™ํ•˜๊ธฐ ์œ„ํ•ด์„œ

    ๊ด€๋ จ ํŒจํ‚ค์ง€๋“ค์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

     

     

    4. webpack.common.js ์ถ”๊ฐ€

    webpack ๊ตฌ๋™์„ ์œ„ํ•œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

    const path = require('path');
    
    module.exports = {
      mode: 'development',
      entry: './src/js/main_react.js',
      devtool: 'inline-source-map',
      target: 'electron-renderer',
      // ํ”„๋กœ์ ํŠธ ๋‚ด ๋ชจ๋“ˆ ์ฒ˜๋ฆฌ ๋ฐฉ์‹ ์„ค์ •
      module: {
        rules: [
          	// ํ…Œ์ŠคํŠธ ํ†ต๊ณผํ•  ๋ชจ๋“ˆ ์ •์˜
          {
            // ๋กœ๋”
            test: /\.js$/,
            // ์ œ์™ธํ•  ๋ชจ๋“ˆ ์ •์˜
            exclude: /node_modules/,
            // ๋กœ๋“œ์ค‘์ธ ๋ชจ๋“ˆ ์„ค๋ช…
            use: {
              loader: 'babel-loader',
              // ๋กœ๋” ์˜ต์…˜
              options: {
                presets: [[
                  '@babel/preset-env', {
                    targets: {
                      esmodules: true
                    }
                  }],
                  '@babel/preset-react']
              }
            }
          },
          {
          	// css ๊ด€๋ จ
            test: [/\.s[ac]ss$/i, /\.css$/i],
            use: [
              // Creates `style` nodes from JS strings
              'style-loader',
              // Translates CSS into CommonJS
              'css-loader',
              // Compiles Sass to CSS
              'sass-loader',
            ],
          }
        ]
      },
      // ์„ค์ • ์˜ต์…˜
      resolve: {
      	// ํ•ด์„ํ•  ํŒŒ์ผ ์„ค์ •
        extensions: ['.js'],
      },
      // ์ถœ๋ ฅ ์„ค์ •
      output: {
        // ํŒŒ์ผ๋ช…
        filename: 'app.js',
        // ํŒŒ์ผ๊ฒฝ๋กœ
        path: path.resolve(__dirname, 'build', 'js'),
      },
    };

     

     

    6. package.json์˜ scripts์— watch ์ถ”๊ฐ€

    "scripts": { 
      "watch": "webpack --config webpack.common.js --watch",
    }

    scripts์— watch๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

     

     

    7. ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰

    npm run watch

    ์Šคํฌ๋ฆฝํŠธ์— ์ •์˜ํ•œ watch ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด build/js/app.js ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

     

     

    8. index.html์— app.js ์ถ”๊ฐ€

    index.html ํŒŒ์ผ์˜ body ํƒœ๊ทธ ์•ˆ์— app.js๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

    <body>
      <div id="root"></div>
      <script src="../../build/js/app.js"></script>
    </body>

    ์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ app.js๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

     

     

    9. ์‹œ์ž‘

    npm start

    ์‹œ์ž‘ ๋ช…๋ น์–ด๋กœ ์•ฑ์„ ๊ตฌ๋™ํ•œ๋‹ค.

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

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

    ๋Œ“๊ธ€