webpack入门学习笔记10 —— 在项目中使用图片资源

1. 写在前面

在前端项目中,图片是必不可少的一种资源。在使用图片的时候,我们可以有以下几种方式:

  • .html 文件中,通过 <img src="" alt=""> 标签引入图片
  • .css 文件中,通过 background: url() 属性来引入图片
  • .js 文件中,通过 import 或者 require 语法来引入图片

但是在基于webpack的项目中,如果你只是简单的引入,而没有对这些图片文件做一些配置处理的话,是不能成功的。比如,如果你在一个 .js 文件中书写以下代码:

import logo from './logo.png'
console.log(logo);

或者在 .css 文件中,书写以下代码:

body {
    background-color: gray;
    background: url('./logo.png')
}

那么在进行打包编译项目或者启动本地开发服务器的时候,会报出以下错误:

错误提示很明显:我们需要一个合适的loader来处理这些图片文件。

那么在这篇博客中,我们就来讲述一下:如何进行配置webpack,使得图片资源可以在项目中被正常地使用。主要内容包括:

  • 使用 file-loader 处理图片资源
  • 使用 html-withimg-loader 处理图片资源

2. 使用 file-loader 处理图片资源

首先讲述如何使用 file-loader 来处理图片资源,既然是一个loader,那么第一步肯定是安装,安装命令如下:

yarn add file-loader -D

安装完成之后,到 webpack.config.js 文件中进行配置,配置规则非常简单,这里直接给出代码:

module.exports = {
    /* 节省篇幅,其他配置信息已省略 */
    module: {
        rules: [
            {
                test: /\.(png|jpg|jpeg|gif)$/,  // 使用正则匹配文件图片
                use: {
                    loader: 'file-loader'       // 使用file-loader对这些文件进行处理
                }
            }
        ]
    }
}

这里将其他的配置规则省略了,如果你想看其他的配置规则,可以查看我的 GitHub仓库 - webpack-learning ,或者通过本文末尾的链接,跳转到我之前的博客中进行查看。

这里介绍一下 file-loader 的作用:对项目中使用到的图片文件进行处理,将处理后的文件保存到 输出文件夹

,并返回输出文件夹下该文件的 URL 。如此一来,这个文件就可以被正常使用。

下面我们在项目中体验一下这个loader的作用,在一个 .js 文件中书写以下代码,使用图片资源:

import logo from './logo.png'   // 引入图片
console.log(logo);  

let img = new Image();
img.src = logo;

document.getElementsByTagName('body')[0].appendChild(img);

打包编译之后,就可以在页面上看到这样图片了。

同理,在 .css 文件中也可以正常使用图片资源,参考代码如下:

body {
    background-color: gray;
    background: url('./logo.png')
}

如果这时候,你在 .html 文件中 <img src="" alt=""> 标签引入图片,webpack一样会报错。仅靠file-loader做不到这一点,为了解决这个问题,我们需要 在file-loader的基础上,使用下面讲到的loader。

3. 使用 html-withimg-loader 处理图片资源

这个loader的作用是:处理 .html 文件中引用的图片,使得图片能够被正常使用。但是要注意,使用这个loader的时候,同时也要使用上面的 file-loader

和上面一样,首先我们要做的还是进行安装,安装命令如下:

yarn add html-withimg-loader -D

安装完成之后,到 webpack.config.js 文件中进行配置,配置规则非常简单,这里直接给出代码:

module.exports = {
    /* 节省篇幅,其他配置信息已省略 */
    module: {
        rules: [
            // 配置 file-loader
            {
                test: /\.(png|jpg|jpeg|gif)$/,   
                use: {
                    loader: 'file-loader'    
                }
            },
            // 配置 html-withimg-loader
            {
                test: /\.html$/,
                use: 'html-withimg-loader'
            }
        ]
    }
}

配置完成之后,我们就可以在 .html 文件中正常使用图片,比如以下代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <!-- 引入logo.png图片 -->
    <img src="./logo.png" alt="">       
    
</body>
</html>

现在进行打包编译项目,得到编译后的 index.html 文件代码如下:

<!DOCTYPE html>
<html lang=en>

<head>
    <meta charset=UTF-8>
    <meta name=viewport content="width=device-width,initial-scale=1">
    <meta http-equiv=X-UA-Compatible content="ie=edge">
    <title>Document</title>
</head>

<body>
    <!-- 注意这里的图片引用路径不再是logo.png -->
    <!-- 这个路径是 file-loader 对文件处理后生成新文件的路径 -->
    <img src=2b9a35ac93ae4d5ea1eb21230d61c324.png alt="">
</body>

</html>

这个时候在浏览器中打开编译后的 index.html 文件,就可以看到我们的图片了。

4. 优化图片处理方式,减少http请求

在编写前端项目的过程中,我们可能会用到很多图片资源,有些图片非常小,只有几KB。但是这些小图片的数量有时却非常大,有的项目中可能有几十个甚至是上百个图片。

这就会导致一个问题:项目上线之后,为了请求这些图片资源,浏览器端会大量使用http请求来获取图片,这就可能导致浏览器的开销过大,性能较低。那么怎么来解决这个问题呢?

这里我们介绍一种编码方式——Base64编码。这不是一种加密解密方式,它是一种编码方式。简单来说,它的作用就是:使用字符来表示任意二进制文件(这些字符包括:A B ... Z a b ... z 0 1 ... 9 + / 等64个字符)。

为了更加贴切的体会Base64编码,你可以复制以下长长的Base64编码,粘贴到浏览器地址栏,便可以看到这段Base64编码表示的图片了。

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABEYAAAJmCAIAAADephJOAAAACXBIWXMAAAsTAAALEwEAmpwYAAAL/WlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDIgNzkuMTYwOTI0LCAyMDE3LzA3LzEzLTAxOjA2OjM5ICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnBob3Rvc2hvcD0iaHR0cDovL25zLmFkb2JlLmNvbS9waG90b3Nob3AvMS4wLyIgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjEvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RFdnQ9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuM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EzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABCYpAEAAAKTNAAAQGCSBgAACEzSAAAAgUkaAAAgMEkDAAAEJmkAAIDAJA0AABDY/wfOJNyGtUsLSgAAAABJRU5ErkJggg==

使用Base64对图片进行编码,就可以将这些图片切入到静态的 .html 文件中,这样就不需要再使用http请求来获取图片了,从而节省浏览器开销,提高性能。

为了在基于Webpack的项目中使用Base64编码处理图片,我们需要使用 url-loader 来进行处理。首先安装该loader,安装命令如下:

yarn add url-loader -D

安装完成之后,我们到 webpack.config.js 文件中进行相关配置,配置非常简单,代码如下:

module.exports = {
   /* 节省篇幅,其他的配置信息已省略 */
    module: {
        rules: [
            {
                test: /\.(png|jpg|jpeg|gif)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 200*1024     // 小于200k的图片,使用Base64进行处理
                    }
                }
            }
        ]
    }
}

这里将其他的配置规则省略了,如果你想看其他的配置规则,可以查看我的 GitHub仓库 - webpack-learning ,或者通过本文末尾的链接,跳转到我之前的博客中进行查看。

上述配置非常好理解:对于png、jpg等图片格式的资源,使用url-loader进行处理,如果图片小于200k,那么就是用Base64进行编码;如果图片大于200k,就不做处理,使用http请求来获取图片。

配置完成之后,当我们编译项目时,就会在生成的 .html 文件中看到使用Base64编码后的图片,由于内容较多,就不贴代码,想查看的编译后代码的同学,可以点击 Github连接 进行查看。

5. 写在最后

这是本篇博客的全部内容,关于 file-loader 的更多知识,推荐大家访问 webpack官网 - file-loader

上一篇: webpack入门学习笔记09 —— 在项目中引入全局变量

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,179评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,229评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,032评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,533评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,531评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,539评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,916评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,813评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,568评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,654评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,354评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,937评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,918评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,152评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,852评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,378评论 2 342

推荐阅读更多精彩内容

  • Webpack学习总结 此文只是自己学习webpack的一些总结,方便自己查阅,阅读不变,非常抱歉!! 下载安装:...
    Lxs_597阅读 928评论 0 0
  • 学习目的 学习前端构建化工具 webpack,能熟练掌握 并使用 webpack 对项目进行打包优化。 ...
    _1633_阅读 1,347评论 0 7
  • 接着上一块https://www.jianshu.com/p/dd135bdb9065 模块:CSS文件打包 We...
    感觉不错哦阅读 494评论 0 2
  • webpack 是什么? 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(mo...
    IT老马阅读 3,299评论 2 27
  • webpack使用学习 本分享学习借鉴webpack中文官网,官网链接(中文文档):https://www.web...
    腿毛怪丶叔叔阅读 865评论 0 5