[웹]/WebPack
[WebPack] HtmlWebpackPlugin (Step4)
삼손스
2019. 11. 27. 16:19
따로 분리하여 bundle한 css파일과 js파일을 각각 html 파일에 link 태그와 script태그로 추가해줘야 합니다.
HtmlWebpackPlugin플러그인은 이것을 자동화해줍니다.
npm install --save-dev html-webpack-plugin
const HtmlWebpackPlugin = require('html-webpack-plugin');
const config = {
plugins: [
new HtmlWebpackPlugin({
template: './index.html'
})
]
}
또는
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
module.exports = {
mode: "development",
entry: "./src/index.js",
devtool: "inline-source-map",
plugins: [
new HtmlWebpackPlugin({
title: "Output Management"
})
],
devServer: {
contentBase: "./dist"
},
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: ["file-loader"]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: ["file-loader"]
},
{
test: /\.(csv|tsv)$/,
use: ["csv-loader"]
},
{
test: /\.xml$/,
use: ["xml-loader"]
}
]
}
};
/dist폴더 정리
/dist폴더에 넣지 만 프로젝트에서 실제로 사용중인 파일을 추적하지는 않습니다.
일반적으로 /dist각 빌드 전에 폴더 를 정리하여 사용 된 파일 만 생성되도록하는 것이 좋습니다
npm install --save-dev clean-webpack-plugin
webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
+ const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
entry: {
app: './src/index.js',
print: './src/print.js',
},
plugins: [
+ new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: 'Output Management',
}),
],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
},
};