默认webpack无法处理css文件中的url地址 在打包时会报错 在项目根目录下输入 然后在配置文件webpack.config.js中进行配置loader: 使用url-loader打包之后 在页面中 默认会将路径以base64编码 减少图片的二次请求 进行配置: 右击图片 – 属性 若不进行base64的转换 可通过配置 不使用默认的改名为Hash字符串: 例: 否则 由于打包后的url引用的直接是 若既要不改文件名 又要避免图片重复问题 若通过路径的形式引入node_modules中相关的文件 可省略node_modules 例: 然后 同样 在webpack.config.js中进行配置即可:
一、处理图片文件
无论是图片还是字体库 只要是url地址 都无法处理解决方法:
cnpm i url-loader file-loader -D
安装url-loader和file-loader
(url-loader内部依赖于file-loader)var path=require("path") var htmlWebpackPlugin=require("html-webpack-plugin") module.exports={ entry:path.join(__dirname,"./src/main.js"), output:{ path:path.join(__dirname,"./dist"), filename:"bundle.js" }, // 所有webpack插件的配置节点 plugins:[ new htmlWebpackPlugin({ template:path.join(__dirname,"./src/index.html"), filename:"index.html" }) ], // 配置第三方loader模块 module:{ rules:[ // 第三方模块的匹配规则 {test:/.css$/,use:["style-loader","css-loader"]}, // 处理css文件的loader {test:/.jpg|png|gif|bmp|jpeg$/,use:"url-loader"} // 处理图片url的loader ] } }
关闭默认的以base64编码:
给url-loader传参 格式和网页url地址传参完全一致 在url-loader后面加问号传递参数即可
属性名:limit
设定一个大小范围 当图片大于或等于给定的limit值(单位byte) 则不将url转换为base64格式 反之进行转换
格式:{test:/.jpg|png|gif|bmp|jpeg$/,use:"url-loader?limit=图片大小"}
如何查看图片大小:
注:若图片长度大于三位 每三位要以逗号进行分隔
496886要写成496,886
例:{test:/.jpg|png|gif|bmp|jpeg$/,use:"url-loader?limit=496,886"}
图片的名称会默认变成一串Hash字符串 以防止图片重名
在url-loader后面加上name=[name].[ext]
即可
[name]意为:之前是什么名称 打包之后也叫什么名称
[ext]意为:之前是什么后缀名 打包之后也叫什么后缀名{test:/.jpg|png|gif|bmp|jpeg$/,use:"url-loader?limit=496,886&name=[name].[ext]"}
文件名.后缀名
的格式 若有重名的图片 即使不在同一个文件夹下
也会出现引入的图片重复
为避免此问题 需要将名称改为不重复的
可配置为:{test:/.jpg|png|gif|bmp|jpeg$/,use:"url-loader?limit=496,886&name=[hash:8]-[name].[ext]"}
name=[hash:8]-[name].[ext]的意思是:在图片前面带上32位hash值的前8位 中间用短横杠进行连接 具体的hash值长度可根据需要设置 但不能超过32 因为hash值最大只有32位
二、处理字体文件
在main.js里引入字体文件:
若不写node_modules 默认会从node_modules里查找
bootstrap.css文件在node_modules/bootstrap/dist/css下
但在引入的时候 可以这么写:import "bootstrap/dist/css/bootstrap.css"
字体文件也是使用url-loader进行处理var path=require("path") var htmlWebpackPlugin=require("html-webpack-plugin") module.exports={ entry:path.join(__dirname,"./src/main.js"), output:{ path:path.join(__dirname,"./dist"), filename:"bundle.js" }, // 所有webpack插件的配置节点 plugins:[ new htmlWebpackPlugin({ template:path.join(__dirname,"./src/index.html"), filename:"index.html" }) ], // 配置第三方loader模块 module:{ rules:[ // 第三方模块的匹配规则 {test:/.css$/,use:["style-loader","css-loader"]}, {test:/.jpg|png|gif|bmp|jpeg$/,use:"url-loader?limit=496,886&name=[hash:8]-[name].[ext]"}, {test:/.ttf|eot|svg|woff|woff2$/,use:"url-loader"} // 处理字体文件的loader ] } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算