webpack4-- 处理html中引入的图片

index.html

    <img src="./src/images/current_day_icon.png" alt="">

配置

   module: {
        rules: [{
                test: /\.(png|jpg|gif)$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        publicPath: "./images/",
                        outputPath: "images/"
                    }
                }
                ]
            },
            {
                test: /\.(html)$/,
                use: {
                    loader: 'html-loader',
                    options: {
                        attrs: ['img:src', 'img:data-src', 'audio:src'],
                        minimize: true
                    }
                }
            }
 ]

打包结果

在这里插入图片描述

必须file-loader和html-loader’同时使用才会把img打包到build/images

猜你喜欢

转载自blog.csdn.net/qq_34035425/article/details/83061399