Giter VIP home page Giter VIP logo

Comments (5)

wesleyguirra avatar wesleyguirra commented on June 15, 2024 3

Same problem here.

from gatsby-plugin-react-svg.

Alecell avatar Alecell commented on June 15, 2024

Apparently if your config match something that isn't SVG you get that error, maybe on svg folder has a file that isn't a SVG file. Thats an opinion based on experience, I don't delve in the code.

from gatsby-plugin-react-svg.

xnyl avatar xnyl commented on June 15, 2024

It is because .svg files are still being consumed by the url-loader plugin. Here is why:

The problem is in this conditional configuration:

const newUrlLoaderRule = (include || exclude) ? {
			...imgsRule,
			include: exclude,
			exclude: include
		} : {
			...imgsRule,
			test: new RegExp(imgsRule.test.toString().replace('svg|', '').slice(1, -1))
		}

If you explicitly specify include: /images\/svgs/, the final url loader configuration will be:

 {
  use: [
    {
      loader: '/.../node_modules/url-loader/dist/cjs.js',
      options: [Object]
    }
  ],
  test: /\.(ico|svg|jpg|jpeg|png|gif|webp|avif)(\?.*)?$/,
  include: undefined,
  exclude: /images\/svgs/
}

If you leave options empty, like this:

 plugins: [
    {
      resolve: "gatsby-plugin-react-svg",
      options: {},
    },

it will be:

{
  use: [
    {
      loader: '/.../node_modules/url-loader/dist/cjs.js',
      options: [Object]
    }
  ],
  test: /\.(ico|jpg|jpeg|png|gif|webp|avif)(\?.*)?$/
}

Here the test entry is modified and skips svg from being consumed by the url-loader.

So it seems that having |svg| in the test both with exclude/include regexes is not working as expected - url-loader is still processing those images before svg-loader. That is probably a bug.

from gatsby-plugin-react-svg.

jacobmischka avatar jacobmischka commented on June 15, 2024

Honestly, I don't use gatsby anymore these days, and I haven't used this library myself since before the 3.0 release, but specifying include works fine for me in ^2.0 in older gatsby projects and the logic still seems correct to me; when specifying include, those paths will be excluded by url-loader, so I still don't see what the issue is despite your examples above.

Pull requests welcome, surely.

Edit: I updated an older project to use gatsby 3 and the most recent version of this plugin, and specifying include still indeed works.

gatsby-config.js:

		{
			resolve: 'gatsby-plugin-react-svg',
			options: {
				rule: {
					include: /icons/
				}
			}
		},

index.js:

...
import TeamIcon from '../images/icons/team.svg';

export default function IndexPage({ data }) {
	return (
		<Layout
			title={data.site.siteMetadata.title}
			className="home"
		>
			<TeamIcon />
		</Layout>
	);
}

SVG icon is rendered correctly.

Same include but with this config results in an error:

		{
			resolve: 'gatsby-plugin-react-svg',
			options: {
				rule: {
					include: /icfons/
				}
			}
		},

So I'm not sure what's going on causing it to not work for you.

from gatsby-plugin-react-svg.

avodynamics avatar avodynamics commented on June 15, 2024

I have the same issue.

from gatsby-plugin-react-svg.

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.