【问题标题】:How to make svg react component imports from external module working with create-react-app?如何使用 create-react-app 从外部模块导入 svg react 组件?
【发布时间】:2021-07-23 00:51:27
【问题描述】:

上下文

为了使一些代码部分在许多 react 项目中可重用,我有以下项目结构:

  1. @foo/design: 包含原始 svg 图像等设计内容
  2. @foo/ui: 基于 Create React App for common 组件的 React 库
  3. @foo/project:使用foo/ui 的最终 CRA 项目

图书馆

foo/ui 库导出一些从@foo/design 导入 svg 图像的组件:

// MyComponent.ts
import React, { FC } from 'react';
import {
  ReactComponent as Logo,
} from '@foo/design/static/img/logo.svg';

export const MyComponent: FC = () => {
  return (
    <div>
      Some stuff.
      <Logo />
    </div>
  );
};

export default null;

此组件在 @foo/ui 的相关 Storybook 实例上完美运行,并使用 tsc 编译,如下所示:

// MyComponent.js
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { ReactComponent as Logo, } from '@foo/design/static/img/logo.svg';
export var Navbar = function () {
    return (_jsxs("div", { children: ["Some stuff.", _jsx(Logo, {}, void 0)] }, void 0));
};
export default null;

这里也是相关的tsconfig.json文件:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "declaration": true,
    "outDir": "./lib",
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "react-jsx"
  },
  "include": ["src"],
  "exclude": [
    "src/samples",
    "src/**/*.stories.*",
    "src/**/*.test.*",
    "src/setupTests.js"
  ],
}

项目导入

在@foo/project 上,我添加了@foo/ui 依赖项,同时安装了@foo/design 包(@foo/ui 需要)。

我就是这样使用我的组件的:

import { FC } from 'react';
import { MyComponent } from '@foo/ui'

const App: FC = () => {
  return (
    <MyComponent />
  );
}

export default App;

但这会产生以下错误:

Failed to compile.

./node_modules/@foo/ui/lib/MyComponent.js
Attempted import error: 'ReactComponent' is not exported from '@foo/design/static/img/logo.svg' (imported as 'Logo').

但是,如果我直接在我的根应用程序组件上从 MyComponent.js 复制/粘贴 svg 导入行:

import { FC } from 'react';
import { ReactComponent as Logo, } from '@foo/design/static/img/logo.svg';

const App: FC = () => {
  return (
    <Logo />
  );
}

export default App;

这可以正常工作,并且 svg 会按预期显示。

这里也是CRA项目相关的tsconfig.json文件:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}

为什么ReactComponent CRA 导入在从我导入的库中完成后无法正常工作? @foo/ui 仅在内部使用,解决此问题的最简单方法是什么?

谢谢

【问题讨论】:

    标签: javascript reactjs typescript svg create-react-app


    【解决方案1】:

    我发现在类似情况下唯一的解决方法可以帮助我 - 内联定义SVG组件,使用https://svg2jsx.com/准备组件。

    因此,在您的情况下,您将无法使用来自 @foo/design 的共享 SVG 来创建包装器组件,但至少您将能够在 @foo/ui 中准备共享的 SVG 组件。

    花了很多时间,最近才解决,所以无法解释根本问题是什么。

    【讨论】:

    • 确实,这与其说是答案,不如说是一种技巧,但感谢您的提示!我会保留它以备不时之需。 ;-)
    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 2020-12-17
    • 2020-08-19
    • 2019-10-29
    • 1970-01-01
    • 2020-10-30
    • 2018-01-14
    • 2019-10-02
    相关资源
    最近更新 更多