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

import SVG directly as a React Component #2269

Closed
Chastrlove opened this issue Feb 26, 2021 · 8 comments
Closed

import SVG directly as a React Component #2269

Chastrlove opened this issue Feb 26, 2021 · 8 comments

Comments

@Chastrlove
Copy link

希望能像cra一样 既提供导出svg资源,又支持导出组件
import { ReactComponent as Logo } from "logo.svg"
import logo from "logo.svg"

@yyx990803
Copy link
Member

This is React specific and can be only be done via plugins.

@aleclarson
Copy link
Member

See https://www.npmjs.com/package/vite-plugin-react-svg

@Chastrlove
Copy link
Author

@svgr/rollup resolved that

@phoenisx
Copy link

phoenisx commented Mar 11, 2021

@Chastrlove Can you share your config here. I am trying to add:

{
  ...svgr({
    memo: true,
    icon: true,
    svgoConfig: {
      removeViewBox: false
    }
  }),
  enforce: 'pre',
}

as plugin, but this isn't working. It still imports .svg as a url.


Let it be, I found that importing .svg needs to be changed. It's supposed to be:

import { ReactComponent as Logo } from './logo.svg';

Update:

No, it works on dev, but breaks for prod builds.

I figured out the issue. It's specific to Typescript. The declaration of module *.svg in vite client.d.ts does not export any const ReactComponent, due to which tsc throws error, while build.

Also, since @rollup/plugin-url is used by default (I guess or some other Rollup Plugin), and is called before svgr plugin, even after using enforce: 'pre' option, forces svgr to export url string as default export.

Also, there is no way (that I am aware of) to override already existing declare statements in client.d.ts files, I guess, as per:

Is it ok to add support for typescript declarations for svgr? If yes, I can raise a PR for the same! 🎉

@MKRhere
Copy link

MKRhere commented May 19, 2021

@phoenisx Just add this to svg.d.ts and add it to include array in tsconfig.json and you get proper TS support

declare module "*.svg" {
	import * as React from "react";

	export const ReactComponent: React.FunctionComponent<
		React.SVGProps<SVGSVGElement> & { title?: string }
	>;
}

@AlexGustafsson
Copy link

AlexGustafsson commented Jun 10, 2021

Is there any way to disable the use of the existing SVG transformer? Sure, this works as a workaround, but it leaves a lot to be desired. First, the SVG assets are still copied to the output. Second, the default export cannot be overwritten, leaving needless use of named imports instead.

The typing can be solved by not specifying client.d.ts in tsconfig and providing a type of your own, as mentioned previously, but there's no way to achieve proper support for SVGs in React even with plugins for as long as there is a hard coded transformer already present.

@aleclarson
Copy link
Member

@AlexGustafsson Please open a new issue for that 👍

@github-actions
Copy link

This issue has been locked since it has been closed for more than 14 days.

If you have found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Vite version. If you have any other comments you should join the chat at Vite Land or create a new discussion.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants