在真实发布项目时,需要将index.html文件打包到dist文件夹中,这时可以使用HtmlWebpackPlugin
1.安装(要注意版本问题)
npm install html-webpack-plugin --save-dev
2.作用
- 自动生成—个index.html文件(可以指定模板来生成)将打包的js文件
- 自动通过script标签插入到body中
3.使用
webpack.config.js中添加
const HtmlWebpackPlugin=require('html-webpack-plugin')
plugins: [
new HtmlWebpackPlugin({
template:'index.html'//这里的template表示根据什么模板来生成index.html
})
]
另外,我们需要删除之前在output中添加的publicPath属性,否则插入的script标签中的src可能会有问题