uniky 1.0.13 → 1.0.15

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.
Files changed (51) hide show
  1. package/ARCHITECTURE.md +233 -0
  2. package/CHANGELOG.md +74 -0
  3. package/README.md +55 -8
  4. package/package.json +13 -12
  5. package/src/lib/hook/index.ts +2 -0
  6. package/src/lib/hook/useParam.ts +114 -0
  7. package/src/lib/http/index.ts +2 -0
  8. package/src/lib/http/ky.ts +454 -0
  9. package/{dist/lib/index.js → src/lib/index.ts} +0 -1
  10. package/src/plugin/_pages.md +37 -0
  11. package/src/plugin/global.defined.ts +333 -0
  12. package/src/plugin/index.ts +29 -0
  13. package/src/plugin/lib.defined.ts +2 -0
  14. package/src/plugin/pages.defined.ts +275 -0
  15. package/dist/index.js +0 -388
  16. package/dist/index.js.map +0 -1
  17. package/dist/lib/hook/index.d.ts +0 -2
  18. package/dist/lib/hook/index.d.ts.map +0 -1
  19. package/dist/lib/hook/index.js +0 -3
  20. package/dist/lib/hook/index.js.map +0 -1
  21. package/dist/lib/hook/useParam.d.ts +0 -19
  22. package/dist/lib/hook/useParam.d.ts.map +0 -1
  23. package/dist/lib/hook/useParam.js +0 -102
  24. package/dist/lib/hook/useParam.js.map +0 -1
  25. package/dist/lib/http/index.d.ts +0 -2
  26. package/dist/lib/http/index.d.ts.map +0 -1
  27. package/dist/lib/http/index.js +0 -3
  28. package/dist/lib/http/index.js.map +0 -1
  29. package/dist/lib/http/ky.d.ts +0 -53
  30. package/dist/lib/http/ky.d.ts.map +0 -1
  31. package/dist/lib/http/ky.js +0 -323
  32. package/dist/lib/http/ky.js.map +0 -1
  33. package/dist/lib/index.d.ts +0 -3
  34. package/dist/lib/index.d.ts.map +0 -1
  35. package/dist/lib/index.js.map +0 -1
  36. package/dist/plugin/global.defined.d.ts +0 -3
  37. package/dist/plugin/global.defined.d.ts.map +0 -1
  38. package/dist/plugin/global.defined.js +0 -284
  39. package/dist/plugin/global.defined.js.map +0 -1
  40. package/dist/plugin/index.d.ts +0 -11
  41. package/dist/plugin/index.d.ts.map +0 -1
  42. package/dist/plugin/index.js +0 -16
  43. package/dist/plugin/index.js.map +0 -1
  44. package/dist/plugin/lib.defined.d.ts +0 -2
  45. package/dist/plugin/lib.defined.d.ts.map +0 -1
  46. package/dist/plugin/lib.defined.js +0 -2
  47. package/dist/plugin/lib.defined.js.map +0 -1
  48. package/dist/plugin/pages.defined.d.ts +0 -6
  49. package/dist/plugin/pages.defined.d.ts.map +0 -1
  50. package/dist/plugin/pages.defined.js +0 -230
  51. package/dist/plugin/pages.defined.js.map +0 -1
@@ -0,0 +1,233 @@
1
+ # uniky 架构说明
2
+
3
+ ## 📦 混合发布模式
4
+
5
+ 从 v1.0.13 开始,`uniky` 采用**混合发布模式**,结合了源码发布和编译发布的优势。
6
+
7
+ ## 🗂️ 目录结构
8
+
9
+ ```
10
+ uniky/
11
+ ├── src/
12
+ │ ├── lib/ # TypeScript 源码(发布)
13
+ │ │ ├── hook/
14
+ │ │ │ ├── index.ts
15
+ │ │ │ └── useParam.ts
16
+ │ │ ├── http/
17
+ │ │ │ ├── index.ts
18
+ │ │ │ └── ky.ts
19
+ │ │ └── index.ts
20
+ │ │
21
+ │ └── plugin/ # TypeScript 源码(编译)
22
+ │ ├── index.ts
23
+ │ ├── pages.defined.ts
24
+ │ └── global.defined.ts
25
+
26
+ └── dist/
27
+ └── plugin/ # 编译后的 JS(发布)
28
+ ├── index.js
29
+ ├── index.d.ts
30
+ ├── pages.defined.js
31
+ ├── pages.defined.d.ts
32
+ ├── global.defined.js
33
+ └── global.defined.d.ts
34
+ ```
35
+
36
+ ## 📋 发布内容
37
+
38
+ 根据 `package.json` 的 `files` 字段:
39
+
40
+ ```json
41
+ "files": [
42
+ "src/lib", // ← lib 的 TS 源码
43
+ "dist/plugin", // ← plugin 的编译 JS
44
+ "README.md"
45
+ ]
46
+ ```
47
+
48
+ ## 🎯 为什么采用混合模式?
49
+
50
+ ### lib 部分 - TypeScript 源码
51
+
52
+ **原因**:
53
+ - 在运行时被用户代码导入使用
54
+ - 用户的 Vite 构建工具会处理 TS 编译
55
+ - peerDependencies (vue, @dcloudio/uni-app) 由用户项目提供
56
+
57
+ **优势**:
58
+ - ✅ 避免依赖冲突 - 使用项目自己的依赖版本
59
+ - ✅ 更好的类型支持 - 直接使用源码类型定义
60
+ - ✅ 调试更方便 - 可以直接查看和调试源码
61
+ - ✅ 体积更小 - 不包含重复的编译代码
62
+
63
+ **使用示例**:
64
+ ```typescript
65
+ // 在用户项目代码中
66
+ import { useParam } from 'uniky'; // ← 导入 src/lib/hook/useParam.ts
67
+
68
+ const params = useParam();
69
+ ```
70
+
71
+ ### plugin 部分 - 编译后的 JavaScript
72
+
73
+ **原因**:
74
+ - 在 Vite 配置文件中使用
75
+ - Vite 加载配置文件时使用 esbuild
76
+ - esbuild 在某些环境会尝试用 `require()` 加载模块
77
+ - 编译为 JS 可以避免 ESM 兼容性问题
78
+
79
+ **优势**:
80
+ - ✅ 兼容性好 - 支持各种 Node.js 环境
81
+ - ✅ 加载更快 - 无需实时编译
82
+ - ✅ 类型完整 - 包含 `.d.ts` 类型定义
83
+
84
+ **使用示例**:
85
+ ```typescript
86
+ // vite.config.ts
87
+ import { unikyPlugin } from 'uniky/plugin'; // ← 导入 dist/plugin/index.js
88
+
89
+ export default defineConfig({
90
+ plugins: [
91
+ uni(),
92
+ ...unikyPlugin() // ← 编译后的 JS 插件
93
+ ]
94
+ });
95
+ ```
96
+
97
+ ## ⚙️ package.json 配置
98
+
99
+ ```json
100
+ {
101
+ "type": "module",
102
+ "main": "./src/lib/index.ts",
103
+ "module": "./src/lib/index.ts",
104
+ "types": "./src/lib/index.ts",
105
+ "exports": {
106
+ ".": {
107
+ "types": "./src/lib/index.ts",
108
+ "import": "./src/lib/index.ts"
109
+ },
110
+ "./plugin": {
111
+ "types": "./dist/plugin/index.d.ts",
112
+ "import": "./dist/plugin/index.js"
113
+ }
114
+ }
115
+ }
116
+ ```
117
+
118
+ ### 解读
119
+
120
+ - **`"."`** - 主入口(lib)
121
+ - 指向 TypeScript 源码
122
+ - 用户项目构建工具会处理
123
+
124
+ - **`"./plugin"`** - 插件入口
125
+ - 指向编译后的 JavaScript
126
+ - 包含类型定义文件
127
+
128
+ ## 🔧 构建配置
129
+
130
+ ### tsconfig.json
131
+
132
+ ```json
133
+ {
134
+ "compilerOptions": {
135
+ "target": "ES2020",
136
+ "module": "ESNext",
137
+ "outDir": "./dist",
138
+ // ... 其他配置
139
+ },
140
+ "include": ["src/plugin/**/*"], // ← 只编译 plugin
141
+ "exclude": ["src/lib/**/*"] // ← 排除 lib
142
+ }
143
+ ```
144
+
145
+ ### 构建脚本
146
+
147
+ ```json
148
+ {
149
+ "scripts": {
150
+ "build": "tsc", // 只编译 plugin
151
+ "prepublishOnly": "npm run build" // 发布前自动构建
152
+ }
153
+ }
154
+ ```
155
+
156
+ ## 🚀 工作流程
157
+
158
+ ### 开发流程
159
+
160
+ 1. 修改 `src/lib/*` - 无需编译
161
+ 2. 修改 `src/plugin/*` - 需要运行 `npm run build`
162
+ 3. 使用 `npm link` 链接到测试项目
163
+ 4. 清除 Vite 缓存:`rm -rf node_modules/.vite`
164
+
165
+ ### 发布流程
166
+
167
+ ```bash
168
+ # 方式一:自动发布脚本
169
+ npm run publish:auto
170
+
171
+ # 方式二:手动发布
172
+ npm run build # 编译 plugin
173
+ npm publish # 发布到 npm
174
+ ```
175
+
176
+ `prepublishOnly` 钩子确保发布前自动编译。
177
+
178
+ ## 💡 最佳实践
179
+
180
+ ### 对于库用户
181
+
182
+ 1. **使用 lib**:
183
+ ```typescript
184
+ import { useParam } from 'uniky';
185
+ ```
186
+ - 确保项目支持 TypeScript
187
+ - Vite 会自动处理编译
188
+ - 不需要任何特殊配置
189
+
190
+ 2. **使用 plugin**:
191
+ ```typescript
192
+ import { unikyPlugin } from 'uniky/plugin';
193
+ ```
194
+ - 直接使用,无需担心 ESM 问题
195
+ - 已编译为 JS,兼容性好
196
+
197
+ ### 对于库开发者
198
+
199
+ 1. **修改 lib 代码**:
200
+ - 直接修改 `src/lib/**/*`
201
+ - 无需编译
202
+
203
+ 2. **修改 plugin 代码**:
204
+ - 修改 `src/plugin/**/*`
205
+ - 运行 `npm run build`
206
+ - 测试编译后的代码
207
+
208
+ 3. **发布新版本**:
209
+ - 运行 `npm run publish:auto`
210
+ - 自动编译 + 发布
211
+
212
+ ## 🎓 技术要点
213
+
214
+ 1. **ESM 模块系统**
215
+ - `"type": "module"` 声明包为 ESM
216
+ - 所有导入使用 `.js` 扩展名
217
+ - Node.js 内置模块使用 `node:` 前缀
218
+
219
+ 2. **TypeScript 编译**
220
+ - 只编译 plugin 部分
221
+ - 生成 `.js` 和 `.d.ts` 文件
222
+ - 保持 ESM 模块格式
223
+
224
+ 3. **依赖管理**
225
+ - peerDependencies 避免依赖冲突
226
+ - 用户项目提供实际依赖
227
+ - 开发时安装 devDependencies
228
+
229
+ ## 📚 参考资料
230
+
231
+ - [Node.js ESM 文档](https://nodejs.org/api/esm.html)
232
+ - [TypeScript 模块解析](https://www.typescriptlang.org/docs/handbook/module-resolution.html)
233
+ - [Vite 插件开发](https://vitejs.dev/guide/api-plugin.html)
package/CHANGELOG.md ADDED
@@ -0,0 +1,74 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ ## [1.0.13] - 2024-01-30
6
+
7
+ ### Changed
8
+ - **重要变更:采用混合发布模式**
9
+ - **lib 部分**:保持 TypeScript 源码(`src/lib/**/*`),由用户项目构建工具处理
10
+ - 避免依赖冲突,使用项目自己的 vue、@dcloudio/uni-app 等依赖
11
+ - 更好的类型支持和调试体验
12
+ - **plugin 部分**:编译为 JavaScript(`dist/plugin/**/*`)
13
+ - 解决 Vite 配置文件加载时的 ESM 兼容性问题
14
+ - 包含完整的类型定义文件(`.d.ts`)
15
+ - 更新 `tsconfig.json`,只编译 `src/plugin` 目录
16
+ - 更新 `package.json` exports 配置,分别指向源码和编译文件
17
+
18
+ ### Fixed
19
+ - **彻底修复 ESM 兼容性问题**
20
+ - 修复了 `uniky/plugin` 在 Vite 配置文件加载时被 `require()` 导致的 ESM 错误
21
+ - 将 Node.js 内置模块导入改为使用 `node:` 前缀 (如 `node:fs`, `node:path`)
22
+ - 在所有相对导入中添加 `.js` 扩展名,确保 ESM 环境下正确解析
23
+ - 验证在 uni-shell 项目中正常运行,无警告
24
+
25
+ ### Added
26
+ - 添加了 `tsconfig.json` 配置文件,提供完整的 TypeScript 编译配置
27
+ - 在 README 中新增"架构说明"章节,详细说明混合发布模式的优势
28
+ - 在 README 中新增"故障排除"章节,包含 7 种常见 ESM 错误的解决方案
29
+ - 在 README 中新增"开发说明"章节,包含本地开发和发布流程
30
+ - 为 uni-shell 项目创建 `UPDATE_UNIKY.md` 更新指南
31
+
32
+ ### 如何更新
33
+
34
+ 在使用 `uniky` 的项目中执行:
35
+
36
+ ```bash
37
+ # 如果是从 npm 安装
38
+ npm update uniky
39
+
40
+ # 如果是本地开发使用 npm link
41
+ cd /path/to/uniky
42
+ npm run build
43
+ npm link
44
+
45
+ cd /path/to/your-project
46
+ npm link uniky
47
+ rm -rf node_modules/.vite # 清除 Vite 缓存
48
+ ```
49
+
50
+ ## [1.0.12] - 2024-01-30
51
+
52
+ ### Changed
53
+ - 全部编译为 JavaScript(已在 1.0.13 中改为混合模式)
54
+
55
+ ## [1.0.11] - 2024-01-30
56
+
57
+ ### Fixed
58
+ - 初步 ESM 兼容性修复(未完全解决)
59
+
60
+ ### Added
61
+ - 添加了基础的 TypeScript 和 ESM 支持
62
+ - 在 README 中新增故障排除章节
63
+
64
+ ### Changed
65
+ - 优化了 `package.json` 配置,添加 `sideEffects: false`
66
+
67
+ ## [1.0.10] - 2024-01-30
68
+
69
+ ### Initial Release
70
+ - 提供 uni-app 常用 hooks
71
+ - 提供 HTTP 请求封装
72
+ - 提供 Vite 插件:
73
+ - `pagesDefinedPlugin`: 从 pages.json 生成类型安全的路由定义
74
+ - `globalDefinedPlugin`: 自动收集并生成全局类型定义
package/README.md CHANGED
@@ -4,10 +4,10 @@ uni-app 开发工具库,提供常用的 hooks、http 请求封装和 vite 插
4
4
 
5
5
  ## 特性
6
6
 
7
- - ✅ 直接使用 TypeScript 源码,无需编译
7
+ - ✅ 混合发布模式:lib 使用 TS 源码,plugin 编译为 JS
8
8
  - ✅ 与用户项目共享依赖,避免冲突
9
9
  - ✅ 完整的类型支持
10
- - ✅ 开箱即用的 Vite 插件
10
+ - ✅ 开箱即用的 Vite 插件,兼容各种构建环境
11
11
 
12
12
  ## 安装
13
13
 
@@ -207,14 +207,26 @@ npm link uniky
207
207
 
208
208
  ## 架构说明
209
209
 
210
- 本库直接发布 TypeScript 源码,不进行编译。这样做的好处:
210
+ 本库采用**混合发布模式**:
211
211
 
212
- 1. **避免依赖冲突**:使用项目自己的 vue、@dcloudio/uni-app 等依赖
213
- 2. **类型支持更好**:直接使用源码类型定义
214
- 3. **调试更方便**:可以直接查看和调试源码
215
- 4. **体积更小**:不包含编译后的代码
212
+ ### lib 部分 - TypeScript 源码
213
+ - 直接发布 TS 源码(`src/lib/**/*`)
214
+ - 由用户项目的构建工具(Vite)处理编译
215
+ - 优势:
216
+ - 避免依赖冲突,使用项目自己的 vue、@dcloudio/uni-app 等依赖
217
+ - 类型支持更好,直接使用源码类型定义
218
+ - 调试更方便,可以直接查看和调试源码
219
+ - 体积更小,不包含编译后的代码
216
220
 
217
- 用户项目的构建工具(如 Vite)会自动处理这些 TypeScript 文件的编译。
221
+ ### plugin 部分 - 编译后的 JavaScript
222
+ - 编译为 JS 文件(`dist/plugin/**/*`)
223
+ - 包含完整的类型定义(`.d.ts` 文件)
224
+ - 原因:
225
+ - Vite 配置文件在加载时使用 esbuild
226
+ - esbuild 在某些环境下会尝试用 `require()` 加载模块
227
+ - 编译为 JS 可以避免 ESM 兼容性问题
228
+
229
+ 这种混合模式结合了两种方式的优势,既保持了库的灵活性,又确保了插件的兼容性。
218
230
 
219
231
  ## 发布
220
232
 
@@ -254,6 +266,41 @@ npm run publish:auto
254
266
  5. 确认后发布到 npm
255
267
  6. 可选自动提交到 git
256
268
 
269
+ ## 开发说明
270
+
271
+ ### 本地开发
272
+
273
+ 1. 克隆仓库
274
+ ```bash
275
+ git clone https://github.com/zhuxietong/uniky.git
276
+ cd uniky
277
+ ```
278
+
279
+ 2. 安装依赖
280
+ ```bash
281
+ npm install
282
+ ```
283
+
284
+ 3. 构建 plugin(仅编译 plugin 部分)
285
+ ```bash
286
+ npm run build
287
+ ```
288
+
289
+ 4. 链接到本地项目
290
+ ```bash
291
+ npm link
292
+ cd /path/to/your-project
293
+ npm link uniky
294
+ ```
295
+
296
+ ### 发布流程
297
+
298
+ ```bash
299
+ npm run publish:auto
300
+ ```
301
+
302
+ 发布前会自动执行 `npm run build`,编译 plugin 部分。
303
+
257
304
  ## License
258
305
 
259
306
  MIT
package/package.json CHANGED
@@ -1,29 +1,30 @@
1
1
  {
2
2
  "name": "uniky",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "uni-app 开发工具库,包含 hooks、http 请求和 vite 插件",
5
5
  "type": "module",
6
- "main": "./dist/lib/index.js",
7
- "module": "./dist/lib/index.js",
8
- "types": "./dist/lib/index.d.ts",
6
+ "main": "./src/lib/index.ts",
7
+ "module": "./src/lib/index.ts",
8
+ "types": "./src/lib/index.ts",
9
9
  "exports": {
10
10
  ".": {
11
- "types": "./dist/lib/index.d.ts",
12
- "import": "./dist/lib/index.js"
11
+ "types": "./src/lib/index.ts",
12
+ "import": "./src/lib/index.ts"
13
13
  },
14
14
  "./plugin": {
15
- "types": "./dist/plugin/index.d.ts",
16
- "import": "./dist/plugin/index.js"
15
+ "types": "./src/plugin/index.ts",
16
+ "import": "./src/plugin/index.ts"
17
17
  }
18
18
  },
19
19
  "sideEffects": false,
20
20
  "files": [
21
- "dist",
22
- "README.md"
21
+ "src/lib",
22
+ "src/plugin",
23
+ "README.md",
24
+ "CHANGELOG.md",
25
+ "ARCHITECTURE.md"
23
26
  ],
24
27
  "scripts": {
25
- "build": "tsc",
26
- "prepublishOnly": "npm run build",
27
28
  "publish:auto": "bash publish.sh"
28
29
  },
29
30
  "keywords": [
@@ -0,0 +1,2 @@
1
+ // created by zhuxietong on 2026-01-30 16:37
2
+ export * from './useParam.js';
@@ -0,0 +1,114 @@
1
+ import { onLoad } from '@dcloudio/uni-app'
2
+ import { onMounted, ref } from 'vue'
3
+
4
+ export const useParam = <Q extends object = {}, J extends object = {}>(
5
+ callBack?: (params: { option: Partial<Q & J>; query: Partial<Q>; json: Partial<J> }) => void,
6
+ ) => {
7
+ const query = ref<Partial<Q>>({})
8
+ const json = ref<Partial<J>>({})
9
+ const param = ref<Partial<Q & J>>({})
10
+
11
+ onLoad((op?: AnyObject) => {
12
+ let jsonObj: any = {}
13
+ let queryObj: any = {}
14
+
15
+ if (!op || Object.keys(op).length === 0) {
16
+ param.value = {} as any
17
+ query.value = {} as any
18
+ json.value = {} as any
19
+ callBack?.({ option: {} as any, query: {} as any, json: {} as any })
20
+ return
21
+ }
22
+
23
+ const decodedOp: { [k: string]: any } = {}
24
+ for (const key in op) {
25
+ if (typeof op[key] === 'string') {
26
+ if (key === 'json') {
27
+ try {
28
+ decodedOp[key] = JSON.parse(decodeURIComponent(op[key]))
29
+ jsonObj = decodedOp[key]
30
+ json.value = jsonObj
31
+ } catch (e) {}
32
+ continue
33
+ }
34
+ decodedOp[key] = decodeURIComponent(op[key])
35
+ } else {
36
+ decodedOp[key] = op[key]
37
+ }
38
+ }
39
+
40
+ try {
41
+ queryObj = { ...decodedOp }
42
+ delete queryObj.json
43
+ query.value = queryObj
44
+ } catch (e) {}
45
+
46
+ param.value = { ...jsonObj, ...queryObj }
47
+ callBack?.({ option: param.value as any, query: query.value as any, json: json.value as any })
48
+ })
49
+
50
+ return {
51
+ query,
52
+ param,
53
+ json,
54
+ }
55
+ }
56
+
57
+ export const useMountedLoad = <Q extends object = {}, J extends object = {}>(
58
+ callBack?: (params: { option: Partial<Q & J>; query: Partial<Q>; json: Partial<J> }) => void,
59
+ ) => {
60
+ const query = ref<Partial<Q>>({})
61
+ const json = ref<Partial<J>>({})
62
+ const param = ref<Partial<Q & J>>({})
63
+
64
+ onLoad((op?: AnyObject) => {
65
+ let jsonObj: any = {}
66
+ let queryObj: any = {}
67
+
68
+ if (!op || Object.keys(op).length === 0) {
69
+ param.value = {} as any
70
+ query.value = {} as any
71
+ json.value = {} as any
72
+ return
73
+ }
74
+
75
+ const decodedOp: { [k: string]: any } = {}
76
+ for (const key in op) {
77
+ if (typeof op[key] === 'string') {
78
+ if (key === 'json') {
79
+ try {
80
+ decodedOp[key] = JSON.parse(decodeURIComponent(op[key]))
81
+ jsonObj = decodedOp[key]
82
+ json.value = jsonObj
83
+ } catch (e) {}
84
+ continue
85
+ }
86
+ decodedOp[key] = decodeURIComponent(op[key])
87
+ } else {
88
+ decodedOp[key] = op[key]
89
+ }
90
+ }
91
+
92
+ try {
93
+ queryObj = { ...decodedOp }
94
+ delete queryObj.json
95
+ query.value = queryObj
96
+ } catch (e) {}
97
+
98
+ param.value = { ...jsonObj, ...queryObj }
99
+ })
100
+
101
+ onMounted(() => {
102
+ callBack?.({
103
+ option: (param.value || {}) as any,
104
+ query: (query.value || {}) as any,
105
+ json: (json.value || {}) as any
106
+ })
107
+ })
108
+
109
+ return {
110
+ query,
111
+ param,
112
+ json,
113
+ }
114
+ }
@@ -0,0 +1,2 @@
1
+ // created by zhuxietong on 2026-01-30 16:37
2
+ export * from './ky.js';