๊ณผ์ ์ค์์ ์นํฉ์ผ๋ก ๋ฒ๋ค๋งํ๋ ๊ณผ์ ๊ฐ ์์๋ค.
๊ณ์ ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ์๋ชป์จ์ ๊ทธ๋ฐ์ง
์ด๋ป๊ฒ ์์ฑํด์ผํ ์ง ์ ๋ฅผ ๋จน๋ค๊ฐ ๋๋์ด ์ฑ๊ณตํ๋ค. ใ
ใ
package.jsom ํ์ผ์ ์ค์ ํด์ฃผ์๋ค. (์ ํ์ผ ์๋ค๋ฉด ์๋ต)
{
"name": "fe-sprint-my-agora-states",
"version": "1.0.0",
"description": "",
"main": "./src/script.js", // ์ ๋จน์ ๋ถ๋ถ.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack" // ์นํฉ ์ฌ์ฉํ๊ธฐ ์ํด์ ์์ฑํ๊ธฐ.
},
main์๋ ์์์ง์ ์ ์
๋ ฅํ๋ฉด ๋๋ ๋ฏํ๋ค.
html ํ์ผ์ ์์ฑํด์ผ ํ๋๊ฑด๊ฐ? ์๊ฐํ๋๋ฐ ๋ฉ์ธ์์น์ js ํ์ผ๋ก ์์ฑํ๋ฉด ๋๋ค.
npm install -D webpack webpack-cli // ์นํฉ ์ค์น
npm i -D css-loader style-loader // ์คํ์ผ ๋ก๋ ์ค์น
npm i -D html-webpack-plugin // html ์นํฉ ํ๋ฌ๊ทธ์ธ ์ค์น
const path = require("path");
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: "./src/script.js", // ์์์ง์ ํ์ผ ๊ฒฝ๋ก ์ค์
output: {
path: path.resolve(__dirname, "dist"),
filename: "app.bundle.js",
},
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
exclude: /node_modules/,
},
],
},
plugins: [new HtmlWebpackPlugin({
template: path.resolve(__dirname, "src", "index.html")
})]
};
require('./style.css');
์ถ๊ฐ๋๋ ์ฐ์ script ํ์ผ์ require ์์ฑํด์ฃผ์ง ์์์ dist/index.html ํ์ผ์ด ๊ฒฝ๋ก๋ฅผ ์ ๋๋ก ๋ถ๋ฌ์ค์ง ๋ชปํด์ ํค๋งธ๋ค ๐
npx webpack
์คํํ๋ฉด dist ํด๋ ์์ฑ > app.bundle.js / index.html ํ์ผ์ด ์์ฑ๋์๋ค.
์ฒ์์๋ ๋นํฉ์ค๋ฌ์์ dist ํด๋ ๋ด์ index.html ํ์ผ๋ ์์ ํด์ผํ๋? ์๊ฐํ๋๋ฐ ์๊ฐํด๋ณด๋ ์ ๊ฑฐ๋ ๋ฒ๋ค๋ง ํ์ ์์ฑ๋๋ ์ค๋ฐ ๋น์ฐํ ์์ ํ ๊ฒ ์๋.. ์ถ์๋ค.
src/index.html
dist.index.html ํ์ผ์ด ๋๋ค ๋์ผํ๊ฒ ์๋๋๋ฉด ์ฑ๊ณต์ด๋ค.
"main": "./src/script.js", ์ด ๋ถ๋ถ์ ๋ฃจํธ ๊ฒฝ๋ก๋ก ์ธ๋ถ ๊ฒฝ๋ก๊น์ง ์ ์์ฑํด์ฃผ๊ณ .
css ๋ถ๋ฌ์ค๋ ค๋ฉด ๊ผญ require ์ ์ฐ์..