vite-plugin-svgr icon indicating copy to clipboard operation
vite-plugin-svgr copied to clipboard

Plugins don't work

Open worlddai opened this issue 3 years ago • 17 comments

40/5000 I'm having a problem. Your plug-in doesn't work after using it. I don't know what's wrong with the configuration. The browser returned the following error:

Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('/src/images/smcLogo.svg') is not a valid name.

my code is this:

import React, { Component } from 'react';
import cssObj from './Logo.css';
import Icon from '@ant-design/icons';
import {ReactComponent as SMCLogo } from '@/images/smcLogo.svg';
class Logo extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        return (

            <div style={{width: this.props.width, height: this.props.height, paddingTop:'10px' }} >
                <div className={cssObj.LogoLeft}>
                    <Icon component={()=><SMCLogo width="70px" height="22px"  fill="#B7C1CF"/>} />
                </div>
            </div>

        );
    }
}

export default Logo;

Looking forward to your reply. thank you

worlddai avatar Jun 15 '21 12:06 worlddai

Thanks for the report. Could you provide a minimal reproduce repo?

pd4d10 avatar Aug 11 '21 12:08 pd4d10

same error after updating svgr related dependencies to 6.1.2

yckbilly1929 avatar Dec 19 '21 14:12 yckbilly1929

I'm having an issue here too where my svg files are not optimized by svgo at all. Here's the config

svgrPlugin({
      svgrOptions: {
        svgo: true,
        icon: false,
        svgoConfig: {
          plugins: [
            { moveGroupAttrsToElems: true },
            { convertPathData: true },
          ],
        },
      },
    })],

alkanna avatar Jan 11 '22 09:01 alkanna

latest svgo changed his default configuration format from yml to js. So that could be a issue.

FDiskas avatar Jan 31 '22 21:01 FDiskas

This is missing a reproduction step. Something similar happened when I tried setting up the webpack config for Storybook to work with the @webpack/svgr. Turns out the default webpack config setup for Storybook had fileloader rules that hit the *.svg and returned the path before my svgr config was hit. So there was no ReactComponent to use. I had to dynamically replace the regex to be able to override what to do with the svgs. Might be something similar going on here, seeing that Vite handles svg loading as paths by default. Don't see how, though, as it works fine for me :shrug:

fatso83 avatar Feb 21 '22 13:02 fatso83

@alkanna I meet same issue, I fix my issue by by add @svgr/plugin-svgo and @svgr/plugin-jsx, see https://github.com/pd4d10/vite-plugin-svgr/issues/49 for more details

JiangWeixian avatar Jan 15 '23 10:01 JiangWeixian

I am facing the issue where svgo config is setup but svg components get rendered as it is without any optimisation. I have tried above mentioned ways but still no success.

javaharut avatar Jun 22 '23 07:06 javaharut

I suggest everyone having trouble to just fire up a debugger to see what is going on. This makes it quite clear without waiting for anyone else to figure it out (which is not likely to happen).

Just

  1. Figure out which plugins are loaded in which order
  2. Open up the plugin modules in node_modules in your editor and put breakpoints in them, or insert debugger statements
  3. Figure out how to run Vite inside your debugger (open node_modules/.bin/vite to see)
  4. Profit

fatso83 avatar Jun 22 '23 07:06 fatso83

I figured out and it seems working as a pie. here is full config which will make things better for newcomers

svgr({
      svgrOptions: {
        plugins: ['@svgr/plugin-svgo', '@svgr/plugin-jsx'],
        svgoConfig: {
          plugins: ['preset-default', 'removeTitle', 'removeDesc', 'removeDoctype', 'cleanupIds'],
        },
      },
    }),

javaharut avatar Jun 22 '23 08:06 javaharut

I have been trying to use this and debugged the code both with exportAsDefault on and off. The code generates fine, but I end up with this in the bundle:

function Footer({ connected }) {
  return /* @__PURE__ */ React.createElement("div", { className: classNames("socials", { connected }) }, /* @__PURE__ */ React.createElement("a", { href: "#", title: "GitHub", className: "github-logo" }, /* @__PURE__ */ React.createElement("svgGithub", null)), /* @__PURE__ */ React.createElement("a", { href: "#", title: "Discord", className: "discord-logo" }, /* @__PURE__ */ React.createElement("svgDiscord", null)), /* @__PURE__ */ React.createElement("a", { href: "#", title: "Twitter", className: "twitter-logo" }, /* @__PURE__ */ React.createElement("svgTwitter", null)), /* @__PURE__ */ React.createElement("a", { href: "#", title: "Facebook", className: "facebook-logo" }, /* @__PURE__ */ React.createElement("svgFacebook", null)));
}

this is on the classic react mode react({ jsxRuntime: "classic" }),

in the default mode:

function Footer({ connected }) {
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: classNames("socials", { connected }), children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx("a", { href: "#", title: "GitHub", className: "github-logo", children: /* @__PURE__ */ jsxRuntimeExports.jsx("svgGithub", {}) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("a", { href: "#", title: "Discord", className: "discord-logo", children: /* @__PURE__ */ jsxRuntimeExports.jsx("svgDiscord", {}) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("a", { href: "#", title: "Twitter", className: "twitter-logo", children: /* @__PURE__ */ jsxRuntimeExports.jsx("svgTwitter", {}) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("a", { href: "#", title: "Facebook", className: "facebook-logo", children: /* @__PURE__ */ jsxRuntimeExports.jsx("svgFacebook", {}) })
  ] });
}

JSX Code is:

import "./Footer.scss";
import classNames from "classnames";
import svgGithub from "../../../assets/icon/github.svg";
import svgDiscord from "../../../assets/icon/discord.svg";
import svgTwitter from "../../../assets/icon/twitter.svg";
import svgFacebook from "../../../assets/icon/facebook.svg";

export default function Footer({ connected }) {
  return (
    <div className={classNames("socials", { connected })}>
      <a href="#" title="GitHub" className="github-logo">
        <svgGithub />
      </a>
      <a href="#" title="Discord" className="discord-logo">
        <svgDiscord />
      </a>
      <a href="#" title="Twitter" className="twitter-logo">
        <svgTwitter />
      </a>
      <a href="#" title="Facebook" className="facebook-logo">
        <svgFacebook />
      </a>
    </div>
  );
}

pcfreak30 avatar Jul 27 '23 21:07 pcfreak30

@pcfreak30 You are not actually stating what your problem with the generated code is, so hard to help you out 😄

fatso83 avatar Jul 30 '23 14:07 fatso83

Sorry thought it was obvious.

jsxRuntimeExports.jsx("svgGithub", {}) }),

React.createElement("svgFacebook", null)

The element isn't actually created, its null/empty.

pcfreak30 avatar Jul 30 '23 15:07 pcfreak30

I had the same problem as I didn't notice latest breaking change of V4

If you use the latest version vite-plugin-svgr (v4), you have to update the import syntax as follows

### before
import { ReactComponent as Logo } from "./logo.svg";

## after
import Logo from "./logo.svg?react";

antekai avatar Sep 20 '23 18:09 antekai

using svgr in electron and vite, same problem, warning like:

react-dom.development.js:26923 Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('/src/aa.svg?react') is not a valid name.
    at createElement (http://localhost:5173/node_modules/.vite/deps/chunk-J4JQZ5XV.js?v=15f35e51:7509:42)
    at createInstance (http://localhost:5173/node_modules/.vite/deps/chunk-J4JQZ5XV.js?v=15f35e51:8345:28)
    at completeWork (http://localhost:5173/node_modules/.vite/deps/chunk-J4JQZ5XV.js?v=15f35e51:16277:34)
    at completeUnitOfWork (http://localhost:5173/node_modules/.vite/deps/chunk-J4JQZ5XV.js?v=15f35e51:19215:24)
    at performUnitOfWork (http://localhost:5173/node_modules/.vite/deps/chunk-J4JQZ5XV.js?v=15f35e51:19200:13)
    at workLoopSync (http://localhost:5173/node_modules/.vite/deps/chunk-J4JQZ5XV.js?v=15f35e51:19131:13)
    at renderRootSync (http://localhost:5173/node_modules/.vite/deps/chunk-J4JQZ5XV.js?v=15f35e51:19110:15)
    at recoverFromConcurrentError (http://localhost:5173/node_modules/.vite/deps/chunk-J4JQZ5XV.js?v=15f35e51:18730:28)
    at performConcurrentWorkOnRoot (http://localhost:5173/node_modules/.vite/deps/chunk-J4JQZ5XV.js?v=15f35e51:18678:30)
    at workLoop (http://localhost:5173/node_modules/.vite/deps/chunk-J4JQZ5XV.js?v=15f35e51:195:42)

Including svg like

// src/app.tsx line 7
import CustomIcon from './aa.svg?react';

Including svgr in vite config

// vite.main.config.ts

// line 2
import svgr from 'vite-plugin-svgr';

// line 12
plugins: [svgr()],

A minimal repo is here:

https://github.com/zzswang/for-svgr-issue

Any ideas? Thanks

zzswang avatar Sep 23 '23 02:09 zzswang

@zzswang did you figure that out?

red2678 avatar Nov 02 '23 09:11 red2678

@zzswang did you figure that out?

Sorry, forgot to update it.

Yes, I've fixed it.

How to fix it:

My project is based on electron forge. I move the plugin settings from vite.main.config.ts to vite.renderer.config.ts. Then it works like a charm.

zzswang avatar Nov 02 '23 13:11 zzswang

it's work when i use relative path in import , and install @sgvr plugins

npm i -D @svgr/plugin-jsx @svgr/plugin-svgo

My vite config :

import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";
import svgr from "vite-plugin-svgr";

export default defineConfig({
	base: "/my-app/",
	plugins: [
		[react()],
		svgr({
			// svgr options: https://react-svgr.com/docs/options/
			svgrOptions: {
				plugins: ["@svgr/plugin-svgo", "@svgr/plugin-jsx"],
				svgoConfig: {
					plugins: ["preset-default", "removeTitle", "removeDesc", "removeDoctype", "cleanupIds"],
				},
			},
			// A minimatch pattern, or array of patterns, which specifies the files in the build the plugin should include.
			include: "**/*.svg?react",
		}),
	],
});

My code :

import Logo from "../../../public/logo/logo.svg?react";
...
export default function App() {
return (
			<Logo/>
	);
}

issam-seghir avatar Mar 01 '24 09:03 issam-seghir