@vitarx/plugin-vite 1.0.9 → 1.0.10

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.
@@ -1,7 +1,8 @@
1
1
  import * as t from '@babel/types';
2
2
  import { isJSXElement, isJSXText } from '@babel/types';
3
3
  import { createError } from '../../error.js';
4
- import { createArrowFunction, createBranch, getJSXAttributeByName, getJSXElementName } from '../../utils/index.js';
4
+ import { createArrowFunction, createBranch, filterEffectiveChildren, getJSXAttributeByName, getJSXElementName } from '../../utils/index.js';
5
+ import { processChildren } from '../jsx/processChildren.js';
5
6
  import { getAttributeValue } from '../props/attribute.js';
6
7
  /**
7
8
  * 处理 Switch 组件
@@ -15,7 +16,7 @@ export function processSwitch(path, ctx) {
15
16
  // 收集 Match 子节点
16
17
  const matchNodes = collectMatchNodes(children);
17
18
  // 构建条件和分支
18
- const { conditions, branches } = buildConditionsAndBranches(matchNodes);
19
+ const { conditions, branches } = buildConditionsAndBranches(matchNodes, ctx);
19
20
  // 添加 fallback 分支
20
21
  if (fallbackValue) {
21
22
  conditions.push(t.booleanLiteral(true)); // 添加一个总是为 true 的条件作为 fallback
@@ -85,9 +86,10 @@ function collectMatchNodes(children) {
85
86
  * 该函数接收一个 JSX 元素数组,提取每个元素的 when 属性作为条件,
86
87
  * 并将子元素内容转换为对应的分支函数
87
88
  * @param matchNodes - JSX 元素数组,每个元素代表一个条件分支
89
+ * @param ctx - 转换上下文
88
90
  * @returns 返回一个对象,包含 conditions 条件数组和 branches 分支数组
89
91
  */
90
- function buildConditionsAndBranches(matchNodes) {
92
+ function buildConditionsAndBranches(matchNodes, ctx) {
91
93
  const conditions = []; // 存储提取的条件表达式
92
94
  const branches = []; // 存储转换后的分支函数
93
95
  // 遍历每个 JSX 元素,提取条件和分支
@@ -103,28 +105,19 @@ function buildConditionsAndBranches(matchNodes) {
103
105
  ? whenAttr.value.expression // 如果是表达式容器,取出表达式
104
106
  : whenAttr.value; // 否则直接使用值
105
107
  conditions.push(whenExpr); // 将条件添加到条件数组
106
- // 提取分支内容,过滤掉空白文本节点
107
- const matchChildren = matchNode.children.filter(c => !isJSXText(c) || c.value.trim());
108
- // 检查是否有子元素
109
- if (matchChildren.length === 0) {
110
- throw createError('E013', matchNode); // 如果没有子元素,抛出错误
111
- }
112
- // 根据子元素数量创建不同的分支函数
113
- if (matchChildren.length === 1) {
114
- const child = matchChildren[0];
115
- if (isJSXText(child)) {
116
- // 如果是单个文本节点,创建返回字符串的函数
117
- branches.push(createArrowFunction(t.stringLiteral(child.value.trim())));
118
- }
119
- else {
120
- // 如果是单个表达式节点,直接使用该表达式
121
- branches.push(createArrowFunction(child));
122
- }
123
- }
124
- else {
125
- // 如果有多个子元素,创建返回数组表达式的函数
126
- branches.push(createArrowFunction(t.arrayExpression(matchChildren)));
108
+ const children = filterEffectiveChildren(matchNode);
109
+ if (children.length === 0)
110
+ throw createError('E013', matchNode);
111
+ // 单个纯文本分支保留原有的首尾去空白行为。
112
+ const first = children[0];
113
+ if (children.length === 1 && isJSXText(first)) {
114
+ branches.push(createArrowFunction(t.stringLiteral(first.value.trim())));
115
+ continue;
127
116
  }
117
+ // 复用 JSX 子节点转换,解开表达式容器并保留分支内容的响应式更新。
118
+ const expressions = processChildren(children, ctx);
119
+ const body = expressions.length === 1 ? expressions[0] : t.arrayExpression(expressions);
120
+ branches.push(createArrowFunction(body));
128
121
  }
129
122
  return { conditions, branches }; // 返回条件和分支数组
130
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitarx/plugin-vite",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "The official plugin for Vitarx support in Vite.",
5
5
  "author": "ZhuChongLin <8210856@qq.com>",
6
6
  "license": "MIT",
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "peerDependencies": {
38
38
  "vitarx": "^4.0.0",
39
- "vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
39
+ "vite": "^8.3.3"
40
40
  },
41
41
  "dependencies": {
42
42
  "@babel/generator": "^7.29.1",
@@ -54,15 +54,15 @@
54
54
  "@types/babel__generator": "^7.27.0",
55
55
  "@types/babel__traverse": "^7.28.0",
56
56
  "@types/node": "^25.3.2",
57
- "happy-dom": "^20.14.5",
57
+ "happy-dom": "21.0.4",
58
58
  "husky": "^9.1.7",
59
59
  "node": "^25.7.0",
60
60
  "prettier": "^3.8.1",
61
61
  "rimraf": "^6.1.3",
62
62
  "typescript": "^6.0.3",
63
63
  "vitarx": "^4.0.6",
64
- "vite": "^8.0.5",
65
- "vitest": "^4.0.18"
64
+ "vite": "8.3.3",
65
+ "vitest": "5.0.5"
66
66
  },
67
67
  "scripts": {
68
68
  "build": "vitest run && rimraf dist && tsc -p tsconfig.build.json",