webpack的html-webpack-plugin安装配置无报错,但运行不生效问题
检查了大小写和空格均没问题
重新下载了低版本的plugin也不行
就又下载的最新的版本
npm install html-webpack-plugin@latest -D
现在的各种版本号为
"devDependencies": {
"html-webpack-plugin": "^5.5.0",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^3.11.2"
}
折腾了一个小时,各种找,一直运行无效,最后粘贴了别人和我一样的配置代码,成功了,虽然还不知道问题在哪。(打了注释的是我之前的代码,没有注释的是之后的代码)
webpack里面的配置代码如下:
const path= require('path')
// //1.导入HTML插件,得到一个构造函数
// const HtmlPlugin = require('html-webpack-plugin');
// //2.创建html插件的实例对象
// const htmlPlugin = new HtmlPlugin({
// template:'./src/index.html',//指定源文件的存放路径
// filename:'./index/html',//指定生成的文件的存放路径
// })
// //使用法Node.js中的导出语,向外导出一个webpack的配置对象
// module.exports = {
// //入口文件
// entry:path.join(__dirname,'./src/index.js'),
// //出口路径
// output:{
// //输出的文件名
// filename:'bundle.js',
// //输出路径
// path: path.join(__dirname,'dist'),
// },
// //代表webpack运行的模式,可选值有两个development和production,当开发阶段是development,当快上线时,改成production.
// mode:'development',
// plugins: [htmlPlugin],//通过pulgins节点,使htmlplugin插件生效。
// // devServer: {
// // static: {
// // directory: path.join(__dirname, './'),
// // watch: true
// // }
// // }
// }
const HtmlPlugin =require('html-webpack-plugin')
const htmlPlugin = new HtmlPlugin({
template:'./src/index.html', //指定源文件的存放路径
filename: './index.html', //指定生成文件的存放路径
})
module.exports = {
entry: path.join(__dirname, './src/index.js'), //打包入口文件的路径
output: {
path: path.join(__dirname, './dist'), //输出文件的存放路径
filename: 'bundle.js',//输出文件的名称
},
mode: 'development', //mode 用来指定构建模式。可选值有 development 和 production
// devServer:{
// open:true,
// host:'localhost',
// port:8080,
// static: {
// directory: path.join(__dirname, './')
// },
// },
plugins:[htmlPlugin], //通过plugins节点,使htmlPlugin插件生效
}