您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

聊聊webpack中怎么压缩打包html资源

2026/3/12 12:23:10发布12次查看
webpack中怎么压缩打包html资源?下面本篇文章就来给大家简单介绍一下webpack压缩打包html资源的方法,希望对大家有所帮助!
为什么需要打包html资源写代码时引入的是src下面的js文件,经过webpack打包后,形成了一个入口文件,此时html中js文件的名称和路径就不对了,所以需要webpack打包,把html中引入js文件的路径替换了。
用webpack打包html的好处有:
(1)可以自动将打包后的js文件引入html
(2)html打包后依然会生成在build文件夹下和打包后的js文件放在一起,这样上线的时候我们只需要将打包生成的文件夹一起拷贝到上线环境就可以了
(3)会帮我们压缩html文件
webpack中怎么压缩打包html资源1、安装插件
webpack原生只能理解js和json文件,要支持打包其他类型的文件,都需要安装相应的插件或loader。
如果我们需要打包html文件,首先需要安装html-webpack-plugin插件:
npm install html-webpack-plugin -d
这个插件的作用:
默认在出口下创建一个html文件,然后导入所有的js/css资源
我们也可以自己指定一个html文件,在此html文件中加入资源
2、webpack.config.js配置
安装好html-webpack-plugin插件后,需要在webpack.config.js文件中进行配置:
// ... // 1. 引入插件 const htmlwebpackplugin = require('html-webpack-plugin'); module.exports = { // ... // 2. 在plugins中配置插件 plugins: [ new htmlwebpackplugin({ template: 'index.html', // 指定入口模板文件(相对于项目根目录) filename: 'index.html', // 指定输出文件名和位置(相对于输出目录) // 关于插件的其他项配置,可以查看插件官方文档 }) ] }
详细配置链接: https://www.npmjs.com/package/html-webpack-plugin
确保入口模板文件的路径和文件名与配置一致,然后可以编译。
3、多js入口和多html情况的配置
面对需要编译出多个html文件,且文件需要引入不同的js文件,但默认情况下,打包后的html文件会引入所有打包后的js文件,我们可以指定chunk来分配js。
const path = require('path'); // 1. 引入插件 const htmlwebpackplugin = require('html-webpack-plugin'); module.exports = { // ... // 2. 配置js入口(多入口) entry: { vendor: ['./src/jquery.min.js', './src/js/common.js'], index: './src/index.js', cart: './src/js/cart.js' }, // 配置出口 output: { filename: '[name].js', path: path.resolve(__dirname, 'build/js') }, // 3. 配置插件 plugins: [ new htmlwebpackpugin({ template: 'index.html', filename: 'index.html', // 通过chunk来指定引入哪些js文件 chunk: ['index', 'vendor'] }), // 需要编译多少个html,就需要new几次插件 new htmlwebpackplugin({ template: './src/cart.html', filename: 'cart.html', chunk: ['cart', 'vendor'] }) ] }
tip: 这里需要注意的是要编译多少个html文件,就要new几次htmlwebpackplugin。
上面的配置编译成功后,输出情况是这样的:
build |__ index.html # 引入index.js和vendor.js |__ cart.html # 引入cart.js和vendor.js |__ js |__ vendor.js # 由jquery.min.js和common.js生成 |__ index.js # 由index.js生成 |__ cart.js # 由cart.js生成
压缩打包html资源实例1、webpack.config.js配置
const htmlwebpackplugin = require('html-webpack-plugin')... plugins: [ // html-webpack-plugin html 打包配置 该插件将为你生成一个 html5 文件 new htmlwebpackplugin({ template: "./index.html", // 打包到模板的相对或绝对路径 (打包目标) title: '首页', // 用于生成的html文档的标题 hash: true,//true则将唯一的webpack编译哈希值附加到所有包含的脚本和css文件中。主要用于清除缓存, minify: { // 压缩html collapsewhitespace: true, // 折叠空白区域 keepclosingslash: true, // 保持闭合间隙 removecomments: true, // 移除注释 removeredundantattributes: true, // 删除冗余属性 removescripttypeattributes: true, // 删除script脚本类型属性 removestylelinktypeattributes: true, // 删除样式链接类型属性 useshortdoctype: true, // 使用短文档类型 preservelinebreaks: true, // 保留换行符 minifycss: true, // 压缩文内css minifyjs: true, // 压缩文内js } }), ],...
2、此时我们的index.html
<!doctype html><html lang=""> <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>webpackdemo</title> </head> <body> <div id="app"> html 打包配置 </div> </body></html>
3、此时我们的index.js
import './../css/index.less'function add(x,y) { return x+y}console.log(add(2,3));
3、控制台webpack键入打包,发现打包输出文件多了个index.html,内容如下
<!doctype html><html lang=""> <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>webpackdemo</title> <script defer src="index.js"></script></head> <body> <div id="app"> html 打包配置 </div> </body></html>
fa47545964b5492a54172b73a22d827b2cacc6d41bbb37262a98f745aa00fbf0是自动引入的
浏览器打开打包输出的 index.html,发现样式起了效果,控制太也正常输出:
更多编程相关知识,请访问:编程视频!!
以上就是聊聊webpack中怎么压缩打包html资源的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product