cmpt-huitu-cli 1.0.2 → 1.0.4

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": "cmpt-huitu-cli",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "type": "module",
5
5
  "description": "慧图前端项目工程化 CLI 工具",
6
6
  "main": "index.js",
@@ -0,0 +1,164 @@
1
+ # ESM 环境说明
2
+
3
+ ## 什么是 ESM?
4
+
5
+ **ESM (ES Modules)** 是 JavaScript 的现代模块系统,使用 `import/export` 语法。
6
+
7
+ ### ESM vs CommonJS
8
+
9
+ | 特性 | ESM (ES Modules) | CommonJS |
10
+ | -------------- | ---------------------------------------------- | ----------------------------------------------- |
11
+ | **语法** | `import/export` | `require/module.exports` |
12
+ | **加载时机** | 编译时静态分析 | 运行时动态加载 |
13
+ | **支持** | 现代浏览器、Node.js 12+ | Node.js 传统方式 |
14
+ | **文件扩展名** | `.mjs` 或 `package.json` 中 `"type": "module"` | `.js` 或 `package.json` 中 `"type": "commonjs"` |
15
+
16
+ ## 为什么项目使用 ESM?
17
+
18
+ ### 1. 项目配置
19
+
20
+ 在 `package.json` 中:
21
+
22
+ ```json
23
+ {
24
+ "type": "module"
25
+ }
26
+ ```
27
+
28
+ 这表示项目使用 ESM 模块系统。
29
+
30
+ ### 2. Vite 默认使用 ESM
31
+
32
+ Vite 是一个基于 ESM 的构建工具:
33
+
34
+ - 开发时:直接使用浏览器原生的 ESM
35
+ - 构建时:将代码转换为优化的 ESM 格式
36
+
37
+ ### 3. 现代标准
38
+
39
+ ESM 是 JavaScript 的官方标准,未来趋势。
40
+
41
+ ## 为什么需要特殊写法?
42
+
43
+ ### 问题:CommonJS 模块兼容性
44
+
45
+ 某些第三方库(如 `dayjs`、`xe-utils`)可能是 **CommonJS 格式**,在 ESM 环境下导入时可能出现问题:
46
+
47
+ ```javascript
48
+ // CommonJS 模块导出方式
49
+ module.exports = {
50
+ default: function() { ... },
51
+ // 其他导出
52
+ }
53
+
54
+ // ESM 导入时
55
+ import dayjs from 'dayjs' // 可能获取不到 default
56
+ ```
57
+
58
+ ### 解决方案
59
+
60
+ #### 1. Vite 自动处理(推荐)
61
+
62
+ Vite 会自动将 CommonJS 模块转换为 ESM,**正常情况下不需要特殊写法**:
63
+
64
+ ```javascript
65
+ // ✅ 正常写法(应该可以工作)
66
+ import dayjs from 'dayjs'
67
+ ```
68
+
69
+ #### 2. 配置 optimizeDeps
70
+
71
+ 在 `vite.config.js` 中配置预构建:
72
+
73
+ ```javascript
74
+ optimizeDeps: {
75
+ include: ['dayjs', 'xe-utils'], // 告诉 Vite 预构建这些模块
76
+ }
77
+ ```
78
+
79
+ #### 3. 特殊写法(仅在必要时)
80
+
81
+ 如果自动转换失败,才需要使用兼容写法:
82
+
83
+ ```javascript
84
+ // 兼容写法(仅在必要时)
85
+ import * as dayjsModule from 'dayjs'
86
+ const dayjs = dayjsModule.default || dayjsModule
87
+ ```
88
+
89
+ ## 实际情况
90
+
91
+ ### dayjs 支持情况
92
+
93
+ `dayjs` 实际上**同时支持 CommonJS 和 ESM**:
94
+
95
+ - 有 `main` 字段(CommonJS)
96
+ - 有 `module` 字段(ESM,如果提供)
97
+ - 现代版本应该可以直接使用 `import dayjs from 'dayjs'`
98
+
99
+ ### 为什么会出现问题?
100
+
101
+ 可能的原因:
102
+
103
+ 1. **Vite 预构建未完成**
104
+ - 首次启动时,Vite 需要预构建依赖
105
+ - 如果预构建失败或未完成,可能出现导入错误
106
+
107
+ 2. **缓存问题**
108
+ - Vite 缓存了错误的构建结果
109
+ - 需要清理 `node_modules/.vite` 缓存
110
+
111
+ 3. **依赖未正确安装**
112
+ - `dayjs` 未安装或版本不兼容
113
+ - pnpm 的依赖提升机制导致路径问题
114
+
115
+ 4. **包版本问题**
116
+ - 某些旧版本的 `dayjs` 可能不完全支持 ESM
117
+
118
+ ## 最佳实践
119
+
120
+ ### 1. 优先使用标准写法
121
+
122
+ ```javascript
123
+ // ✅ 推荐:标准 ESM 导入
124
+ import dayjs from 'dayjs'
125
+ ```
126
+
127
+ ### 2. 确保 Vite 配置正确
128
+
129
+ ```javascript
130
+ // vite.config.js
131
+ export default {
132
+ optimizeDeps: {
133
+ include: ['dayjs'], // 确保预构建
134
+ },
135
+ }
136
+ ```
137
+
138
+ ### 3. 清理缓存
139
+
140
+ 如果出现问题:
141
+
142
+ ```bash
143
+ rm -rf node_modules/.vite
144
+ pnpm dev
145
+ ```
146
+
147
+ ### 4. 仅在必要时使用兼容写法
148
+
149
+ 如果标准写法不工作,再使用兼容写法。
150
+
151
+ ## 总结
152
+
153
+ **正常情况下,不需要特殊写法!**
154
+
155
+ - ESM 是标准,应该直接使用 `import dayjs from 'dayjs'`
156
+ - Vite 会自动处理 CommonJS 到 ESM 的转换
157
+ - 如果出现问题,通常是配置或缓存问题,而不是语法问题
158
+ - 特殊写法只是**临时解决方案**,应该优先排查根本原因
159
+
160
+ ## 相关资源
161
+
162
+ - [MDN: ES Modules](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Modules)
163
+ - [Vite: 依赖预构建](https://cn.vitejs.dev/guide/dep-pre-bundling.html)
164
+ - [dayjs 官方文档](https://day.js.org/)
@@ -25,6 +25,7 @@
25
25
  "lodash-es": "^4.17.21",
26
26
  "nprogress": "^0.2.0",
27
27
  "pinia": "^3.0.3",
28
+ "vxe-table": "^4.16.20",
28
29
  "vue": "^3.5.18",
29
30
  "vue-router": "^4.5.1"
30
31
  },
@@ -96,6 +96,8 @@ export default defineConfig(({ mode, command }) => {
96
96
  },
97
97
  // import引用可以忽略文件后缀如:import router from './router' === import router from './router/index.js'
98
98
  extensions: ['.mjs', '.ts', '.jsx', '.tsx', '.json', '.js', '.vue', '.css', '.scss'],
99
+ // 确保正确解析 CommonJS 模块,避免重复打包
100
+ dedupe: ['vxe-table', 'xe-utils'],
99
101
  },
100
102
 
101
103
  // CSS预处理器配置
@@ -120,6 +122,13 @@ export default defineConfig(({ mode, command }) => {
120
122
  sourcemap: !isProduction, // 开发保留 sourcemap
121
123
  target: ['es2015', 'edge88', 'chrome87'], // 产物目标浏览器
122
124
  minify: 'esbuild', // 代码压缩,vite默认使用esbuild,如果需要使用terser,需要安装npm install terser@vitejs/plugin-terser -D
125
+ // CommonJS 选项配置
126
+ commonjsOptions: {
127
+ // 将 CommonJS 模块转换为 ES 模块
128
+ transformMixedEsModules: true,
129
+ // 包含的 CommonJS 模块
130
+ include: [/xe-utils/, /vxe-table/],
131
+ },
123
132
  // 下面要配合 minify: 'terser',才能生效
124
133
  // terserOptions: isProduction
125
134
  // ? {
@@ -146,6 +155,7 @@ export default defineConfig(({ mode, command }) => {
146
155
  manualChunks: {
147
156
  'x-vuelib': ['vue', 'vue-router', 'pinia', 'axios', 'dayjs', 'lodash-es'],
148
157
  'x-eleuilib': ['element-plus'],
158
+ 'x-vxetable': ['vxe-table', 'xe-utils'],
149
159
  },
150
160
  experimentalMinChunkSize: 2048, // 该选项用于为代码分割设置一个以字节为单位的最小 chunk 大小
151
161
  // inlineDynamicImports: true // 该选项用于内联动态引入,该选项只在单页面应用的时候起作用
@@ -179,9 +189,20 @@ export default defineConfig(({ mode, command }) => {
179
189
  'dayjs/plugin/weekOfYear',
180
190
  'dayjs/plugin/isSameOrBefore',
181
191
  'dayjs/plugin/isSameOrAfter',
192
+ 'vxe-table',
193
+ 'xe-utils',
182
194
  ], // 默认情况下,不在 node_modules 中的,链接的包不会被预构建
183
195
  exclude: ['cmpt-huitu-ui', 'cmpt-huitu-utils'], // 在预构建中强制排除的依赖项
184
196
  force: false, // 强制进行依赖预构建
197
+ esbuildOptions: {
198
+ // 处理 CommonJS 模块(如 xe-utils)
199
+ format: 'esm',
200
+ // 支持 CommonJS
201
+ supported: {
202
+ 'dynamic-import': true,
203
+ 'import-meta': true,
204
+ },
205
+ },
185
206
  },
186
207
 
187
208
  // 下面要配合 minify: 'esbuild',才能生效
@@ -0,0 +1,106 @@
1
+ # 修复 vxe-table 依赖问题
2
+
3
+ ## 问题描述
4
+
5
+ 执行 `pnpm dev` 后,终端报错:
6
+ ```
7
+ [vite] Internal server error: Failed to resolve import "vxe-table" from "node_modules/.pnpm/cmpt-huitu-ui@1.0.1/node_modules/cmpt-huitu-ui/index.js?v=8ce970af". Does the file exist?
8
+ ```
9
+
10
+ 控制台报错:
11
+ ```
12
+ GET http://localhost:3002/node_modules/.pnpm/cmpt-huitu-ui@1.0.1/node_modules/cmpt-huitu-ui/index.js?v=8ce970af net::ERR_ABORTED 500 (Internal Server Error)
13
+ ```
14
+
15
+ ## 问题原因
16
+
17
+ `cmpt-huitu-ui` 包依赖 `vxe-table`,但项目的 `package.json` 中没有直接声明 `vxe-table` 依赖。在使用 pnpm 时,依赖提升机制可能没有正确提升 `vxe-table` 到项目的 `node_modules` 中,导致 Vite 无法解析该模块。
18
+
19
+ ## 解决方案
20
+
21
+ ### 方案 1:手动添加依赖(推荐)
22
+
23
+ 在项目根目录执行:
24
+
25
+ ```bash
26
+ pnpm add vxe-table@^4.16.20
27
+ ```
28
+
29
+ 或者使用 npm:
30
+
31
+ ```bash
32
+ npm install vxe-table@^4.16.20
33
+ ```
34
+
35
+ ### 方案 2:修改 package.json
36
+
37
+ 在项目的 `package.json` 的 `dependencies` 中添加:
38
+
39
+ ```json
40
+ {
41
+ "dependencies": {
42
+ "vxe-table": "^4.16.20",
43
+ // ... 其他依赖
44
+ }
45
+ }
46
+ ```
47
+
48
+ 然后执行:
49
+
50
+ ```bash
51
+ pnpm install
52
+ ```
53
+
54
+ ### 方案 3:配置 pnpm 依赖提升(可选)
55
+
56
+ 如果希望 pnpm 自动提升 `vxe-table`,可以在项目根目录创建 `.npmrc` 文件:
57
+
58
+ ```
59
+ shamefully-hoist=true
60
+ ```
61
+
62
+ 然后重新安装依赖:
63
+
64
+ ```bash
65
+ rm -rf node_modules pnpm-lock.yaml
66
+ pnpm install
67
+ ```
68
+
69
+ **注意**:`shamefully-hoist=true` 会让 pnpm 的行为更像 npm,可能会增加 `node_modules` 的大小。
70
+
71
+ ## 验证
72
+
73
+ 修复后,检查以下内容:
74
+
75
+ 1. ✅ `package.json` 中包含 `vxe-table` 依赖
76
+ 2. ✅ `node_modules` 中存在 `vxe-table` 目录
77
+ 3. ✅ 终端没有 `vxe-table` 相关错误
78
+ 4. ✅ 页面正常加载
79
+ 5. ✅ `VexTable` 组件可以正常使用
80
+
81
+ ## 预防措施
82
+
83
+ **对于新创建的项目**:
84
+
85
+ 模板已经更新,新创建的项目会自动包含 `vxe-table` 依赖,不会再出现此问题。
86
+
87
+ **对于已创建的项目**:
88
+
89
+ 请按照上述方案手动添加 `vxe-table` 依赖。
90
+
91
+ ## 相关依赖
92
+
93
+ `vxe-table` 的依赖关系:
94
+ - `vxe-table` 依赖 `xe-utils`
95
+ - `cmpt-huitu-ui` 依赖 `vxe-table`
96
+
97
+ 因此,项目中需要同时安装:
98
+ - `vxe-table`(已通过模板添加)
99
+ - `xe-utils`(会自动作为 `vxe-table` 的依赖安装)
100
+
101
+ ## 注意事项
102
+
103
+ - 确保 `vxe-table` 版本与 `cmpt-huitu-ui` 包中声明的版本兼容(当前为 `^4.16.20`)
104
+ - 如果使用 Monorepo,确保依赖正确安装
105
+ - 清理缓存后重新启动开发服务器
106
+
@@ -0,0 +1,158 @@
1
+ # 修复 xe-utils 导入错误
2
+
3
+ ## 问题描述
4
+
5
+ 执行 `pnpm dev` 后,控制台报错:
6
+ ```
7
+ Uncaught SyntaxError: The requested module '/node_modules/.pnpm/xe-utils@3.8.3/node_modules/xe-utils/index.js?v=e03718de' does not provide an export named 'default' (at config.js?v=e03718de:1:8)
8
+ ```
9
+
10
+ ## 问题原因
11
+
12
+ `xe-utils` 是 `vxe-table` 的依赖,是一个 CommonJS 模块。在 Vite 的 ESM 环境下,需要正确预构建和转换。
13
+
14
+ ## 解决方案
15
+
16
+ ### 方案 1:清理缓存并重启(推荐先尝试)
17
+
18
+ ```bash
19
+ # 1. 停止开发服务器(Ctrl+C)
20
+
21
+ # 2. 删除 Vite 缓存
22
+ rm -rf node_modules/.vite
23
+
24
+ # 3. 如果使用 pnpm,清理 pnpm 缓存
25
+ rm -rf node_modules/.pnpm
26
+
27
+ # 4. 重新安装依赖(可选,如果上述步骤无效)
28
+ pnpm install
29
+
30
+ # 5. 重新启动开发服务器
31
+ pnpm dev
32
+ ```
33
+
34
+ ### 方案 2:检查 vite.config.js 配置
35
+
36
+ 确保 `vite.config.js` 中包含以下配置:
37
+
38
+ ```javascript
39
+ export default defineConfig({
40
+ // ... 其他配置
41
+
42
+ // 优化依赖预构建
43
+ optimizeDeps: {
44
+ include: [
45
+ // ... 其他依赖
46
+ 'vxe-table',
47
+ 'xe-utils',
48
+ ],
49
+ exclude: ['cmpt-huitu-ui', 'cmpt-huitu-utils'],
50
+ esbuildOptions: {
51
+ format: 'esm',
52
+ supported: {
53
+ 'dynamic-import': true,
54
+ 'import-meta': true,
55
+ },
56
+ },
57
+ },
58
+
59
+ // 路径解析配置
60
+ resolve: {
61
+ // ... 其他配置
62
+ dedupe: ['vxe-table', 'xe-utils'],
63
+ },
64
+
65
+ // 构建配置
66
+ build: {
67
+ // ... 其他配置
68
+ commonjsOptions: {
69
+ transformMixedEsModules: true,
70
+ include: [/xe-utils/, /vxe-table/],
71
+ },
72
+ },
73
+ })
74
+ ```
75
+
76
+ ### 方案 3:如果上述方案无效,尝试强制重新构建
77
+
78
+ 在 `vite.config.js` 中临时设置:
79
+
80
+ ```javascript
81
+ optimizeDeps: {
82
+ // ... 其他配置
83
+ force: true, // 强制重新构建依赖
84
+ }
85
+ ```
86
+
87
+ 然后重启开发服务器。构建完成后,可以改回 `force: false`。
88
+
89
+ ### 方案 4:检查 vxe-table 版本
90
+
91
+ 如果问题仍然存在,可能是 `vxe-table` 版本与 `xe-utils` 版本不兼容。
92
+
93
+ 检查 `cmpt-huitu-ui` 包中的 `vxe-table` 版本:
94
+
95
+ ```bash
96
+ cd node_modules/cmpt-huitu-ui
97
+ cat package.json | grep vxe-table
98
+ ```
99
+
100
+ 如果版本过旧,可能需要更新 `cmpt-huitu-ui` 包。
101
+
102
+ ### 方案 5:使用 Vite 插件(最后手段)
103
+
104
+ 如果以上方案都无效,可以创建一个 Vite 插件来处理:
105
+
106
+ ```javascript
107
+ // vite/plugins/fixXeUtils.js
108
+ export default function fixXeUtils() {
109
+ return {
110
+ name: 'fix-xe-utils',
111
+ enforce: 'pre',
112
+ resolveId(id) {
113
+ if (id === 'xe-utils') {
114
+ return id
115
+ }
116
+ },
117
+ load(id) {
118
+ if (id === 'xe-utils') {
119
+ // 返回一个包装的 ESM 模块
120
+ return `
121
+ import * as xeUtils from 'xe-utils'
122
+ export default xeUtils
123
+ export * from 'xe-utils'
124
+ `
125
+ }
126
+ },
127
+ }
128
+ }
129
+ ```
130
+
131
+ 然后在 `vite.config.js` 中使用:
132
+
133
+ ```javascript
134
+ import fixXeUtils from './vite/plugins/fixXeUtils'
135
+
136
+ export default defineConfig({
137
+ plugins: [
138
+ // ... 其他插件
139
+ fixXeUtils(),
140
+ ],
141
+ // ... 其他配置
142
+ })
143
+ ```
144
+
145
+ ## 验证
146
+
147
+ 修复后,检查以下内容:
148
+
149
+ 1. ✅ 控制台没有 `xe-utils` 相关错误
150
+ 2. ✅ 页面正常加载
151
+ 3. ✅ `VexTable` 组件可以正常使用
152
+
153
+ ## 注意事项
154
+
155
+ - 如果使用 Monorepo,确保 `cmpt-huitu-ui` 包已正确发布并安装
156
+ - 清理缓存后,首次启动可能会较慢(需要重新预构建依赖)
157
+ - 如果问题持续存在,可能需要检查 `vxe-table` 和 `xe-utils` 的版本兼容性
158
+
Binary file
Binary file
Binary file