HtmlWebpackPlugin

htmlWebpackPlugin을 통해서 webpack에서 번들된 결과물을 html 파일과 같이 작성할 수 있습니다.

npm i --save-dev html-webpack-plugin

webpack.config.js 파일을 생성하고 아래의 코드를 넣습니다.

const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "index.js",
output: {
path: __dirname + "/dist",
filename: "index_bundle.js"
},
plugins: [new HtmlWebpackPlugin({})]
};

만약 여러개의 entry 포인트가 있다면 모든 번들된 파일들이 생성된 html안의 script 태그로 포함이 됩니다.

만약 여러개의 html 파일을 생성하고 싶다면 plugins안에 원하는 만큼의 new HtmlWebpackPlugin()을 하시면 됩니다.

options

파라미터에는 객체가 들어가는데 객체 안에는 다음과 같은 옵션을 설정할 수 있습니다.

NameTypeDefaultDescription
filenamestringindex.html생성될 html 파일 이름입니다.
templatestring템플릿으로 사용할 html 파일의 상대경로 또는 절대 경로입니다.
chunksarray번들된 파일중에서 어떤것을 html 파일에 포함시킬 건지 입니다.