Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()
  ]
};
like image 925
bullettrain Avatar asked Aug 27 '19 09:08

bullettrain


1 Answers

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
    }
},
like image 174
PattyOK Avatar answered Oct 05 '22 10:10

PattyOK