zoukankan      html  css  js  c++  java
  • (四)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'
    }
  • 相关阅读:
    待完成
    [NOI2006]神奇口袋
    [UVA 10529]Dumb Bones
    概率与期望习题总结
    [SHOI2012]随机树
    [luogu3412]仓鼠找sugar II
    [CF908D]New Year and Arbitrary Arrangement
    [清华集训]小 Y 和恐怖的奴隶主
    [SDOI2015]序列统计
    [luogu3600]随机数生成器
  • 原文地址:https://www.cnblogs.com/qiaoyun/p/13263604.html
Copyright © 2011-2022 走看看