解决Gatsby构建中的SVG错误

凯文·惠特克

我正在尝试使一个简单的Gatsby网站联机-gatsby develop运行正常,但出现gatsby build以下错误消息:

在此处输入图片说明

UNHANDLED REJECTION 
- Error in parsing SVG: 
- Unencoded <
- Line: 0
- Column: 2
- Char: <

我没有明确使用任何SVG,因此很难解决此问题-有什么建议吗?

我的项目在这里,改编自流明启动器主题

费兰·比雷(Ferran Buireu)

Icon.js(第14行)文件中,您使用的是<svg>这导致您的问题:

// @flow strict
import React from 'react';
import styles from './Icon.module.scss';

type Props = {
  name: string,
  icon: {
    viewBox?: string,
    path?: string
  }
};

const Icon = ({ name, icon }: Props) => (
  <svg className={styles['icon']} viewBox={icon.viewBox}>
    <title>{name}</title>
    <path d={icon.path} />
  </svg>
);

export default Icon;

在处理SVG时,我建议使用gatsby-plugin-react-svg或使用React内置用法。

  • 随着gatsby-plugin-react-svg你只是为了隔离在一个单独的文件夹中只能包含SVGs)的SVG:

    {
       resolve: 'gatsby-plugin-react-svg',
       options: {
         rule: {
           include: /assets/
         }
       }
    }
    
    

    然后将其作为React组件导入:

    import Icon from "./path/assets/icon.svg";
    
    // ...
    
    <Icon />;
    
  • React内置用法(仅适用https://create-react-app.dev/docs/adding-images-fonts-and-files/中的[email protected]更高版本,以及[email protected]更高版本,更多详细信息):

      import { ReactComponent as FacebookIcon } from '../images/svg/facebookF.svg';
      import { ReactComponent as Email } from '../images/svg/email.svg';
    
      ...
    
        <FacebookIcon />
        <Email /> 
    

由于您是从入门主题扩展而来的,因此请从项目中删除该组件及其正在使用的组件,并根据需要按需添加它。

您可以阅读此StackOverflow答案,以了解有关在盖茨比处理SVG的更多详细信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章