uniky 1.0.10 → 1.0.12

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 CHANGED
@@ -102,6 +102,109 @@ import { installGlobals } from './autoGen/global.install';
102
102
  installGlobals();
103
103
  ```
104
104
 
105
+ ## 故障排除
106
+
107
+ ### ESM 相关错误
108
+
109
+ 如果遇到类似以下错误:
110
+
111
+ ```
112
+ ERROR: [plugin: externalize-deps] Failed to resolve "uniky/plugin".
113
+ This package is ESM only but it was tried to load by `require`.
114
+ ```
115
+
116
+ **解决方案:**
117
+
118
+ 1. **确保项目 package.json 配置正确**
119
+
120
+ 在使用 `uniky` 的项目中,确保 `package.json` 包含:
121
+
122
+ ```json
123
+ {
124
+ "type": "module"
125
+ }
126
+ ```
127
+
128
+ 2. **确保 vite.config.ts 使用 ES 模块语法**
129
+
130
+ ```typescript
131
+ // ✅ 正确 - 使用 import
132
+ import { defineConfig } from 'vite';
133
+ import uni from '@dcloudio/vite-plugin-uni';
134
+ import { unikyPlugin } from 'uniky/plugin';
135
+
136
+ export default defineConfig({
137
+ plugins: [uni(), ...unikyPlugin()]
138
+ });
139
+
140
+ // ❌ 错误 - 不要使用 require
141
+ const { unikyPlugin } = require('uniky/plugin');
142
+ ```
143
+
144
+ 3. **配置 Vite 以正确处理 TypeScript 源码**
145
+
146
+ 如果使用的是 TypeScript 源码版本,确保 `vite.config.ts` 中正确配置:
147
+
148
+ ```typescript
149
+ import { defineConfig } from 'vite';
150
+ import uni from '@dcloudio/vite-plugin-uni';
151
+ import { unikyPlugin } from 'uniky/plugin';
152
+
153
+ export default defineConfig({
154
+ plugins: [uni(), ...unikyPlugin()],
155
+ optimizeDeps: {
156
+ // 排除 uniky 包,让 Vite 直接处理其 TS 源码
157
+ exclude: ['uniky']
158
+ }
159
+ });
160
+ ```
161
+
162
+ 4. **检查 tsconfig.json 配置**
163
+
164
+ 确保项目的 `tsconfig.json` 支持 ESM:
165
+
166
+ ```json
167
+ {
168
+ "compilerOptions": {
169
+ "module": "ESNext",
170
+ "moduleResolution": "bundler",
171
+ "esModuleInterop": true,
172
+ "allowSyntheticDefaultImports": true
173
+ }
174
+ }
175
+ ```
176
+
177
+ 5. **检查 Node.js 版本**
178
+
179
+ 确保使用 Node.js 16+ 版本,该版本对 ESM 支持更好。
180
+
181
+ 6. **清除缓存后重试**
182
+
183
+ ```bash
184
+ # 清除 node_modules 和 lock 文件
185
+ rm -rf node_modules package-lock.json
186
+ npm install
187
+
188
+ # 或使用 pnpm/yarn
189
+ rm -rf node_modules pnpm-lock.yaml
190
+ pnpm install
191
+
192
+ # 清除 Vite 缓存
193
+ rm -rf node_modules/.vite
194
+ ```
195
+
196
+ 7. **本地开发时使用 npm link**
197
+
198
+ 如果是本地开发调试 `uniky` 包:
199
+
200
+ ```bash
201
+ # 在 uniky 目录
202
+ npm link
203
+
204
+ # 在使用项目目录
205
+ npm link uniky
206
+ ```
207
+
105
208
  ## 架构说明
106
209
 
107
210
  本库直接发布 TypeScript 源码,不进行编译。这样做的好处:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uniky",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "uni-app 开发工具库,包含 hooks、http 请求和 vite 插件",
5
5
  "type": "module",
6
6
  "main": "./src/lib/index.ts",
@@ -16,6 +16,7 @@
16
16
  "import": "./src/plugin/index.ts"
17
17
  }
18
18
  },
19
+ "sideEffects": false,
19
20
  "files": [
20
21
  "src",
21
22
  "README.md"
@@ -3,8 +3,8 @@
3
3
  // 以便在项目中全局使用这些类型定义,避免了手动维护全局类型定义文件的麻烦。
4
4
 
5
5
  import type { Plugin } from 'vite';
6
- import * as fs from 'fs';
7
- import * as path from 'path';
6
+ import * as fs from 'node:fs';
7
+ import * as path from 'node:path';
8
8
 
9
9
  interface ExportInfo {
10
10
  name: string;
@@ -1,6 +1,6 @@
1
1
  import type { Plugin } from 'vite';
2
- import fs from 'fs';
3
- import path from 'path';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
4
 
5
5
  interface PagesJson {
6
6
  pages?: Array<{