npm i html-webpack-plugin -D
- webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
plugins: [
new ESLintWebpackPlugin({
// 指定检查文件的根目录
context: path.resolve(__dirname, "src"),
}),
new HtmlWebpackPlugin({
// 以 public/index.html 为模板创建文件
// 新的html文件有两个特点:1. 内容和源文件一致 2. 自动引入打包生成的js等资源
template: path.resolve(__dirname, "public/index.html"),
}),
],
原来index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h3>hello webpack</h3>
<div class="div1">
hello webpack
</div>
<div class="div2">
hello webpack
</div>
<div class="div3">
hello webpack
</div>
<div class="div4">
hello webpack
</div>
<!-- 7这是手动引入,不要了 -->
<!-- <script src="../dist/main.js"></script> -->
</body>
</html>