Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include all SVG files, without the ?react doesn't work #107

Open
sanjarcode opened this issue Jan 6, 2024 · 3 comments
Open

Include all SVG files, without the ?react doesn't work #107

sanjarcode opened this issue Jan 6, 2024 · 3 comments

Comments

@sanjarcode
Copy link

sanjarcode commented Jan 6, 2024

Can we omit the need to add ?react at the end of every SVG import?

Current situation

// works

export default defineConfig({
  plugins: [react(), svgr()], // no params
  base: `/${viteBaseName}/`,
});

import MyIcon from "../MyIcon.svg?react";   //  has `?react`

<MyIcon /> // works

Goal situation

I used the include option in vite.config.js, but that didn't work:

// this doesn't works
export default defineConfig({
  plugins: [
    react(),
    svgr({
      include: "**/*.svg", // params
    }),
  ],
  base: `/${viteBaseName}/`,
});

import MyIcon from "../MyIcon.svg";  // no `?react`
<MyIcon /> // doesn't work

/*
DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('/vite-react-js-template/src/MyIcon.svg') is not a valid name
*/

How to fix this? I think some default parameters may be missing in this second case. If yes, can we use callback pattern such the callback returns the default param, and I can selectively set include while retaining the rest.

@sanjarcode sanjarcode changed the title Include all SVG files, even without the ?react Include all SVG files, without the ?react doesn't work Jan 6, 2024
@theonastos
Copy link

theonastos commented Jan 22, 2024

@sanjarcode Any luck here?

@mariedevweb
Copy link

Try adding in your vite.config.ts, this allowed me to include my svg file without the ?react
plugins: [react(), svgr({ include: '**/*.svg' })],

@lazaro9318
Copy link

Try adding in your vite.config.ts, this allowed me to include my svg file without the ?react plugins: [react(), svgr({ include: '**/*.svg' })],

This works like a charm, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants