-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Importing SVG as react component does not work as mentioned in the docs. #7587
Comments
Im getting the very same, this is for quite a while. No one has got a workaround or a fix for it yet right? |
One possible workaround is to add |
This actually makes it work. Should i consider this issue to be resolved? |
No, its not resolved, this is a workaround not a fix. |
When we apply this workaround the error is gone but parcel gets stuck Our .parcelrc: {
"extends": "@parcel/config-default",
"transformers": {
"jsx:*.svg": ["...", "@parcel/transformer-svg-react"],
"jsx:*": ["..."],
"*.{js,mjs,jsx,cjs}": ["@parcel/transformer-js", "@parcel/transformer-react-refresh-wrap"]
}
} By removing the last line, starting with EDIT: Sadly there are still issues which I am unable to diagnose as the Website simply does not render. The only error I get is this: |
same issue with |
@MLNW - I hit the same error that you did - I think it's related to #7389 I found two workarounds: (1) remove inline style props on any svgs you are importing through your
|
@astegmaier I somehow missed your comment. It looks promising. Sadly I have moved on from the project for now so I can't test your solution at the moment |
My project uses SVG files in 2 ways:
import Icon from "./icon.svg";
function Component() {
return <Icon />;
}
import icon from "./icon.svg";
function Component() {
return <img src={icon} />;
} To make parcel work with this I had to:
"transformers": {
"*.jsx.svg": ["@parcel/transformer-svg-react"],
"*.{js,mjs,jsx,cjs,ts,tsx}": [
"@parcel/transformer-js",
"@parcel/transformer-react-refresh-wrap"
]
}
declare module '*.jsx.svg' {
import { FunctionComponent, SVGProps } from 'react';
const svg: FunctionComponent<SVGProps<SVGSVGElement> & { title?: string }>;
export default svg;
}
declare module '*.svg' {
const src: string;
export default src;
} It's not ideal because I cannot use the same image file both inline and as image but it works. |
While #7587 (comment) works, this named pipeline conflicts with Storybook's webpack. Anyway to have it working without the named pipeline? |
I kept the named pipeline and created this webpack plugin so storybook can remove the module.exports = function (source) {
return source.replace(/import\('jsx:(.*\.svg)'\)/g, "import('$1')")
} And have it in the storybook's webpack rules' array: {
test: /\.(ts|tsx)$/,
use: [
{
loader: path.resolve(__dirname, 'renameJsxImport.js'),
},
],
}, |
## Description The SVG project icons can't be included via `<img>` because they are colored using `currentColor`. They also can't be included via Parcel's `bundle-text` named pipeline becase The way we used `<sl-icon>`, it was fetching the svg file over the network and dynamically inserting it into the DOM. This instead uses another Parcel plugin to import an svg file as a React component at build time. Getting it to work is kinda finicky. Ssee parcel-bundler/parcel#7587, plus the workaround I ended up using in the not-merged PR parcel-bundler/parcel#7711. The list of transformers for `jsx:*.{js,jsx}` is copied from Parcel's default config for those file types. The icons are set to width `1em` to allow them to be sized by the font-size at the usage site, like `<sl-icon>`. ## Test Plan Look at the project icons in all contexts where they appear: project multiselect, icon tab bar in pill mode, icon tab bar in dropdown mode.
## Description The SVG project icons can't be included via `<img>` because they are colored using `currentColor`. They also can't be included via Parcel's `bundle-text` named pipeline becase The way we used `<sl-icon>`, it was fetching the svg file over the network and dynamically inserting it into the DOM. This instead uses another Parcel plugin to import an svg file as a React component at build time. Getting it to work is kinda finicky. Ssee parcel-bundler/parcel#7587, plus the workaround I ended up using in the not-merged PR parcel-bundler/parcel#7711. The list of transformers for `jsx:*.{js,jsx}` is copied from Parcel's default config for those file types. The icons are set to width `1em` to allow them to be sized by the font-size at the usage site, like `<sl-icon>`. ## Test Plan Look at the project icons in all contexts where they appear: project multiselect, icon tab bar in pill mode, icon tab bar in dropdown mode.
## Description The SVG project icons can't be included via `<img>` because they are colored using `currentColor`. They also can't be included via Parcel's `bundle-text` named pipeline becase The way we used `<sl-icon>`, it was fetching the svg file over the network and dynamically inserting it into the DOM. This instead uses another Parcel plugin to import an svg file as a React component at build time. Getting it to work is kinda finicky. Ssee parcel-bundler/parcel#7587, plus the workaround I ended up using in the not-merged PR parcel-bundler/parcel#7711. The list of transformers for `jsx:*.{js,jsx}` is copied from Parcel's default config for those file types. The icons are set to width `1em` to allow them to be sized by the font-size at the usage site, like `<sl-icon>`. ## Test Plan Look at the project icons in all contexts where they appear: project multiselect, icon tab bar in pill mode, icon tab bar in dropdown mode.
## Description The SVG project icons can't be included via `<img>` because they are colored using `currentColor`. They also can't be included via Parcel's `bundle-text` named pipeline becase The way we used `<sl-icon>`, it was fetching the svg file over the network and dynamically inserting it into the DOM. This instead uses another Parcel plugin to import an svg file as a React component at build time. Getting it to work is kinda finicky. Ssee parcel-bundler/parcel#7587, plus the workaround I ended up using in the not-merged PR parcel-bundler/parcel#7711. The list of transformers for `jsx:*.{js,jsx}` is copied from Parcel's default config for those file types. The icons are set to width `1em` to allow them to be sized by the font-size at the usage site, like `<sl-icon>`. ## Test Plan Look at the project icons in all contexts where they appear: project multiselect, icon tab bar in pill mode, icon tab bar in dropdown mode.
🐛 bug report
I am trying to import SVG as react component based on the documentation from parcel site. Instead of the svg being rendered as a component in the page, the build actually fails. With an error -
@parcel/core: No transformers found for src/assets/logo.jsx with pipeline: 'jsx'.
🎛 Configuration (.babelrc, package.json, cli command)
Using default babel configuration.
🤔 Expected Behavior
The svg image should be loaded as a react component in the webpage.
😯 Current Behavior
An error is thrown saying
@parcel/core: No transformers found for src/assets/logo.jsx with pipeline: 'jsx'.
💁 Possible Solution
🔦 Context
I am trying to import a svg image as a react component.
💻 Code Sample
Clone this repo and run
yarn && yarn run dev
🌍 Your Environment
Attachments:
The text was updated successfully, but these errors were encountered: