webpack 4.0 부터는 기본적으로 내장된 기본 설정을 사용한다.
기본 설정을 사용하여 webpack을 실행하면 entry 파일을 찾고 output 파일을 생성하며 JavaScript, CSS 및 이미지 파일을 처리하는 로더를 사용하여 번들링을 해준다.
./src/index.js
이다.module.exports = {
entry: './src/index.js'
}
./dist
이고 파일 이름은 main.js
이다.const path = require('path')
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'main.js'
}
}
const path = require('path')
module.exports = {
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'main.js'
},
module: {
rules: [{
test: /\.css$/,
use: ["style-loader", "css-loader"],
}]
}
}
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'main.js'
}
plugins: [
new HtmlWebpackPlugin({
template: "src/index.html",
filename: "index.html"
})
]
}
development
, production
, none
으로 설정하면 내장된 환경별 최적화를 활성할 수 있다.production
이다.module.exports = {
mode: 'development'
}
아래와 같이 WARNING이 뜨는게 신경쓰이면 mode를 development로 변경해주면 된다.
$npx webpack
asset main.js 224 bytes [emitted] [minimized] (name: main)
./src/index.js 62 bytes [built] [code generated]
./src/home/go.js 43 bytes [built] [code generated]
WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value.
Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
webpack 5.75.0 compiled with 1 warning in 156 ms
mkdir -p webpack_practice/src/users
cd webpack_practice
npm init -y
npm install webpack webpack-cli
npm install react react-dom
const React = require("react");
const ReactDOM = require("react-dom");
const { name } = require("./users/user.js");
const root = ReactDOM.createRoot(document.querySelector("#root"));
root.render(React.createElement("div", null, `Hello ${name}`));
src/users/user.js
module.exports = {
name: "cloudcoke",
};
npx webpack
<!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>Document</title>
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="./main.js"></script>
</body>
</html>
npm install webpack webpack-cli
npm install css-loader style-loader
import './loader.css'
src/loader.css
* {
margin: 0;
padding: 0;
background: blue;
}
const path = require("path");
module.exports = {
entry: "./src/loader.js",
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
],
},
output: {
filename: "loader_output.js",
path: path.join(__dirname, "dist"),
},
};
npx webpack --config webpack.config.js
<!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>Document</title>
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="./loader_output.js"></script>
</body>
</html>
npm install webpack webpack-cli
npm install html-webpack-plugin
<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>Document</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
src/plugin.js
const root = document.querySelector("#root");
root.innerHTML = "Hello World";
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/plugin.js",
plugins: [
new HtmlWebpackPlugin({
template: "src/plugin.html",
filename: "plugin.html",
}),
],
output: {
filename: "plugin_output.js",
path: path.join(__dirname, "dist"),
},
};
npx webpack --config webpack.config.js
npm install webpack webpack-cli
npm install babel-loader
npm install @babel/core
npm install @babel/preset-env @babel/preset-react
npm install react react-dom
import React from "react";
import ReactDOM from "react-dom";
import App from "./app.js";
const root = ReactDOM.createRoot(document.querySelector("#root"));
root.render(<App />);
src/app.js
import React from "react";
class App extends React.Component {
render() {
return <div>Webpack with Babel</div>;
}
}
export default App;
src/babel.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>Document</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/babel.js",
module: {
rules: [{ test: /\.(js|jsx)$/, exclude: /node_modules/, use: ["babel-loader"] }],
},
plugins: [
new HtmlWebpackPlugin({
template: "src/babel.html",
filename: "babel.html",
}),
],
output: {
filename: "babel_output.js",
path: path.join(__dirname, "dist"),
},
mode: "development",
};
npx webpack --config webpack.config.js