最近更新:去除了.css文件,统一使用tailwindcss来控制样式
心血来潮想要写一个自己的react组件库,但是又不知道如何入手,于是让ai直接帮我生成的了一个模板,于是有了下面这些内容。
项目结构详解
📁 根目录结构
my-component/
├── 📄 配置文件
├── 📁 src/ # 源代码
├── 📁 dist/ # 构建输出
├── 📁 example/ # 示例代码
├── 📁 scripts/ # 构建脚本
└── 📄 文档文件
📁 各文件/目录作用详解
🔧 配置文件层
package.json - 项目核心配置
{
"name": "@your-username/my-component-library",
"version": "1.0.0",
"main": "dist/index.js", // CommonJS 入口
"module": "dist/index.esm.js", // ES模块 入口
"types": "dist/index.d.ts", // TypeScript 类型定义
"files": ["dist"], // 发布时包含的文件
"peerDependencies": { // 对等依赖
"react": ">=16.8.0",
"react-dom": ">=16.8.0"
}
}
设计理念:
- 双格式支持:同时支持 CommonJS 和 ES 模块
- 类型安全:提供完整的 TypeScript 类型定义
- 对等依赖:避免版本冲突,让用户控制 React 版本
tsconfig.json - TypeScript 配置
{
"compilerOptions": {
"target": "es5", // 兼容性
"jsx": "react-jsx", // React 17+ JSX 转换
"declaration": true, // 生成类型定义文件
"outDir": "dist" // 输出目录
}
}
设计理念:
- 兼容性优先:支持更多浏览器
- 类型安全:生成完整的类型定义
rollup.config.js - 构建配置
export default [
{
input: 'src/index.ts',
output: [
{ file: packageJson.main, format: 'cjs' }, // CommonJS
{ file: packageJson.module, format: 'esm' } // ES模块
],
plugins: [
peerDepsExternal(), // 外部化对等依赖
resolve(), // 模块解析
commonjs(), // CommonJS 支持
postcss(), // CSS 处理
typescript() // TypeScript 编译
]
}
];
设计理念:
- 多格式输出:同时支持 CommonJS 和 ES 模块
- 外部化依赖:避免重复打包 React
- CSS 处理:自动提取和压缩样式
📁 源代码结构(src/)
src/
├── index.ts # 主入口文件
└── components/ # 组件目录
├── Input/ # Input 组件
│ ├── Input.tsx # 组件实现
│ └── index.ts # 组件导出
├── Select/ # Select 组件
│ ├── Select.tsx
│ └── index.ts
└── Form/ # Form 组件
├── Form.tsx
└── index.ts
为什么这样设计?
- 模块化组织:每个组件独立目录,便于维护
- 统一导出:通过 index.ts 统一导出,简化导入路径
src/index.ts - 主入口
export { Input } from "./components/Input";
export { Select } from "./components/Select";
export { Form, FormField, useForm } from "./components/Form";
export type { InputProps } from "./components/Input";
export type { SelectProps, SelectOption } from "./components/Select";
export type { FormProps, FormFieldProps } from "./components/Form";
设计理念:
- 统一导出:所有组件和类型从单一入口导出
- 类型导出:同时导出组件和类型定义
- 简洁导入:用户只需从一个地方导入
🎨 组件设计模式
Input组件结构
Input/
├── Input.tsx # 组件实现
└── index.ts # 导出文件
Input.tsx 设计:
export interface InputProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size'> {
size?: 'small' | 'medium' | 'large';
className?: string;
style?: React.CSSProperties;
}
export const Input = forwardRef<HTMLInputElement, InputProps>((props, ref) => {
// 组件实现
});
设计理念:
- 继承原生属性:通过 extends 继承所有 HTML input 属性
- 自定义属性:只添加必要的自定义属性
- Ref 转发:支持 ref 访问,提升灵活性
📦 构建输出 (dist/)
dist/
├── index.js # CommonJS 格式
├── index.esm.js # ES 模块格式
├── index.d.ts # TypeScript 类型定义
└── components/ # 各组件类型定义
├── Input/
├── Select/
└── Form/
为什么生成这些文件?
- 兼容性:支持不同的模块系统
- 类型安全:提供完整的 TypeScript 支持
📚 示例和文档
example/ 目录
example/
├── usage.tsx # 基础使用示例
├── styling-examples.tsx # 样式定制示例
├── basic-props-test.tsx # 属性测试示例
└── index.html # 静态示例页面
设计理念:
- 渐进式学习:从基础到高级示例
- 实用性:展示真实使用场景
- 可运行:所有示例都可以直接运行
文档文件
- README.md:主要文档,包含安装、使用、API
- DESIGN_PRINCIPLES.md:设计原则和最佳实践
- PUBLISH_GUIDE.md:发布指南
🔧 工具和脚本
scripts/ 目录
scripts/
├── build.js # 构建脚本
└── publish.js # 发布脚本
设计理念:
- 自动化:减少手动操作
- 错误处理:完善的错误提示
- 用户友好:清晰的执行步骤
配置文件
- tailwind.config.js:Tailwind CSS 配置
- postcss.config.js:PostCSS 配置
- .eslintrc.js:ESLint 配置
295

被折叠的 条评论
为什么被折叠?



