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'
    }
  • 相关阅读:
    python3----练习题(斐波那契)
    python3----运算符
    python3----函数、匿名函数
    python3----生成器generator(yield)
    Python捕获异常
    OS模块
    发送邮件
    IO文件读写
    Unittest框架概念
    生成报告
  • 原文地址:https://www.cnblogs.com/qiaoyun/p/13263604.html
Copyright © 2011-2022 走看看