@vitarx/plugin-vite 1.0.6 → 1.0.8

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.
@@ -9,11 +9,18 @@ import { markImport } from '../../context.js';
9
9
  import { addPureComment, createAccessorCall, createBinaryBranch, createExprCall, getAlias } from '../../utils/index.js';
10
10
  /**
11
11
  * 处理成员表达式
12
- * object 为简单标识符时使用 accessor(细粒度追踪)
13
- * object 为其他表达式(嵌套成员访问等)时降级为 expr(粗粒度追踪)
12
+ * 静态成员(obj.prop,computed=false)时使用 accessor(细粒度追踪),
13
+ * property 标识符即属性名,字符串化后作为 key;
14
+ * 计算成员(obj[keyExpr],computed=true)时降级为 expr(粗粒度追踪)——
15
+ * accessor(obj, key) 的 key 在构建期一次求值,无法追踪 key 本身的变化,
16
+ * 且 key 为标识符时若误字符串化会读成 obj.key 改变语义(读取不存在的属性
17
+ * 渲染为空),必须保留动态求值语义。
14
18
  */
15
19
  function handleMemberExpression(expr, ctx) {
16
- if (isIdentifier(expr.object)) {
20
+ // key 静态(obj.prop 或 obj['prop'])才可 accessor:字符串字面量 key 原样
21
+ // 透传即为正确的静态 key;标识符(非计算成员)即属性名,由 accessor 构建器字符串化
22
+ const isStaticKey = !expr.computed || t.isStringLiteral(expr.property);
23
+ if (isStaticKey && isIdentifier(expr.object)) {
17
24
  markImport(ctx, 'accessor');
18
25
  const accessorAlias = getAlias(ctx.vitarxAliases, 'accessor');
19
26
  return createAccessorCall(expr.object, expr.property, accessorAlias);
@@ -102,6 +109,10 @@ function processChildExpression(expr, ctx) {
102
109
  if (isMemberExpression(expr)) {
103
110
  return handleMemberExpression(expr, ctx);
104
111
  }
112
+ // 可选链初始可能为空,必须延迟读取,才能在信号变化后重新建立依赖。
113
+ if (t.isOptionalMemberExpression(expr) || t.isOptionalCallExpression(expr)) {
114
+ return wrapWithExpr(expr, ctx);
115
+ }
105
116
  // 如果是条件表达式(如 ternary operator),递归处理条件表达式
106
117
  if (isConditionalExpression(expr)) {
107
118
  return processConditionalExpression(expr, ctx);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitarx/plugin-vite",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "The official plugin for Vitarx support in Vite.",
5
5
  "author": "ZhuChongLin <8210856@qq.com>",
6
6
  "license": "MIT",