(五)webpack打包圖片資源


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'
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM