最近在react项目中初次用到了html-webapck-plugin插件,用到该插件的两个主要作用:
为html文件中引入的外部资源如script、link动态添加每次compile后的hash,防止引用缓存的外部文件问题
可以生成创建html入口文件,比如单页面可以生成一个html文件入口,配置N个html-webpack-plugin可以生成N个页面入口
1、安装
cnpm i webpack-plugin -D
2、在webpack.config.json中引用
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin') //第一步
module.exports = {
entry: path.join(__dirname, './src/main.js'),
output: {
path: path.join(__dirname, './dist'),
filename: 'bundle.js',
},
mode: 'development',
devServer: {
open: true,
port: 8080,
hot: true,
contentBase: 'src'
},
plugins: [
new htmlWebpackPlugin({ //第二步
template: path.join(__dirname, './src/index.html'), //指定生成模板的路径
filename: 'index.html' //指定生成页面的名称
})
]
}
3、html-webpack-plugin的作用
一、在内存中生成一个指定模板的文件,在访问时速度更快
二、自动为指定模板文件添加bundle.js文件
总结
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理