@vitarx/plugin-vite 1.0.0-beta.0 → 1.0.0-beta.2

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 (36) hide show
  1. package/dist/constants/index.d.ts +1 -0
  2. package/dist/constants/index.js +1 -0
  3. package/dist/hmr-client/update.js +21 -4
  4. package/dist/hmr-client/utils.js +11 -3
  5. package/dist/index.js +9 -10
  6. package/dist/passes/components/ifBlock.d.ts +3 -0
  7. package/dist/passes/components/ifBlock.js +24 -8
  8. package/dist/passes/components/switch.js +49 -21
  9. package/dist/passes/directives/index.d.ts +1 -1
  10. package/dist/passes/directives/index.js +1 -1
  11. package/dist/passes/directives/processDirectives.d.ts +3 -1
  12. package/dist/passes/directives/processDirectives.js +36 -9
  13. package/dist/passes/directives/vIf.d.ts +18 -6
  14. package/dist/passes/directives/vIf.js +59 -44
  15. package/dist/passes/hmr/inject-state.js +64 -7
  16. package/dist/passes/hmr/inject.js +4 -3
  17. package/dist/passes/imports/collectImports.d.ts +1 -4
  18. package/dist/passes/imports/collectImports.js +4 -5
  19. package/dist/passes/imports/collectRefVariables.js +22 -3
  20. package/dist/passes/jsx/processChildren.js +30 -1
  21. package/dist/passes/jsx/processJSXElement.js +31 -27
  22. package/dist/passes/jsx/processJSXFragment.d.ts +1 -1
  23. package/dist/passes/jsx/processJSXFragment.js +3 -2
  24. package/dist/passes/props/attribute.d.ts +3 -1
  25. package/dist/passes/props/attribute.js +27 -5
  26. package/dist/transform.js +4 -1
  27. package/dist/utils/ast-builders.js +2 -11
  28. package/dist/utils/branch-factory.js +1 -1
  29. package/dist/utils/collect-names.js +29 -0
  30. package/dist/utils/index.d.ts +1 -1
  31. package/dist/utils/index.js +1 -1
  32. package/dist/utils/jsx-helpers.d.ts +10 -0
  33. package/dist/utils/jsx-helpers.js +39 -11
  34. package/dist/utils/vif-helpers.d.ts +1 -3
  35. package/dist/utils/vif-helpers.js +2 -2
  36. package/package.json +1 -1
@@ -4,9 +4,9 @@
4
4
  */
5
5
  import * as t from '@babel/types';
6
6
  import { isJSXElement, isJSXText } from '@babel/types';
7
- import { DIRECTIVE_PREFIX, PURE_COMPILE_COMPONENTS } from '../constants/index.js';
8
- import { isWhitespaceJSXText } from './ast-guards.js';
7
+ import { DIRECTIVE_PREFIX, PURE_COMPILE_COMPONENTS, V_IF_CHAIN_DIRECTIVES } from '../constants/index.js';
9
8
  import { createError } from '../error.js';
9
+ import { isWhitespaceJSXText } from './ast-guards.js';
10
10
  /**
11
11
  * 获取 JSX 元素的名称
12
12
  */
@@ -14,6 +14,32 @@ export function getJSXElementName(node) {
14
14
  const nameNode = node.openingElement.name;
15
15
  return nameNode.type === 'JSXIdentifier' ? nameNode.name : null;
16
16
  }
17
+ /**
18
+ * 将 JSX 元素名称节点转换为 createView 的类型表达式
19
+ * JSXIdentifier → Identifier(组件)或 StringLiteral(原生元素)
20
+ * JSXMemberExpression → MemberExpression(如 Obj.Key → Obj.Key)
21
+ */
22
+ export function resolveJSXElementType(node) {
23
+ const nameNode = node.openingElement.name;
24
+ if (nameNode.type === 'JSXIdentifier') {
25
+ const name = nameNode.name;
26
+ return isNativeElement(name) ? t.stringLiteral(name) : t.identifier(name);
27
+ }
28
+ if (nameNode.type === 'JSXMemberExpression') {
29
+ return jsxMemberExprToMemberExpr(nameNode);
30
+ }
31
+ return null;
32
+ }
33
+ /**
34
+ * 递归转换 JSXMemberExpression 为 MemberExpression
35
+ * Obj.Key → Obj.Key, A.B.C → A.B.C
36
+ */
37
+ function jsxMemberExprToMemberExpr(node) {
38
+ const object = node.object.type === 'JSXMemberExpression'
39
+ ? jsxMemberExprToMemberExpr(node.object)
40
+ : t.identifier(node.object.name);
41
+ return t.memberExpression(object, t.identifier(node.property.name));
42
+ }
17
43
  /**
18
44
  * 判断名称是否为纯编译组件
19
45
  */
@@ -24,7 +50,7 @@ export function isPureCompileComponent(name) {
24
50
  * 判断名称是否为组件(首字母大写)
25
51
  */
26
52
  export function isComponent(name) {
27
- return name[0] === name[0]?.toUpperCase();
53
+ return name.length > 0 && name[0] === name[0].toUpperCase();
28
54
  }
29
55
  /**
30
56
  * 判断名称是否为原生元素
@@ -47,12 +73,16 @@ export function getJSXAttributeByName(node, name) {
47
73
  }
48
74
  /**
49
75
  * 检查元素是否具有指定指令
76
+ * v-if/v-else-if/v-else 仅支持 JSXIdentifier 格式(v-if),不支持命名空间格式(v:if)
77
+ * 其他指令(如 v-show)支持 v-xxx:arg 命名空间格式
50
78
  */
51
79
  export function hasDirective(node, directiveName) {
52
80
  for (const attr of node.openingElement.attributes) {
53
81
  if (attr.type === 'JSXAttribute') {
54
82
  const attrName = attr.name;
55
83
  if (attrName.type === 'JSXNamespacedName') {
84
+ if (V_IF_CHAIN_DIRECTIVES.has(directiveName))
85
+ continue;
56
86
  if (attrName.namespace.name === 'v' && attrName.name.name === directiveName.slice(2)) {
57
87
  return true;
58
88
  }
@@ -66,6 +96,7 @@ export function hasDirective(node, directiveName) {
66
96
  }
67
97
  /**
68
98
  * 获取指令的值
99
+ * v-if/v-else-if/v-else 仅支持 JSXIdentifier 格式(v-if),不支持命名空间格式(v:if)
69
100
  */
70
101
  export function getDirectiveValue(node, directiveName) {
71
102
  for (const attr of node.openingElement.attributes) {
@@ -73,6 +104,8 @@ export function getDirectiveValue(node, directiveName) {
73
104
  const attrName = attr.name;
74
105
  let matches = false;
75
106
  if (attrName.type === 'JSXNamespacedName') {
107
+ if (V_IF_CHAIN_DIRECTIVES.has(directiveName))
108
+ continue;
76
109
  matches = attrName.namespace.name === 'v' && attrName.name.name === directiveName.slice(2);
77
110
  }
78
111
  else if (attrName.type === 'JSXIdentifier') {
@@ -129,19 +162,14 @@ export function removeVDirectives(node) {
129
162
  }
130
163
  /**
131
164
  * 移除元素上的 v-if 链指令(v-if、v-else-if、v-else)
165
+ * 仅处理 JSXIdentifier 格式,不支持命名空间格式(v:if)
132
166
  */
133
167
  export function removeVIfChainDirectives(node) {
134
- const vIfChainDirectives = ['v-if', 'v-else-if', 'v-else'];
135
168
  node.openingElement.attributes = node.openingElement.attributes.filter(attr => {
136
169
  if (attr.type === 'JSXAttribute') {
137
170
  const attrName = attr.name;
138
- if (attrName.type === 'JSXNamespacedName') {
139
- if (attrName.namespace.name === 'v') {
140
- return !vIfChainDirectives.includes(`v-${attrName.name.name}`);
141
- }
142
- }
143
- else if (attrName.type === 'JSXIdentifier') {
144
- return !vIfChainDirectives.includes(attrName.name);
171
+ if (attrName.type === 'JSXIdentifier') {
172
+ return !V_IF_CHAIN_DIRECTIVES.has(attrName.name);
145
173
  }
146
174
  }
147
175
  return true;
@@ -8,11 +8,9 @@ import * as t from '@babel/types';
8
8
  * v-if 链信息
9
9
  */
10
10
  export interface VIfChainInfo {
11
- /** 链中的元素节点 */
12
11
  nodes: t.JSXElement[];
13
- /** 条件表达式数组 */
14
12
  conditions: t.Expression[];
15
- /** 结束索引 */
13
+ startIndex: number;
16
14
  endIndex: number;
17
15
  }
18
16
  /**
@@ -55,7 +55,7 @@ export function collectVIfChainInfo(nodes) {
55
55
  }
56
56
  throw createError('E008', node, 'Element missing v-if/v-else-if/v-else directive');
57
57
  }
58
- return { nodes, conditions, endIndex: nodes.length - 1 };
58
+ return { nodes, conditions, startIndex: 0, endIndex: nodes.length - 1 };
59
59
  }
60
60
  /**
61
61
  * 收集 Fragment 中的 v-if 链
@@ -123,5 +123,5 @@ function collectSingleChainFromFragment(children, startIndex) {
123
123
  break;
124
124
  }
125
125
  }
126
- return { nodes, conditions, endIndex: j - 1 };
126
+ return { nodes, conditions, startIndex, endIndex: j - 1 };
127
127
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitarx/plugin-vite",
3
- "version": "1.0.0-beta.0",
3
+ "version": "1.0.0-beta.2",
4
4
  "description": "The official plugin for Vitarx support in Vite.",
5
5
  "author": "ZhuChongLin <8210856@qq.com>",
6
6
  "license": "MIT",