[英]How do you use an SVG inline in React using Parcel 2?
Previously in Parcel v1 you could just use something like the @svgr/parcel-plugin-svgr
plugin for Parcel.以前在 Parcel v1 中,您可以只使用类似@svgr/parcel-plugin-svgr
的 Parcel 插件。 This would give you the ability to use SVGs inline like when using CRA:这将使您能够像使用 CRA 一样使用内联 SVG:
import Star from './star.svg'
const App = () => (
<div>
<Star />
</div>
)
Can anyone help figure out a way to do this in Parcel 2?任何人都可以帮助找出在 Parcel 2 中执行此操作的方法吗?
Parcel2 provides the @parcel/transformer-svg-react
plugin to accomplish this. Parcel2 提供了@parcel/transformer-svg-react
插件来完成这个。
Here's what you need to do:这是您需要做的:
Install it:安装它:
yarn add @parcel/transformer-svg-react --dev
Add a .parcelrc
file at the root of your project that defines a named pipleine that uses this plugin:在项目的根目录中添加一个.parcelrc
文件,该文件定义了一个使用此插件的命名管道:
{ "extends": "@parcel/config-default", "transformers": { "jsx:*.svg": ["...", "@parcel/transformer-svg-react"] } }
(The "..."
entry should actually be typed into the .parcelrc
file - it tells parcel "process these kinds of assets as usual, and only after you are done, transform it into a react component.") ( "..."
条目实际上应该输入到.parcelrc
文件中——它告诉 parcel “像往常一样处理这些类型的资产,只有在你完成后,才将其转换为反应组件。”)
Use the named pipeline in your import statements:在导入语句中使用命名管道:
import Star from 'jsx:./star.svg' const App = () => ( <div> <Star /> </div> )
See parcel 2 documentation .请参阅包裹 2 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.