|
| 1 | +# 组件库打包与发布指南 |
| 2 | + |
| 3 | +本文档详细说明了 `@w.ui/wui-react` 组件库的打包配置、开发规范及发布流程。 |
| 4 | + |
| 5 | +## 1. 项目配置 |
| 6 | + |
| 7 | +### 1.1 构建配置 (`vite.config.lib.ts`) |
| 8 | + |
| 9 | +项目使用 Vite 的库模式 (Library Mode) 进行打包。核心配置如下: |
| 10 | + |
| 11 | +- **入口文件**: `src/components/index.ts` |
| 12 | +- **输出目录**: `dist-lib` |
| 13 | +- **格式**: 生成 `es` (ESM) 和 `umd` 格式 |
| 14 | +- **外部依赖**: `react`, `react-dom`, `antd`, `react-router-dom` 等被配置为 `external`,不会打包进库文件中,而是作为 `peerDependencies`。 |
| 15 | +- **类型定义**: 使用 `vite-plugin-dts` 自动生成 TypeScript 类型定义文件 (`.d.ts`)。 |
| 16 | +- **路径别名**: 配置了 `@stateless`, `@hooks`, `@utils`, `@assets` 等别名,确保构建时能正确解析。 |
| 17 | + |
| 18 | +### 1.2 Package.json 配置 |
| 19 | + |
| 20 | +关键字段说明: |
| 21 | + |
| 22 | +```json |
| 23 | +{ |
| 24 | + "name": "@w.ui/wui-react", |
| 25 | + "version": "2.0.0", |
| 26 | + "files": ["dist-lib"], |
| 27 | + "main": "./dist-lib/pro-react-components.umd.js", |
| 28 | + "module": "./dist-lib/pro-react-components.es.js", |
| 29 | + "types": "./dist-lib/index.d.ts", |
| 30 | + "exports": { |
| 31 | + ".": { |
| 32 | + "types": "./dist-lib/index.d.ts", |
| 33 | + "import": "./dist-lib/pro-react-components.es.js", |
| 34 | + "require": "./dist-lib/pro-react-components.umd.js" |
| 35 | + }, |
| 36 | + "./style.css": "./dist-lib/style.css" |
| 37 | + }, |
| 38 | + "peerDependencies": { |
| 39 | + "antd": ">=6.0.0", |
| 40 | + "react": ">=19.0.0", |
| 41 | + "react-dom": ">=19.0.0" |
| 42 | + }, |
| 43 | + "scripts": { |
| 44 | + "build:lib": "vite build -c vite.config.lib.ts", |
| 45 | + "prepublishOnly": "npm run build:lib", |
| 46 | + "pub": "npm publish --access public", |
| 47 | + "pub:beta": "npm publish --tag beta --access public" |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## 2. 开发规范 |
| 53 | + |
| 54 | +### 2.1 组件开发 |
| 55 | +- **目录结构**: |
| 56 | + - `src/components/stateless/`: 无状态组件(UI 组件) |
| 57 | + - `src/components/stateful/`: 有状态组件(业务组件) |
| 58 | + - `src/components/hooks/`: 自定义 Hooks |
| 59 | +- **样式**: 使用 Less Modules (`.module.less`) 避免样式冲突。 |
| 60 | +- **资源引用**: 图片、视频等资源应放在 `src/assets` 下,并使用别名 `@assets` 引用。 |
| 61 | + |
| 62 | +### 2.2 导出组件 |
| 63 | +所有需要发布的组件必须在 `src/components/index.ts` 中导出: |
| 64 | + |
| 65 | +```typescript |
| 66 | +export { default as MyComponent } from './stateless/MyComponent'; |
| 67 | +// 或者命名导出 |
| 68 | +export { MyComponent } from './stateless/MyComponent'; |
| 69 | +``` |
| 70 | + |
| 71 | +## 3. 构建流程 |
| 72 | + |
| 73 | +在发布之前,必须先进行构建以生成 `dist-lib` 目录。 |
| 74 | + |
| 75 | +```bash |
| 76 | +# 执行构建 |
| 77 | +npm run build:lib |
| 78 | +``` |
| 79 | + |
| 80 | +构建产物包括: |
| 81 | +- `pro-react-components.es.js`: ESM 格式(用于现代构建工具) |
| 82 | +- `pro-react-components.umd.js`: UMD 格式(用于浏览器直接引入) |
| 83 | +- `style.css`: 所有组件的样式汇总 |
| 84 | +- `index.d.ts`: 类型定义文件 |
| 85 | + |
| 86 | +## 4. 发布流程 |
| 87 | + |
| 88 | +### 4.1 准备工作 |
| 89 | +1. 确保代码已提交并推送到远程仓库。 |
| 90 | +2. 登录 NPM 账号: |
| 91 | + ```bash |
| 92 | + npm login |
| 93 | + ``` |
| 94 | + |
| 95 | +### 4.2 版本管理 |
| 96 | +使用 `standard-version` 自动更新版本号并生成 CHANGELOG: |
| 97 | + |
| 98 | +```bash |
| 99 | +# 发布标准版本 (自动判断版本升级类型) |
| 100 | +npm run release |
| 101 | + |
| 102 | +# 手动指定版本升级 |
| 103 | +npm run release:minor # 1.0.0 -> 1.1.0 |
| 104 | +npm run release:patch # 1.0.0 -> 1.0.1 |
| 105 | +npm run release:major # 1.0.0 -> 2.0.0 |
| 106 | +``` |
| 107 | + |
| 108 | +### 4.3 发布到 NPM |
| 109 | + |
| 110 | +```bash |
| 111 | +# 发布正式版 |
| 112 | +npm run pub |
| 113 | + |
| 114 | +# 发布 Beta 版 |
| 115 | +npm run pub:beta |
| 116 | +``` |
| 117 | + |
| 118 | +> **注意**: 由于包名为 `@w.ui/wui-react` (Scoped Package),发布时必须指定 `--access public`(脚本中已配置)。 |
| 119 | +
|
| 120 | +## 5. 常见问题排查 |
| 121 | + |
| 122 | +1. **构建报错 "Cannot find module"**: |
| 123 | + - 检查 `vite.config.lib.ts` 中的 `alias` 配置是否包含该路径。 |
| 124 | + - 检查 `tsconfig.json` 中的 `paths` 配置。 |
| 125 | + - 确保文件扩展名正确(JSX 文件应为 `.jsx` 或 `.tsx`)。 |
| 126 | + |
| 127 | +2. **发布失败 "You do not have permission to publish"**: |
| 128 | + - 检查是否登录了正确的 NPM 账号。 |
| 129 | + - 确认是否有 `@w.ui` 组织的发布权限。 |
| 130 | + - 如果是个人项目,考虑更改包名为 `@your-username/wui-react`。 |
| 131 | + |
| 132 | +3. **样式丢失**: |
| 133 | + - 确保在使用库的项目中引入了样式文件:`import '@w.ui/wui-react/style.css';` |
| 134 | + |
0 commit comments