@taro-minify-pack/plugin-remote-assets 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 +16 -20
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -57,7 +57,7 @@ pnpm add @taro-minify-pack/plugin-remote-assets
|
|
|
57
57
|
|-----------------|------|--------------------------|------|------------------------|
|
|
58
58
|
| `assetsDirPath` | 是 | `string` | - | 本地静态资源根目录,用于限制可上传的资源范围 |
|
|
59
59
|
| `pathAlias` | 否 | `Record<string, string>` | `{}` | 路径别名映射,用于解析代码中的资源引用 |
|
|
60
|
-
| `uploader` |
|
|
60
|
+
| `uploader` | 否 | `Uploader` | - | 上传函数,返回包含 `remoteUrl` 的资源信息;未配置时跳过上传 |
|
|
61
61
|
|
|
62
62
|
### 🔌 自定义上传适配器
|
|
63
63
|
|
|
@@ -67,13 +67,8 @@ pnpm add @taro-minify-pack/plugin-remote-assets
|
|
|
67
67
|
#### 接口定义
|
|
68
68
|
|
|
69
69
|
```ts
|
|
70
|
-
import {
|
|
71
|
-
|
|
72
|
-
interface LocalAssetInfo {
|
|
73
|
-
localPath: string; // 本地资源文件路径
|
|
74
|
-
uniqueKey: string; // 基于文件内容生成的 MD5 值
|
|
75
|
-
ext: string; // 文件扩展名
|
|
76
|
-
}
|
|
70
|
+
import { defineUploaderAdapter } from '@taro-minify-pack/helper'
|
|
71
|
+
import type { LocalAssetInfo } from '@taro-minify-pack/helper'
|
|
77
72
|
|
|
78
73
|
export interface CustomUploadAdapterOpt {
|
|
79
74
|
// 自定义适配器参数
|
|
@@ -90,10 +85,10 @@ export const customUploadAdapter = defineUploaderAdapter((opt: CustomUploadAdapt
|
|
|
90
85
|
|
|
91
86
|
#### 内置阿里云 OSS 适配器
|
|
92
87
|
|
|
93
|
-
|
|
88
|
+
阿里云 OSS 上传适配器由 `@taro-minify-pack/helper` 提供。使用下列示例前,请额外安装该包(例如 `pnpm add @taro-minify-pack/helper`):
|
|
94
89
|
|
|
95
90
|
```ts
|
|
96
|
-
import { aliOssUploadAdapter } from '@taro-minify-pack/
|
|
91
|
+
import { aliOssUploadAdapter } from '@taro-minify-pack/helper'
|
|
97
92
|
|
|
98
93
|
interface AliOssUploadAdapterOpt {
|
|
99
94
|
customDomain?: string; // 自定义域名(可选)
|
|
@@ -112,7 +107,7 @@ interface AliOssUploadAdapterOpt {
|
|
|
112
107
|
```js
|
|
113
108
|
// config/index.js
|
|
114
109
|
const path = require('path')
|
|
115
|
-
const { aliOssUploadAdapter } = require('@taro-minify-pack/
|
|
110
|
+
const { aliOssUploadAdapter } = require('@taro-minify-pack/helper')
|
|
116
111
|
|
|
117
112
|
module.exports = {
|
|
118
113
|
plugins: [
|
|
@@ -126,7 +121,7 @@ module.exports = {
|
|
|
126
121
|
assetsDirPath: path.resolve(__dirname, '../src/assets/'),
|
|
127
122
|
// 上传适配器(以阿里云 OSS 为例)
|
|
128
123
|
uploader: aliOssUploadAdapter({
|
|
129
|
-
customDomain: 'https://your-custom-domain.com',
|
|
124
|
+
customDomain: 'https://your-custom-domain.com/taro-assets',
|
|
130
125
|
accessKeyId: 'your-access-key-id',
|
|
131
126
|
accessKeySecret: 'your-access-key-secret',
|
|
132
127
|
bucket: 'your-bucket-name',
|
|
@@ -140,32 +135,33 @@ module.exports = {
|
|
|
140
135
|
|
|
141
136
|
## 🧠 工作原理简述
|
|
142
137
|
|
|
143
|
-
1.
|
|
138
|
+
1. **构建开始**:插件在构建开始时扫描指定的静态资源目录,上传其中所有未命中内容缓存的文件(不限于已引用文件)
|
|
144
139
|
2. **资源分析**:
|
|
145
140
|
- 递归遍历目录下所有文件
|
|
146
141
|
- 基于文件内容生成 MD5 值作为唯一标识
|
|
147
142
|
- 检查缓存文件,跳过已上传的资源
|
|
148
143
|
3. **并发上传**:使用 PromisePool 并发上传新资源(默认并发数:2)
|
|
149
|
-
4. **生成缓存**:将上传结果写入缓存文件(node_modules/.cache/remote-assets
|
|
144
|
+
4. **生成缓存**:将上传结果写入缓存文件(node_modules/.cache/remote-assets.json)
|
|
150
145
|
5. **路径转换**:
|
|
151
146
|
- Babel 插件:替换 JS/TS 文件中的 import 语句
|
|
152
147
|
- PostCSS 插件:替换 CSS 文件中的 url() 引用
|
|
153
148
|
6. **构建完成**:最终产物中静态资源路径已替换为远程 URL
|
|
154
149
|
|
|
150
|
+
路径转换依赖与缓存完全匹配的本地路径,建议按示例配置绝对路径别名;当前不会自动按引用文件所在目录解析相对路径。CSS 请使用带引号的单个 `url()`,避免复合背景声明或同一声明中的多个 URL。
|
|
151
|
+
|
|
155
152
|
### 缓存机制
|
|
156
153
|
|
|
157
154
|
插件使用本地缓存文件记录已上传的资源信息,避免重复上传:
|
|
158
|
-
- 缓存文件路径:`node_modules/.cache/remote-assets
|
|
155
|
+
- 缓存文件路径:`node_modules/.cache/remote-assets.json`
|
|
159
156
|
- 缓存键:本地资源文件路径
|
|
160
|
-
-
|
|
157
|
+
- 缓存值:包含 `localPath`、`uniqueKey`、`ext`、`remoteUrl` 的资源信息
|
|
161
158
|
- 每次构建前会检查缓存,仅上传新的或修改过的资源
|
|
162
159
|
|
|
163
160
|
### 资源唯一性校验
|
|
164
161
|
|
|
165
162
|
为确保资源唯一性,插件使用文件内容的 MD5 值作为唯一标识:
|
|
166
163
|
- 即使文件名相同,只要内容不同,也会被视为不同资源
|
|
167
|
-
-
|
|
168
|
-
- 确保同一资源不会被重复存储
|
|
164
|
+
- 内置 OSS 适配器默认以 MD5 加扩展名作为远程文件名;自定义适配器可另行决定命名和去重方式
|
|
169
165
|
|
|
170
166
|
## 📌 适用场景
|
|
171
167
|
|
|
@@ -213,7 +209,7 @@ const logo = 'https://your-cdn.com/taro-assets/2f9b7a8c9d0e1f2a3b4c5d6e7f8a9b0c.
|
|
|
213
209
|
### 2. 为什么修改了资源后没有重新上传?
|
|
214
210
|
|
|
215
211
|
- 插件基于文件内容的 MD5 值判断资源是否修改
|
|
216
|
-
-
|
|
212
|
+
- 当前缓存按内容 MD5 过滤上传;重命名或复制同内容文件可能缺少新路径的映射,遇到路径未替换时请清理缓存后重建
|
|
217
213
|
- 如需强制重新上传,可删除缓存文件后重新构建
|
|
218
214
|
|
|
219
215
|
### 3. 如何处理动态引入的资源?
|
|
@@ -230,7 +226,7 @@ const logo = 'https://your-cdn.com/taro-assets/2f9b7a8c9d0e1f2a3b4c5d6e7f8a9b0c.
|
|
|
230
226
|
### 5. 如何自定义缓存文件路径?
|
|
231
227
|
|
|
232
228
|
- 当前版本不支持自定义缓存文件路径
|
|
233
|
-
- 缓存文件固定在 `node_modules/.cache/remote-assets
|
|
229
|
+
- 缓存文件固定在 `node_modules/.cache/remote-assets.json`
|
|
234
230
|
|
|
235
231
|
## 📄 许可证
|
|
236
232
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taro-minify-pack/plugin-remote-assets",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"ali-oss": "^6.23.0",
|
|
29
29
|
"mini-css-extract-plugin": "^2.9.2",
|
|
30
30
|
"postcss": "^8.5.3",
|
|
31
|
-
"@taro-minify-pack/helper": "0.1.
|
|
31
|
+
"@taro-minify-pack/helper": "0.1.6"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@tarojs/service": "^4.0.9",
|