본문 바로가기
Basic Development/Electron

3. Adding webpack-package and practice

by 공기팡수 2023. 12. 31.

1. Adding the webpack-package

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

 

2. Make 'webpack.common.js' in root directory and write through following code

<make file>  (through visual code)

shell
close
code webpack.common.js

<webpack.common.js>

javascript
close
const path = require('path'); module.exports = { ​​mode: 'development', ​​entry: './src/js/main.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'] ​​​​​​​​​​} ​​​​​​​​} ​​​​​​}, ​​​​​​{ ​​​​​​​​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'), ​​}, };

 

3. modifying file

<package.json>

html
close
​​"scripts": { ​​​​"start": "electron-forge start", ​​​​"watch": "webpack --config webpack.common.js --watch", // modified ​​​​"test": "echo \"Error: no test specified\" && exit 1", ​​​​"package": "electron-forge package", ​​​​"make": "electron-forge make" ​​},

 

4. Run watch (on the first Terminal window)

shell
close
npm run watch

 

5. modifying file

<index.html>

html
close
​​<body> ​​​​<script src="./build/js/app.js"></script> // modified ​​​​<div id="root"></div> ​​</body>

 

 

6. Run start (on the second Terminal window)

shell
close
npm run start

 

Result of Executing

7. Make React component and modifying some file

<make file> (through visual code)

shell
close
code src/js/app.js

<app.js>

javascript
close
import React from 'react'; export default function App() { ​​​​return( ​​​​​​​​<h1>I am App Component</h1> ​​​​) }

</src/js/main.js>

html
close
import React from 'react'; import ReactDom from 'react-dom'; import App from './app.js'; // modified ReactDom.render(<h2>Hello React App</h2>, document.getElementById('root')) ReactDom.render(<App/>, document.getElementById('root1')) // modified

<main.html>

html
close
​​<body> ​​​​<script src="./build/js/app.js"></script> ​​​​<div id="root"></div> ​​​​<div id="root1"></div> // modified ​​</body>

- Reload (ctrl+R)

Result of Executing

8. Finally, compare it with your root directory