Skip to content

Commit 0b4afb8

Browse files
committed
chore: 更新 package.json 和 pnpm-lock.yaml,添加独立函数支持
- 在 package.json 中添加独立函数导入配置,支持 ESM 和 CommonJS 格式 - 更新 pnpm-lock.yaml,添加新依赖并调整版本 - 修改 rslib.config.ts,增加独立函数构建逻辑 - 更新 README.md,详细说明新的导入方式和包结构
1 parent 19e03bd commit 0b4afb8

File tree

4 files changed

+3332
-3780
lines changed

4 files changed

+3332
-3780
lines changed

README.md

Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
- 🚀 开箱即用:提供丰富的工具函数,无需重复造轮子
88
- 🛠️ 类型安全:使用 TypeScript 开发,提供完整的类型定义
9-
- 📦 模块化:按需引入,减小打包体积
9+
- 📦 **独立函数导入**:支持单个函数导入,极致的按需加载,减小打包体积
10+
- 🎯 **多格式支持**:提供 ESM、CommonJS、UMD 多种格式,适配不同环境
1011
- 🔧 可扩展:支持自定义配置,满足不同场景需求
1112
- 📝 文档完善:提供详细的 API 文档和使用示例
1213
- 🔄 持续更新:定期更新,保持代码质量
14+
-**零依赖**:除必要的运行时依赖外,无额外依赖
1315

1416
## 安装
1517

@@ -26,29 +28,110 @@ pnpm add @winner-fed/cloud-utils
2628

2729
## 使用
2830

29-
### 按需引入
31+
### 完整引入(推荐)
3032

3133
```typescript
32-
import { formatDate, debounce } from '@winner-fed/cloud-utils';
34+
import { formatDate, accAdd, isEmpty } from '@winner-fed/cloud-utils';
3335

3436
// 使用日期格式化
3537
const formattedDate = formatDate(new Date(), 'YYYY-MM-DD');
3638

37-
// 使用防抖函数
38-
const debouncedFn = debounce(() => {
39-
console.log('执行防抖函数');
40-
}, 300);
39+
// 使用精确加法
40+
const sum = accAdd(0.1, 0.2); // 0.3
41+
42+
// 判断是否为空
43+
const empty = isEmpty(null); // true
4144
```
4245

43-
### 完整引入
46+
### 独立函数导入(按需加载)
47+
48+
对于只需要少数几个函数的场景,可以直接导入独立函数,减少打包体积:
49+
50+
```typescript
51+
// ESM 格式 - 自动选择合适的文件
52+
import { accAdd } from '@winner-fed/cloud-utils/accAdd';
53+
import { formatDate } from '@winner-fed/cloud-utils/formatDate';
54+
import { isEmpty } from '@winner-fed/cloud-utils/isEmpty';
55+
56+
// CommonJS 格式
57+
const { accAdd } = require('@winner-fed/cloud-utils/accAdd');
58+
const { formatDate } = require('@winner-fed/cloud-utils/formatDate');
59+
```
60+
61+
### 直接路径导入
62+
63+
如果需要明确指定导入格式:
64+
65+
```typescript
66+
// 明确使用 ESM 格式
67+
import { accAdd } from '@winner-fed/cloud-utils/dist/esm/accAdd.js';
68+
69+
// 明确使用 CommonJS 格式
70+
const { accAdd } = require('@winner-fed/cloud-utils/dist/cjs/accAdd.js');
71+
```
72+
73+
### 完整命名空间导入
4474

4575
```typescript
4676
import * as utils from '@winner-fed/cloud-utils';
4777

4878
// 使用工具函数
4979
const result = utils.formatDate(new Date(), 'YYYY-MM-DD');
80+
const sum = utils.accAdd(0.1, 0.2);
5081
```
5182

83+
## 包结构
84+
85+
本包提供了多种格式以适应不同的使用场景:
86+
87+
```
88+
@winner-fed/cloud-utils/
89+
├── dist/
90+
│ ├── index.js # ESM 主入口
91+
│ ├── index.cjs # CommonJS 主入口
92+
│ ├── index.d.ts # TypeScript 声明文件
93+
│ ├── *.d.ts # 独立函数的声明文件
94+
│ ├── esm/ # ESM 格式的独立函数
95+
│ │ ├── accAdd.js
96+
│ │ ├── formatDate.js
97+
│ │ └── ...
98+
│ ├── cjs/ # CommonJS 格式的独立函数
99+
│ │ ├── accAdd.js
100+
│ │ ├── formatDate.js
101+
│ │ └── ...
102+
│ └── umd/ # UMD 格式(浏览器直接使用)
103+
│ └── index.js
104+
```
105+
106+
### 在浏览器中使用
107+
108+
```html
109+
<!-- 通过 CDN 引入 UMD 格式 -->
110+
<script src="https://unpkg.com/@winner-fed/cloud-utils/dist/umd/index.js"></script>
111+
<script>
112+
// 全局变量 cloudUtils
113+
const result = cloudUtils.formatDate(new Date(), 'YYYY-MM-DD');
114+
const sum = cloudUtils.accAdd(0.1, 0.2);
115+
</script>
116+
```
117+
118+
## 性能对比
119+
120+
不同的导入方式适用于不同的使用场景:
121+
122+
| 导入方式 | 包体积 | 适用场景 | 示例 |
123+
|---------|-------|---------|------|
124+
| **完整引入** | ~94KB | 使用多个函数时推荐 | `import { a, b, c } from '@winner-fed/cloud-utils'` |
125+
| **独立函数导入** | ~1-3KB/函数 | 只使用少数函数时 | `import { accAdd } from '@winner-fed/cloud-utils/accAdd'` |
126+
| **直接路径导入** | ~1-3KB/函数 | 明确指定格式时 | `import { accAdd } from '@winner-fed/cloud-utils/dist/esm/accAdd.js'` |
127+
128+
### 选择建议
129+
130+
- 🚀 **使用 3 个以上函数**:推荐完整引入
131+
-**只使用 1-2 个函数**:推荐独立函数导入
132+
- 🎯 **对包体积非常敏感**:使用独立函数导入
133+
- 🛠️ **需要特定格式**:使用直接路径导入
134+
52135
## 功能分类
53136

54137
### 日期处理
@@ -90,13 +173,14 @@ const result = utils.formatDate(new Date(), 'YYYY-MM-DD');
90173
- `getPixelRatio`: 获取设备像素比
91174

92175
### 工具函数
93-
- `debounce`: 防抖函数
94-
- `throttle`: 节流函数
95176
- `deepClone`: 深拷贝
96177
- `deepMapKeys`: 深度映射键名
97178
- `promisify`: Promise 化
98179
- `timeTaken`: 计算执行时间
99180
- `anagrams`: 生成字符串的所有排列组合
181+
- `generateGUID`: 生成唯一标识符
182+
- `combineURLs`: URL 组合
183+
- `equals`: 深度比较两个值是否相等
100184

101185
### 数据处理
102186
- `merge`: 对象合并

package.json

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@
1717
"types": "./dist/index.d.cts",
1818
"default": "./dist/index.cjs"
1919
}
20-
}
20+
},
21+
"./*": {
22+
"import": {
23+
"types": "./dist/*.d.ts",
24+
"default": "./dist/esm/*.js"
25+
},
26+
"require": {
27+
"types": "./dist/*.d.ts",
28+
"default": "./dist/cjs/*.js"
29+
}
30+
},
31+
"./package.json": "./package.json",
32+
"./dist/*": "./dist/*"
2133
},
2234
"main": "./dist/index.cjs",
2335
"module": "./dist/index.js",
@@ -33,7 +45,7 @@
3345
"lib"
3446
],
3547
"scripts": {
36-
"build": "node generateExports.cjs && rslib build",
48+
"build": "node generateExports.cjs && rslib build && rm -rf dist/temp",
3749
"dev": "node generateExports.cjs && rslib build --watch",
3850
"format": "prettier --write .",
3951
"format:check": "prettier --check .",
@@ -59,10 +71,12 @@
5971
"devDependencies": {
6072
"@rslib/core": "^0.9.2",
6173
"@rspress/plugin-typedoc": "^1.43.9",
74+
"@types/glob": "^9.0.0",
6275
"@vitest/coverage-v8": "^1.3.1",
6376
"bumpp": "^10.1.0",
6477
"chalk": "^4.1.2",
6578
"gh-pages": "^6.3.0",
79+
"glob": "^11.0.3",
6680
"inquirer": "^8.2.5",
6781
"prettier": "^3.5.2",
6882
"publint": "^0.3.12",
@@ -84,7 +98,9 @@
8498
"esbuild": "^0.25.0"
8599
},
86100
"pnpm": {
87-
"onlyBuiltDependencies": ["core-js"]
101+
"onlyBuiltDependencies": [
102+
"core-js"
103+
]
88104
},
89105
"license": "MIT"
90106
}

0 commit comments

Comments
 (0)