自动生成html,利用hmtl-webpack-plugin插件
yarn add html-webpack-plugin -D,下载插件
yarn add html-webpack-plugin -D
- 在
webpack.config.js引入插件,书写:const HtmlWebpackPlugin = require('html-webpack-plugin') - 配置插件,书写:
plugins: [new HtmlWebpackPlugin({ template: './public/index.html' })]
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
mode: 'development',
plugins: [
new HtmlWebpackPlugin({ template: './public/index.html' })
]
}
yarn build,最后执行webpack打包,就自动生成html文件,以及自动引入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>
<script defer src="bundle.js"></script></head>
<body>
<h1>Hello World!</h1>
</body>
</html>