Coder Social home page Coder Social logo

Comments (13)

kisenka avatar kisenka commented on May 18, 2024 2

@lysoff if you want to import SVGs from CSS with svg-url-loader (inline as data-uri) and from JS with svg-sprite-loader I must warn you that this is potential source of collisions of these two loaders. For workaround this you can define svg-url-loader as default loader for SVGs, and exclude some folders which should be processed by svg-sprite-loader:

const svgSpriteFolder = path.resolve(__dirname, 'folder-with-svgs-for-svg-sprite-loader');

...

loaders: [
  // default loader for SVG
  {
    test: /\.svg$/,
    loader: 'svg-url-loader',
    exclude: svgSpriteFolder
  },

  {
    test: /\.svg$/,
    loader: 'svg-sprite-loader',
    include: svgSpriteFolder
  }
]

from svg-sprite-loader.

kisenka avatar kisenka commented on May 18, 2024 1

@samuelpaulc

  1. You cannot use both loaders for single file.
  2. svg-sprite-loader can be used only in JS, not in CSS.

Use svg-url-loader in CSS and svg-sprite-loader in JS.

from svg-sprite-loader.

constverum avatar constverum commented on May 18, 2024 1

One more way:

.icon1 {
  /* SVG will be inlined in CSS */
  background-image: url('./foo.svg?inline&fill=red');
}

.icon2 {
  /* default way - build a sprite and use SVG fragment identifiers */
  background-image: url('./foo.svg&fill=red');
}

Webpack config:

{
  module: {
    rules: [
      ...
      {
        test: /.svg$/,
        oneOf: [
          {
            resourceQuery: /inline/, 
            use: [
              'svg-url-loader',
              'svg-fill-loader',
            ],
          },
          {
            use: [
              {
                loader: 'svg-sprite-loader',
                options: {
                  esModule: false,
                  extract: true,
                },
              },
              'svg-fill-loader',
            ],
          },
        ],
      },
    ]
  }
}

from svg-sprite-loader.

kisenka avatar kisenka commented on May 18, 2024

Could you provide an example?

from svg-sprite-loader.

samuelpaulc avatar samuelpaulc commented on May 18, 2024

We are using the sprited svgs we get from this loader in the below way which is what the example was saying and it works very well.

<svg
style={style}
dangerouslySetInnerHTML={{ __html: '' }}
/>

But we are using a third party library 'rc-tree' and we want to apply our own icons for the tree.
They have a className provided their api where we can put out icons and style it.

I was wondering if we can reuse the sprited svg somehow?

from svg-sprite-loader.

kisenka avatar kisenka commented on May 18, 2024

@samuelpaulc in this case - no. You should should customize rc-tree classes with following manner:

.override-rc-class {background-image: url('image.svg')} // svg should inlined via url-loader

from svg-sprite-loader.

samuelpaulc avatar samuelpaulc commented on May 18, 2024

Oh so I need to use https://github.com/bhovhannes/svg-url-loader right?

from svg-sprite-loader.

kisenka avatar kisenka commented on May 18, 2024

@samuelpaulc right

from svg-sprite-loader.

samuelpaulc avatar samuelpaulc commented on May 18, 2024

Cool 👍 Thanks Stas

from svg-sprite-loader.

samuelpaulc avatar samuelpaulc commented on May 18, 2024

@kisenka But when we use the svg-url-loader, a separate request is made, can it not work together with svg-sprite-loader where it gets sprited?

from svg-sprite-loader.

kisenka avatar kisenka commented on May 18, 2024

What you mean saying "a separate request is made"? svg-url-loader inline SVG contents into CSS.

from svg-sprite-loader.

samuelpaulc avatar samuelpaulc commented on May 18, 2024

@kisenka
We are also using svg-sprite-loader for our svg images which are used directly in JS as their documentation says and it works well.

Now we are trying to make the svg that we load from within a css class to be also sprited

I am trying this and it does not work.

Using only svg-sprite-loader:
Works well for svgs included in js but if a svg in css is used I get the below error
Error: attribute y: Expected length, "%5C'6%5C'".

Using only svg-url-loader
Works

Using both together
Module build failed: ReferenceError: document is not defined

Order of loaders also dosent help;
Webpack config:
{
test: /.svg$/,
include: 'path/to/svg',
loaders: [
'svg-sprite?' + JSON.stringify({
name: '[name]',
prefixize: true
}),
'svg-url-loader'
]
}

Error I get for this config is
Module build failed: TypeError: Must be an object

Please suggest where I am going wrong and how to get both of these loaders to work together

from svg-sprite-loader.

lysoff avatar lysoff commented on May 18, 2024

@kisenka

Use svg-url-loader in CSS and svg-sprite-loader in JS.

could you provide webpack config example for this?

from svg-sprite-loader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.