Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an svg in webpack with svg-url-loader

I'm having a lot of trouble working with SVG in my webpack workflow. I'm trying to get it to display with the background: url(sample.svg) property in CSS. Using this alone did not work, so I figured I had use a loader. Here are the steps I used.

I used svg-url-loader to load the SVG.

1. I installed svg-url-loader via npm and added this to my module.exports:

 {
        test: /\.svg/,
        use: {
            loader: 'svg-url-loader'
        }
      },

2. I added this to the top of my index.js file:

require('svg-url-loader!./images/topography.svg');

3. I added background-image with the SVG path to my CSS:

body {
  background-image: url("../images/topography.svg");
  background-size: 340px, auto;
  min-height: calc(100vh - 100px);
  margin: 50px;
  background-attachment: fixed;
  letter-spacing: -1px;
}

4. The SVG is not being rendered to the page. When I inspect the body in browser, I find this:

background: url(data:image/svg+xml,module.exports = __webpack_public_path__ + '8dccca4….svg';);

I don't know too much about data-uri, so maybe I am running into the issue there.

Also, I've tried this using different SVG files, and none of them worked.

like image 594
InspectorDanno Avatar asked Aug 17 '18 00:08

InspectorDanno


People also ask

How do I import SVG into Webpack?

Once you start your application, Webpack will do its thing and you don't need to worry about your SVGs anymore. You can put your SVG files anywhere in your src/ folder and import them wherever you need them as React components. This method is generally referred to as inline-svg .

What is URL loader in Webpack?

Webpack's url-loader lets you import arbitrary files, like images. If you import a . png file, url-loader will ensure that import resolves to a Base64 string representing the contents of the file.

What is SVG inline loader?

This Webpack loader inlines SVG as module. If you use Adobe suite or Sketch to export SVGs, you will get auto-generated, unneeded crusts. This loader removes it for you, too.

How to load SVG files in Webpack?

The webpack way to load svg? · Issue #595 · webpack/webpack · GitHub Move svg and png fallback files into the destination folder. Bundle a module for the client to check for svg support. Bundle a module for the client that returns the correct url for the image based on svg support.

What is Webpack File Loader in Webpack?

webpack file loader is a loader used mainly for supporting images such as SVG and PNG, and fonts in your webpack project. There are different configurations for webpack 4 and webpack 5.

How to get the correct URL for a SVG image?

Bundle a module for the client that returns the correct url for the image based on svg support. A svg-loader which works similar to the url-loader and the html-loader. It exports a DataUrl (not base64 encoded, as it's pure text) and it requires referenced resources from inside the SVG (you can use URL in the svg).

What is the use of SVG-inline-loader?

svg-inline-loader | webpack webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset. webpack is a module bundler.


2 Answers

I met the same exact error. After some investigation I found I added another svg loader which caused this problem, so I fixed it by deleting the other svg loader:

      {
        test: /\.svg/,
        use: {
            loader: 'svg-url-loader'
        }
      },

      {
        test: /\.svg$/,
        use: [
          "babel-loader",
          {
            loader: "react-svg-loader",
            options: {
              svgo: {
                plugins: [{ removeTitle: false }],
                floatPrecision: 2
              },
              jsx: true
            }
          }
        ]
      }

So you maybe also added some extra loaders to handle the svg files at the same time, please check.

like image 109
Jeff Tian Avatar answered Sep 18 '22 09:09

Jeff Tian


You can:

a) set up loaders in webpack.config.js:

example.js:

import ExampleIcon from 'assets/icons/example-icon.svg';

...

<ExampleIcon className={styles.exampleIcon} />

webpack.config.js:

{
  test: /\.svg$/,
  use: [
    {
      loader: 'babel-loader',
    },
    {
      loader: 'react-svg-loader',
      options: {
        svgo: {
          plugins: [{ removeTitle: false }],
          floatPrecision: 2
        },
        jsx: true
      }
    }
  ]
},

b) or set up loaders in the import string:

import ExampleIcon from '!babel-loader!react-svg-loader!assets/icons/example-icon.svg';

...

<ExampleIcon className={styles.exampleIcon} />

like image 26
Ukr Avatar answered Sep 20 '22 09:09

Ukr