【发布时间】:2021-08-09 06:20:35
【问题描述】:
我使用 CRA 创建了一个打字稿应用程序,代码如下:
import { ReactComponent as Search } from './search.svg'
它运行良好,现在我想将此代码剥离到一个 npm 包(库)中。我首先再次运行 CRA 创建了这个包,然后删除了所有看起来不相关的内容(例如公用文件夹)。 /dist 的简化版本如下所示:
esm/
icon/
index.d.ts
index.js
index.d.ts
index.js
这是原icon/index.ts:
/// <reference types="react-scripts" />
import { ReactComponent as Search } from './search.svg'
export const Icons = {
Search,
}
这是编译后的icon/index.d.ts:
/// <reference types="react" /> <-- Changed for some reason??
export declare const Icons: {
Search: import("react").FunctionComponent<import("react").SVGProps<SVGSVGElement> & {
title?: string | undefined;
}>;
};
当我尝试运行使用此库的应用时,我收到以下错误:
../dist/esm/common/icon/index.js Attempted import error:
'ReactComponent' is not exported from './search.svg' (imported as 'Search').
我该如何解决这个问题?
【问题讨论】:
-
也停留在这个...看起来像 npmjs.com/package/@svgr/webpack 魔术,但不知道如何将它添加到 npm 包的构建/发布工具中:(
-
解决方法可以是
import Logo from '../images/logo.svg',然后是<img src={Logo} alt="logo" />
标签: reactjs typescript create-react-app