【发布时间】:2023-04-03 15:05:01
【问题描述】:
我正在实现一个组件库(一个 npm 模块,我们称之为awesome-components),它由按钮、输入、下拉列表等组成,最终用户可以将其安装在他们的 react 应用程序中并使用这些组件。我的困惑是为最终用户打包这个库的最佳方式是什么?我有两种方法,
- 使用
index.js文件将要导入的所有组件导出为命名导入。文件夹结构如下,
src
|-components
|-Button
|-index.jsx
|-Input
|-index.jsx
|-index.js
index.js会如下实现,
import Button from "./Button";
import Input from "./Input";
export { Button, Input };
json 包的main 将指向组件根目录中的index.js。 npm 包会将所有组件捆绑到一个文件中,并且将是 npm packed 和 npm publisheded,看起来有点像这样,
dist
|-index.js
|-index.js.map
|-package.json
最终用户可以在他们的应用程序中导入这些组件,如下所示,
import { Button, Input } from "awesome-components";
- 如下组件中没有
index.js,
src
|-components
|-Button
|-index.jsx
|-Input
|-index.jsx
并单独捆绑组件并运送给最终用户,保留看起来像这样的文件夹结构,
dist
|-components
|-Button
|-index.js
|-index.js.map
|-Input
|-index.js
|-index.js.map
最终用户可以通过如下绝对路径使用这些组件,
import Button from "awesome-library/Button";
import Input from "awesoem-library/Input";
我担心的是,哪种方法会使最终用户受益?组件的数量甚至可以增加到 20 个。就可扩展性和性能而言,最好的使用方法是什么?
非常感谢:)
【问题讨论】:
标签: reactjs npm node-modules packaging