@vitarx/plugin-vite 1.0.8 → 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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
}
|
|
@@ -108,13 +108,14 @@ ctx // 转换上下文,包含转换过程中的相关信息
|
|
|
108
108
|
if (isStaticValue(value)) {
|
|
109
109
|
return t.objectProperty(keyNode, value);
|
|
110
110
|
}
|
|
111
|
+
// 绑定目标必须保留引用本身,成员访问和调用结果也不能按普通属性解包。
|
|
112
|
+
if (key === 'ref') {
|
|
113
|
+
return t.objectProperty(keyNode, value);
|
|
114
|
+
}
|
|
111
115
|
// 如果是标识符(变量名)
|
|
112
116
|
if (t.isIdentifier(value)) {
|
|
113
|
-
//
|
|
114
|
-
|
|
115
|
-
// - ref: ref 绑定需要 ref 对象本身,运行时通过 isRef() 判定后赋值 .value
|
|
116
|
-
// 若此处 unref/.value 解包,popProperty 取到解包值(如 null),isRef 失败
|
|
117
|
-
if (key === 'children' || key === 'ref') {
|
|
117
|
+
// children 允许 ref 作为可变渲染源,保持引用本身。
|
|
118
|
+
if (key === 'children') {
|
|
118
119
|
return t.objectProperty(keyNode, value);
|
|
119
120
|
}
|
|
120
121
|
// 已知非 ref 变量(函数声明、箭头函数等):静态分析确定非 ref,直接赋值无需 unref
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitarx/plugin-vite",
|
|
3
|
-
"version": "1.0.
|
|
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": "^
|
|
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": "
|
|
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": "
|
|
65
|
-
"vitest": "
|
|
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",
|