首页 > Web开发 > 详细

(四)webpack打包图片资源

时间:2020-07-07 22:32:17      阅读:76      评论:0      收藏:0      [点我收藏+]

webpack打包图片资源主要分两个方面的配置

一、打包css文件中的图片资源,需要用到file-loader、url-loader,相关配置如下

const path = require(path)
const htmlWebpackPlugin = require(html-webpack-plugin)
module.exports = {
    entry: "./src/main.js",
    output:{
        filename: index.js,
        path:path.resolve(__dirname, build)
    },
    module:{
        rules:[
            {
                test: /\.css$/,
                use:[
                    "style-loader",
                    "css-loader"
                ]
            },
       
            {
                test: /\.(jpg|png|gif)$/,
                loader:"url-loader", //url-loader依赖于file-loader
                options:{
                    limit: 8 * 1024 //图片小于8kb就是用base64的方式
                }
                  
            }
        ]
    },
    plugins:[
        new htmlWebpackPlugin({
            template:"./src/index.html"
        })
    ],
    mode:development
}

二、打包html中的图片资源,主要是用到html-loader  html-loader的作用是引入图片资源,然后让url-loader去解析。相关配置如下

const path = require(path)
const htmlWebpackPlugin = require(html-webpack-plugin)
module.exports = {
    entry: "./src/main.js",
    output:{
        filename: index.js,
        path:path.resolve(__dirname, build)
    },
    module:{
        rules:[
            {
                test: /\.css$/,
                use:[
                    "style-loader",
                    "css-loader"
                ]
            },
            {
                test: /\.(jpg|png|gif)$/,
                loader:"url-loader",
                options:{
                    limit: 8 * 1024
                }
                  
            },
            {
                test: /\.html$/,
                loader:"html-loader",
                  
            }
        ]
    },
    plugins:[
        new htmlWebpackPlugin({
            template:"./src/index.html"
        })
    ],
    mode:development
}

 

(四)webpack打包图片资源

原文:https://www.cnblogs.com/qiaoyun/p/13263604.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!