uniky 1.0.15 → 1.0.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uniky",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "description": "uni-app 开发工具库,包含 hooks、http 请求和 vite 插件",
5
5
  "type": "module",
6
6
  "main": "./src/lib/index.ts",
@@ -20,11 +20,11 @@
20
20
  "files": [
21
21
  "src/lib",
22
22
  "src/plugin",
23
- "README.md",
24
- "CHANGELOG.md",
25
- "ARCHITECTURE.md"
23
+ "scripts",
24
+ "README.md"
26
25
  ],
27
26
  "scripts": {
27
+ "postinstall": "node scripts/postinstall.js",
28
28
  "publish:auto": "bash publish.sh"
29
29
  },
30
30
  "keywords": [
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env node
2
+ import { copyFileSync, mkdirSync, existsSync, writeFileSync } from 'fs';
3
+ import { join, dirname } from 'path';
4
+ import { fileURLToPath } from 'url';
5
+
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = dirname(__filename);
8
+
9
+ function findProjectRoot() {
10
+ let currentDir = process.cwd();
11
+ const maxDepth = 10;
12
+ let depth = 0;
13
+
14
+ while (depth < maxDepth) {
15
+ const packageJsonPath = join(currentDir, 'package.json');
16
+ if (existsSync(packageJsonPath)) {
17
+ const nodeModulesPath = join(currentDir, 'node_modules');
18
+ if (existsSync(nodeModulesPath)) {
19
+ return currentDir;
20
+ }
21
+ }
22
+ const parentDir = dirname(currentDir);
23
+ if (parentDir === currentDir) break;
24
+ currentDir = parentDir;
25
+ depth++;
26
+ }
27
+
28
+ return process.cwd();
29
+ }
30
+
31
+ function installPluginFiles() {
32
+ try {
33
+ const projectRoot = findProjectRoot();
34
+ const unikyDir = join(projectRoot, '.uniky');
35
+ const pluginDir = join(unikyDir, 'plugin');
36
+
37
+ if (!existsSync(unikyDir)) {
38
+ mkdirSync(unikyDir, { recursive: true });
39
+ }
40
+
41
+ if (!existsSync(pluginDir)) {
42
+ mkdirSync(pluginDir, { recursive: true });
43
+ }
44
+
45
+ const sourceDir = join(__dirname, '..', 'src', 'plugin');
46
+ const filesToCopy = [
47
+ 'pages.defined.ts',
48
+ 'global.defined.ts',
49
+ 'lib.defined.ts',
50
+ 'index.ts'
51
+ ];
52
+
53
+ let copiedCount = 0;
54
+ filesToCopy.forEach(file => {
55
+ const sourcePath = join(sourceDir, file);
56
+ const targetPath = join(pluginDir, file);
57
+
58
+ if (existsSync(sourcePath)) {
59
+ copyFileSync(sourcePath, targetPath);
60
+ copiedCount++;
61
+ }
62
+ });
63
+
64
+ const indexContent = `// created by zhuxietong on 2026-01-30 16:37
65
+ export * from './plugin/index.js';
66
+ `;
67
+
68
+ writeFileSync(join(unikyDir, 'index.ts'), indexContent, 'utf-8');
69
+
70
+ console.log(`[uniky] 插件文件已安装到 ${unikyDir} (${copiedCount} 个文件)`);
71
+ } catch (error) {
72
+ console.warn('[uniky] 插件文件安装失败,将在 vite 启动时自动安装:', error.message);
73
+ }
74
+ }
75
+
76
+ installPluginFiles();
@@ -1,8 +1,11 @@
1
1
  // created by zhuxietong on 2026-01-30 16:37
2
2
  import type { Plugin } from 'vite';
3
+
3
4
  import pagesDefinedPlugin from './pages.defined';
4
5
  import globalDefinedPlugin from './global.defined';
5
6
 
7
+
8
+
6
9
  export interface UnikyPluginOptions {
7
10
  enablePages?: boolean;
8
11
  enableGlobal?: boolean;
@@ -1,6 +1,6 @@
1
1
  import type { Plugin } from 'vite';
2
- import fs from 'node:fs';
3
- import path from 'node:path';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
4
 
5
5
  interface PagesJson {
6
6
  pages?: Array<{
@@ -136,7 +136,7 @@ function extractPagePaths(pagesJson: PagesJson): string[] {
136
136
  */
137
137
  function generateTypeScriptCode(pagePaths: string[]): string {
138
138
  const pagesArray = pagePaths.map((p) => `"${p}"`).join(', ');
139
-
139
+
140
140
  return `// Auto-generated by vite-plugin-pages-defined
141
141
  // Do not edit this file manually
142
142
 
@@ -156,18 +156,18 @@ interface ToRouteInterface {
156
156
  const buildUrl = (path: string, query?: Record<string, any>, json?: Record<string, any>): string => {
157
157
  const fullPath = path.startsWith('/') ? path : \`/\${path}\`;
158
158
  const params: string[] = [];
159
-
159
+
160
160
  if (query && Object.keys(query).length > 0) {
161
161
  Object.entries(query).forEach(([key, value]) => {
162
162
  params.push(\`\${encodeURIComponent(key)}=\${encodeURIComponent(String(value))}\`);
163
163
  });
164
164
  }
165
-
165
+
166
166
  if (json && Object.keys(json).length > 0) {
167
167
  const jsonStr = JSON.stringify(json);
168
168
  params.push(\`json=\${encodeURIComponent(jsonStr)}\`);
169
169
  }
170
-
170
+
171
171
  return params.length > 0 ? \`\${fullPath}?\${params.join('&')}\` : fullPath;
172
172
  };
173
173
 
@@ -203,7 +203,7 @@ export const _To = ToRoute;
203
203
  function generatePagesFile(srcDir: string, outputPath: string): void {
204
204
  try {
205
205
  const pagesJsonPath = path.resolve(srcDir, 'pages.json');
206
-
206
+
207
207
  // 读取 pages.json
208
208
  if (!fs.existsSync(pagesJsonPath)) {
209
209
  console.warn('[pages-defined] pages.json not found');
@@ -244,7 +244,7 @@ export default function pagesDefinedPlugin(): Plugin {
244
244
 
245
245
  return {
246
246
  name: 'vite-plugin-pages-defined',
247
-
247
+
248
248
  configResolved(config) {
249
249
  // 确定 src 目录和输出路径
250
250
  srcDir = path.resolve(config.root, 'src');
@@ -259,7 +259,7 @@ export default function pagesDefinedPlugin(): Plugin {
259
259
  configureServer(server) {
260
260
  // 监听 pages.json 文件变化
261
261
  const pagesJsonPath = path.resolve(srcDir, 'pages.json');
262
-
262
+
263
263
  server.watcher.add(pagesJsonPath);
264
264
  server.watcher.on('change', (file) => {
265
265
  if (file === pagesJsonPath) {
@@ -272,4 +272,4 @@ export default function pagesDefinedPlugin(): Plugin {
272
272
  generatePagesFile(srcDir, outputPath);
273
273
  }
274
274
  };
275
- }
275
+ }
package/ARCHITECTURE.md DELETED
@@ -1,233 +0,0 @@
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 DELETED
@@ -1,74 +0,0 @@
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`: 自动收集并生成全局类型定义
@@ -1,37 +0,0 @@
1
-
2
- src/autoGen/global/pages.ts 的生成逻辑
3
- ```ts
4
- // pages.json 提取到的path 整理
5
- export const _Pages = ["pages/home/index", "pages/about/index", "pages/profile/index"] as const;
6
- export type _PagePath = typeof _Pages[number];
7
-
8
-
9
- type RouteFunction = (path: _PagePath, options: { query: Record<string, any>, json: Record<string, any> }) => void;
10
- interface ToRouteInterface {
11
- navigate: RouteFunction;
12
- redirect: RouteFunction;
13
- switchTab: RouteFunction;
14
- reLaunch: RouteFunction;
15
- back: (delta?: number) => void;
16
- }
17
-
18
- const ToRoute: ToRouteInterface = {
19
- push: (path, options) => {
20
- // Implementation for navigate
21
- },
22
- redirect: (path, options) => {
23
- // Implementation for redirect
24
- },
25
- switchTab: (path, options) => {
26
- // Implementation for switchTab
27
- },
28
- reLaunch: (path, options) => {
29
- // Implementation for reLaunch
30
- },
31
- back: (delta = 1) => {
32
- // Implementation for back
33
- }
34
- };
35
-
36
- export const _To = ToRoute;
37
- ```