cmpt-huitu-cli 1.0.3 → 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.3",
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/)