打包html需要用到一个插件html-webapck-plugin。 npm i html-webpack-plugin -D package.json需要的开发依赖如下: webpack.config.js的配置如下: 代码仓库:https://gitee.com/seimin/xiaoming2qianduan-webpack5/tree/v5.1/{
"name": "wpk5-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"html-webpack-plugin": "^5.3.1",
"webpack": "^5.28.0",
"webpack-cli": "^4.5.0"
}
}
const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/main.js',
output: {
filename: 'bundle.js',
path: resolve(__dirname, 'dist')
},
module: {
rules: []
},
plugins: [
new HtmlWebpackPlugin({
template: './src/assets/index.html',
hash: true,
filename: 'index.html',
favicon: './src/assets/favicon.ico'
})
],
mode: 'development'
}