@taro-minify-pack/helper 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
* 🧩 上传适配器定义工具(`defineUploaderAdapter`),支持类型安全地自定义适配器
|
|
11
11
|
* ☁️ 内置阿里云 OSS 上传适配器,开箱即用
|
|
12
12
|
* 🔑 基于文件内容 MD5 的资源唯一性校验
|
|
13
|
-
* 📁 递归目录文件遍历工具
|
|
14
13
|
|
|
15
14
|
## 📦 安装
|
|
16
15
|
|
|
@@ -56,8 +55,8 @@ const results = await uploadAssets({
|
|
|
56
55
|
|
|
57
56
|
| 参数名 | 是否必填 | 类型 | 默认值 | 说明 |
|
|
58
57
|
|-----------------|------|------------------------------|----------------------|------------------------|
|
|
59
|
-
| `assetsDirPath` |
|
|
60
|
-
| `upload` |
|
|
58
|
+
| `assetsDirPath` | 否 | `string` | - | 本地静态资源根目录;缺省时返回空数组 |
|
|
59
|
+
| `upload` | 否 | `Uploader` | - | 上传函数;缺省时返回空数组 |
|
|
61
60
|
| `payload` | 否 | `any` | - | 透传给 upload 函数的额外参数 |
|
|
62
61
|
| `processAssets` | 否 | `(assets) => assets` | `(assets) => assets` | 上传前对资源列表进行过滤或排序 |
|
|
63
62
|
|
|
@@ -95,7 +94,7 @@ const uploader = aliOssUploadAdapter({
|
|
|
95
94
|
bucket: 'your-bucket-name',
|
|
96
95
|
region: 'oss-cn-hangzhou',
|
|
97
96
|
bucketDir: 'taro-assets', // 可选,OSS 存储目录前缀,默认为根目录
|
|
98
|
-
customDomain: 'https://cdn.example.com', //
|
|
97
|
+
customDomain: 'https://cdn.example.com/taro-assets', // 可选,需包含 bucketDir 对应的路径
|
|
99
98
|
fileName: (info) => info.uniqueKey, // 可选,远程文件名生成函数,默认使用 uniqueKey
|
|
100
99
|
})
|
|
101
100
|
```
|
|
@@ -109,18 +108,18 @@ const uploader = aliOssUploadAdapter({
|
|
|
109
108
|
| `bucket` | 是 | `string` | - | OSS Bucket 名称 |
|
|
110
109
|
| `region` | 是 | `string` | - | OSS 区域 |
|
|
111
110
|
| `bucketDir` | 否 | `string` | `''` | OSS 存储目录前缀 |
|
|
112
|
-
| `customDomain` | 否 | `string` | - |
|
|
111
|
+
| `customDomain` | 否 | `string` | - | URL 前缀;当前实现不会自动拼接 `bucketDir`,需自行包含对应路径 |
|
|
113
112
|
| `fileName` | 否 | `(info) => string` | `(info) => info.uniqueKey` | 远程文件名生成函数 |
|
|
114
113
|
|
|
115
|
-
###
|
|
114
|
+
### 内部工具函数
|
|
115
|
+
|
|
116
|
+
以下函数仅供包内部使用,未从包入口导出,不能通过 `@taro-minify-pack/helper` 导入。
|
|
116
117
|
|
|
117
118
|
#### `travelFiles(dir)`
|
|
118
119
|
|
|
119
|
-
|
|
120
|
+
递归遍历目录,返回所有文件路径;传入绝对目录时返回绝对路径。
|
|
120
121
|
|
|
121
122
|
```ts
|
|
122
|
-
import { travelFiles } from '@taro-minify-pack/helper'
|
|
123
|
-
|
|
124
123
|
const files = travelFiles('/path/to/directory')
|
|
125
124
|
// => ['/path/to/directory/a.png', '/path/to/directory/sub/b.png']
|
|
126
125
|
```
|
|
@@ -130,8 +129,6 @@ const files = travelFiles('/path/to/directory')
|
|
|
130
129
|
基于文件内容生成 MD5 唯一标识。
|
|
131
130
|
|
|
132
131
|
```ts
|
|
133
|
-
import { generateFileUniqueKey } from '@taro-minify-pack/helper'
|
|
134
|
-
|
|
135
132
|
const key = generateFileUniqueKey('/path/to/file.png')
|
|
136
133
|
// => '2f9b7a8c9d0e1f2a3b4c5d6e7f8a9b0c'
|
|
137
134
|
```
|
|
@@ -161,7 +158,7 @@ type UploaderAdapter<T = any> = (opt: T) => Uploader
|
|
|
161
158
|
## 🧠 工作原理
|
|
162
159
|
|
|
163
160
|
1. **文件扫描**:`travelFiles` 递归遍历指定目录,收集所有文件路径
|
|
164
|
-
2.
|
|
161
|
+
2. **内容标识**:`generateFileUniqueKey` 读取文件内容并生成 MD5 值;`uploadAssets` 本身不做去重,需由 `processAssets` 或上传适配器处理
|
|
165
162
|
3. **并发上传**:`uploadAssets` 使用 `@supercharge/promise-pool` 控制并发数(默认为 2),批量上传资源
|
|
166
163
|
4. **适配器模式**:通过 `defineUploaderAdapter` 定义上传适配器,将上传逻辑与存储服务解耦,支持灵活扩展
|
|
167
164
|
|
|
@@ -184,7 +181,7 @@ type UploaderAdapter<T = any> = (opt: T) => Uploader
|
|
|
184
181
|
|
|
185
182
|
1. **内部依赖**:该包主要为 `@taro-minify-pack` 系列插件提供公共能力,一般不单独使用
|
|
186
183
|
2. **并发控制**:`uploadAssets` 默认并发数为 2,避免对存储服务造成过大压力
|
|
187
|
-
3. **文件唯一性**:基于文件内容 MD5
|
|
184
|
+
3. **文件唯一性**:基于文件内容 MD5 生成唯一标识;是否跳过上传取决于调用方的过滤逻辑和适配器实现
|
|
188
185
|
4. **OSS 适配器**:使用 `aliOssUploadAdapter` 需要确保阿里云 OSS 配置正确,且有足够的访问权限
|
|
189
186
|
|
|
190
187
|
## 📄 许可证
|