webpack 4 gives background: url([object Module]) as bg image

霸气de小男生 提交于 2020-12-08 06:07:27

问题


I'm having issues with setting up web-pack 4 and svg-sprite-loader to render svg icons as background images. I was following these instructions from official docs for svg-sprite-loader (https://github.com/kisenka/svg-sprite-loader/tree/master/examples/extract-mode).

I have successfully managed to create sprite.svg file in my dist folder and use it as reference for my use tags inside of html. However, i was also trying to use svg icons from my src/images/icons folder for a background image like this:

background: url('../images/icons/upload_icon.svg') 10% 50% no-repeat;

when doing this, webpack compiles correctly, but creates this in dist css file:

background: url([object Module]) 10% 50% no-repeat;

Any help would be great.

here is my webpack config file:

const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const SpriteLoaderPlugin = require("svg-sprite-loader/plugin");

module.exports = {
  mode: "development",
  devtool: "source-map",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist")
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            sourceMap: true
          }
        }
      },
      {
        // scss configuration
        test: /\.scss$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: "css-loader"
          },
          {
            loader: "postcss-loader"
          },
          {
            loader: "sass-loader",
            options: {
              sourceMap: true
            }
          }
        ]
      },
      {
        // html configuration
        test: /\.html$/,
        use: {
          loader: "html-loader"
        }
      },
      {
        // images configuration
        test: /\.(jpg|jpeg|gif|png|woff|woff2)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "[path][name].[ext]"
            }
          }
        ]
      },
      {
        test: /\.svg$/,
        use: [
          {
            loader: "svg-sprite-loader",
            options: {
              extract: true,
              spriteFilename: "sprite.svg"
            }
          }
        ]
      }
    ]
  },
  plugins: [
    // all plugins used for compiling by webpack
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      title: "Style Guide",
      template: path.resolve(__dirname, "src", "index.html")
    }),
    new MiniCssExtractPlugin({
      filename: "app.css"
    }),
    new SpriteLoaderPlugin()
  ]
};

回答1:


Adding esModule: false to the file-loader options did the trick for me.

{
    test: /\.(jpg|png|gif|svg)$/,
    use: {
        loader: 'file-loader',
        options: {
            name: "[name].[ext]",
            outputPath: "img",
            esModule: false
    }
},



回答2:


You have to pass esModule: false for svg-sprite-loader options.

By the way (it is not related to esModule): With MiniCssExtractPlugin you can not to extract svg sprite. I've faced this problem one hour ago..




回答3:


After a few hours, I have managed to make this thing to work, thanks to @WimmDeveloper for pointing me in right direction. Main change from prior webpack config file is that I have added esModule: false in svg-sprite-loader options and replaced MiniCssExtractPlugin with extract-text-webpack-plugin. Mind you that this solution is not ideal since this webpack plugin is deprecated.

here is my full webpack config file:

const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const SpriteLoaderPlugin = require("svg-sprite-loader/plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
    mode: "development",
    devtool: "source-map",
    output: {
        filename: "bundle.js",
        path: path.resolve(__dirname, "dist")
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        sourceMap: true
                    }
                }
            },
            {
                test: /\.(s*)css$/,
                use: ExtractTextPlugin.extract({
                    use: ["css-loader", "postcss-loader", "sass-loader"]
                })
            },
            {
                // html configuration
                test: /\.html$/,
                use: {
                    loader: "html-loader"
                }
            },
            {
                test: /\.svg$/,
                use: [
                    {
                        loader: "svg-sprite-loader",
                        options: {
                            esModule: false,
                            extract: true,
                            spriteFilename: "sprite.svg"
                        }
                    }
                ]
            },
            {
                // files configuration
                test: /\.(jpg|jpeg|gif|png|woff|woff2)$/,
                use: [
                    {
                        loader: "file-loader",
                        options: {
                            name: "[path][name].[ext]"
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        // all plugins used for compiling by webpack
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            title: "Style Guide",
            template: path.resolve(__dirname, "src", "index.html")
        }),
        new ExtractTextPlugin({ filename: "app.css" }),
        new SpriteLoaderPlugin()
    ]
};


来源:https://stackoverflow.com/questions/57671995/webpack-4-gives-background-urlobject-module-as-bg-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!