@vitarx/plugin-vite 1.0.0-beta.2 → 1.0.0-beta.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/dist/context.d.ts CHANGED
@@ -78,6 +78,8 @@ export interface TransformContext {
78
78
  refApiAliases: RefApiAliases;
79
79
  /** ref 变量名集合 */
80
80
  refVariables: Set<string>;
81
+ /** 确定非 ref 的变量名集合(函数声明、箭头函数/函数表达式变量) */
82
+ nonRefVariables: Set<string>;
81
83
  /** builder 函数的本地别名(用于识别纯构建组件) */
82
84
  builderAlias: string | null;
83
85
  /** 已处理的 AST 节点追踪集合 */
package/dist/context.js CHANGED
@@ -43,6 +43,7 @@ export function createContext(code, id, options, ast) {
43
43
  computed: null
44
44
  },
45
45
  refVariables: new Set(),
46
+ nonRefVariables: new Set(),
46
47
  builderAlias: null,
47
48
  processedNodes: new WeakSet(),
48
49
  pureCommentedNodes: new WeakSet(),
@@ -20,3 +20,10 @@ export declare function collectRefApiAliases(program: t.Program): RefApiAliases;
20
20
  * @returns ref 变量名集合
21
21
  */
22
22
  export declare function collectRefVariables(program: t.Program, refApiAliases: RefApiAliases): Set<string>;
23
+ /**
24
+ * 收集确定不是 ref 的变量
25
+ * 包括函数声明、箭头函数和函数表达式变量
26
+ * @param program - AST Program 节点
27
+ * @returns 非 ref 变量名集合
28
+ */
29
+ export declare function collectNonRefVariables(program: t.Program): Set<string>;
@@ -85,6 +85,53 @@ function collectRefVariablesFromNode(node, refVariables, refApiLocalNames, toRef
85
85
  }
86
86
  }
87
87
  }
88
+ /**
89
+ * 收集确定不是 ref 的变量
90
+ * 包括函数声明、箭头函数和函数表达式变量
91
+ * @param program - AST Program 节点
92
+ * @returns 非 ref 变量名集合
93
+ */
94
+ export function collectNonRefVariables(program) {
95
+ const nonRefVariables = new Set();
96
+ for (const node of program.body) {
97
+ collectNonRefFromNode(node, nonRefVariables);
98
+ }
99
+ return nonRefVariables;
100
+ }
101
+ function collectNonRefFromNode(node, nonRefVariables) {
102
+ if (node.type === 'FunctionDeclaration' && node.id) {
103
+ nonRefVariables.add(node.id.name);
104
+ if (node.body) {
105
+ for (const stmt of node.body.body) {
106
+ collectNonRefFromNode(stmt, nonRefVariables);
107
+ }
108
+ }
109
+ }
110
+ else if (node.type === 'VariableDeclaration') {
111
+ for (const decl of node.declarations) {
112
+ if (!decl.init)
113
+ continue;
114
+ if (decl.id.type !== 'Identifier')
115
+ continue;
116
+ const init = decl.init;
117
+ if (init.type === 'ArrowFunctionExpression' ||
118
+ init.type === 'FunctionExpression') {
119
+ nonRefVariables.add(decl.id.name);
120
+ }
121
+ }
122
+ }
123
+ else if (node.type === 'ExportNamedDeclaration') {
124
+ if (node.declaration) {
125
+ collectNonRefFromNode(node.declaration, nonRefVariables);
126
+ }
127
+ }
128
+ else if (node.type === 'ExportDefaultDeclaration') {
129
+ const decl = node.declaration;
130
+ if (decl.type === 'FunctionDeclaration' && decl.id) {
131
+ nonRefVariables.add(decl.id.name);
132
+ }
133
+ }
134
+ }
88
135
  /**
89
136
  * 构建 API 名称集合
90
137
  */
@@ -3,5 +3,5 @@
3
3
  * @module passes/imports
4
4
  */
5
5
  export { collectExistingImports, collectLocalBindings } from './collectImports.js';
6
- export { collectRefApiAliases, collectRefVariables } from './collectRefVariables.js';
6
+ export { collectRefApiAliases, collectNonRefVariables, collectRefVariables } from './collectRefVariables.js';
7
7
  export { injectImports } from './injectImports.js';
@@ -3,5 +3,5 @@
3
3
  * @module passes/imports
4
4
  */
5
5
  export { collectExistingImports, collectLocalBindings } from './collectImports.js';
6
- export { collectRefApiAliases, collectRefVariables } from './collectRefVariables.js';
6
+ export { collectRefApiAliases, collectNonRefVariables, collectRefVariables } from './collectRefVariables.js';
7
7
  export { injectImports } from './injectImports.js';
@@ -2,7 +2,7 @@
2
2
  * 编译转换处理模块
3
3
  * @module passes
4
4
  */
5
- export { collectExistingImports, collectLocalBindings, collectRefApiAliases, collectRefVariables, injectImports } from './imports/index.js';
5
+ export { collectExistingImports, collectLocalBindings, collectNonRefVariables, collectRefApiAliases, collectRefVariables, injectImports } from './imports/index.js';
6
6
  export { processPureCompileComponent, processSwitch, processIfBlock } from './components/index.js';
7
7
  export { processVIfChain, processDirectives } from './directives/index.js';
8
8
  export { processChildren, processJSXElement, transformJSXElement, processJSXFragment } from './jsx/index.js';
@@ -3,7 +3,7 @@
3
3
  * @module passes
4
4
  */
5
5
  // 导入处理
6
- export { collectExistingImports, collectLocalBindings, collectRefApiAliases, collectRefVariables, injectImports } from './imports/index.js';
6
+ export { collectExistingImports, collectLocalBindings, collectNonRefVariables, collectRefApiAliases, collectRefVariables, injectImports } from './imports/index.js';
7
7
  // 编译宏组件
8
8
  export { processPureCompileComponent, processSwitch, processIfBlock } from './components/index.js';
9
9
  // 指令处理
@@ -39,8 +39,8 @@ export declare function getAttributeValue(value: t.JSXAttribute['value']): t.Exp
39
39
  * @param ctx - 转换上下文
40
40
  * @returns 对象属性或方法节点
41
41
  */
42
- export declare function createProperty(key: string, // 属性名称字符串
43
- value: t.Expression, // 属性值的表达式,可以是各种类型的表达式
42
+ export declare function createProperty(key: string, // 属性名称,字符串类型
43
+ value: t.Expression, // 属性值表达式,使用 t.Expression 类型表示
44
44
  ctx: TransformContext): t.ObjectProperty | t.ObjectMethod;
45
45
  /**
46
46
  * 处理单个 JSX 属性
@@ -4,10 +4,9 @@
4
4
  * @module passes/props/attribute
5
5
  */
6
6
  import * as t from '@babel/types';
7
- import { isBooleanLiteral, isIdentifier, isJSXExpressionContainer, isMemberExpression, isNumericLiteral, isStringLiteral } from '@babel/types';
8
7
  import { markImport } from '../../context.js';
9
8
  import { createError } from '../../error.js';
10
- import { createAccessorCall, getAlias } from '../../utils/index.js';
9
+ import { getAlias } from '../../utils/index.js';
11
10
  /**
12
11
  * 处理展开属性 {...props}
13
12
  * 将展开属性转换为 v-bind 特殊属性格式
@@ -71,10 +70,10 @@ export function getAttributeValue(value) {
71
70
  if (!value) {
72
71
  return t.booleanLiteral(true);
73
72
  }
74
- if (isStringLiteral(value)) {
73
+ if (t.isStringLiteral(value)) {
75
74
  return value;
76
75
  }
77
- if (isJSXExpressionContainer(value)) {
76
+ if (t.isJSXExpressionContainer(value)) {
78
77
  if (value.expression.type === 'JSXEmptyExpression') {
79
78
  return t.booleanLiteral(true);
80
79
  }
@@ -95,46 +94,97 @@ export function getAttributeValue(value) {
95
94
  * @param ctx - 转换上下文
96
95
  * @returns 对象属性或方法节点
97
96
  */
98
- export function createProperty(key, // 属性名称字符串
99
- value, // 属性值的表达式,可以是各种类型的表达式
100
- ctx // 转换上下文对象,包含转换所需的各种信息和工具函数
97
+ export function createProperty(key, // 属性名称,字符串类型
98
+ value, // 属性值表达式,使用 t.Expression 类型表示
99
+ ctx // 转换上下文,包含转换过程中的相关信息
101
100
  ) {
102
- // 返回类型,可以是对象属性或对象方法
103
- // 创建属性名的字符串字面量节点
101
+ // 返回类型为对象属性或对象方法
102
+ // 创建属性名的字面量节点
104
103
  const keyNode = t.stringLiteral(key);
105
- // 如果值是字符串、数字或布尔字面量,直接创建对象属性
106
- if (isStringLiteral(value) || isNumericLiteral(value) || isBooleanLiteral(value)) {
104
+ // 如果是静态值,直接创建对象属性
105
+ if (isStaticValue(value)) {
107
106
  return t.objectProperty(keyNode, value);
108
107
  }
109
- // 如果值是标识符
110
- if (isIdentifier(value)) {
111
- // 检查标识符是否是 ref 变量
112
- if (ctx.refVariables.has(value.name)) {
113
- // 如果是 ref 变量,创建一个 getter 方法来访问其 value 属性
114
- return t.objectMethod('get', keyNode, [], t.blockStatement([t.returnStatement(t.memberExpression(value, t.identifier('value')))]));
115
- }
116
- // 特殊处理 children 属性,不使用 unref
117
- if (key === 'children') {
108
+ // 如果是标识符(变量名)
109
+ if (t.isIdentifier(value)) {
110
+ // 特殊处理 children 属性或不在 refVariables 中的变量
111
+ if (key === 'children' || ctx.nonRefVariables.has(value.name)) {
118
112
  return t.objectProperty(keyNode, value);
119
113
  }
120
- // 标记需要导入 unref 函数
121
- markImport(ctx, 'unref');
122
- // 获取 unref 函数的别名
123
- const unrefAlias = getAlias(ctx.vitarxAliases, 'unref');
124
- // 创建 getter 方法,使用 unref 解包属性值
125
- return t.objectMethod('get', keyNode, [], t.blockStatement([t.returnStatement(t.callExpression(t.identifier(unrefAlias), [value]))]));
114
+ // 如果是 ref 变量,创建 getter 方法返回 ref 的 value
115
+ if (ctx.refVariables.has(value.name)) {
116
+ return createGetter(keyNode, t.returnStatement(t.memberExpression(value, t.identifier('value'))));
117
+ }
118
+ // 其他标识符属性创建 unref getter
119
+ return createUnrefGetter(keyNode, value, ctx);
126
120
  }
127
- // 如果值是成员表达式,创建 accessor 调用
128
- if (isMemberExpression(value)) {
129
- // 标记需要导入 accessor 函数
130
- markImport(ctx, 'accessor');
131
- // 获取 accessor 函数的别名
132
- const accessorAlias = getAlias(ctx.vitarxAliases, 'accessor');
133
- // 创建属性节点,使用 accessor 处理成员表达式
134
- return t.objectProperty(keyNode, createAccessorCall(value.object, value.property, accessorAlias));
121
+ // 如果是成员表达式(如 obj.property)
122
+ if (t.isMemberExpression(value)) {
123
+ return createUnrefGetter(keyNode, value, ctx);
124
+ }
125
+ // 如果是调用表达式(如 function())
126
+ if (t.isCallExpression(value)) {
127
+ return createUnrefGetter(keyNode, value, ctx);
128
+ }
129
+ // 默认情况,创建 getter 方法返回值
130
+ return createGetter(keyNode, t.returnStatement(value));
131
+ }
132
+ /**
133
+ * 判断给定的表达式是否为静态值
134
+ * 静态值指的是那些可以在编译时确定值,不会在运行时改变的表达式
135
+ * @param value - 要检查的AST表达式节点
136
+ * @returns 如果是静态值返回true,否则返回false
137
+ */
138
+ function isStaticValue(value) {
139
+ // 使用switch语句检查表达式的类型
140
+ switch (value.type) {
141
+ // 字符串字面量是静态值
142
+ case 'StringLiteral':
143
+ // 数字字面量是静态值
144
+ case 'NumericLiteral':
145
+ // 布尔字面量是静态值
146
+ case 'BooleanLiteral':
147
+ // null字面量是静态值
148
+ case 'NullLiteral':
149
+ // 箭头函数表达式是静态值
150
+ case 'ArrowFunctionExpression':
151
+ // 函数表达式是静态值
152
+ case 'FunctionExpression':
153
+ return true;
154
+ // 其他类型的表达式都不是静态值
155
+ default:
156
+ return false;
135
157
  }
136
- // 默认情况下创建 getter 方法直接返回值
137
- return t.objectMethod('get', keyNode, [], t.blockStatement([t.returnStatement(value)]));
158
+ }
159
+ /**
160
+ * 创建一个getter方法
161
+ * @param keyNode - 属性键的字符串字面量节点
162
+ * @param statement - getter方法体中的语句
163
+ * @returns 返回一个ObjectMethod类型的AST节点,表示一个getter方法
164
+ */
165
+ function createGetter(keyNode, statement) {
166
+ // 使用objectMethod方法创建一个getter方法
167
+ // 参数分别为: 方法类型('get')、属性键、参数数组(空数组)、方法体(包含传入的语句)
168
+ return t.objectMethod('get', keyNode, [], t.blockStatement([statement]));
169
+ }
170
+ /**
171
+ * 创建一个getter方法,该方法会对返回值进行unref处理
172
+ * @param keyNode - 对象的键节点,是一个字符串字面量
173
+ * @param value - 需要进行unref处理的值表达式
174
+ * @param ctx - 转换上下文对象,包含转换所需的各种信息和工具函数
175
+ * @returns 返回一个ObjectMethod类型的AST节点,表示一个getter方法
176
+ */
177
+ function createUnrefGetter(keyNode, // 对象的键,使用字符串字面量表示
178
+ value, // 需要进行unref处理的值表达式
179
+ ctx // 转换上下文,包含转换所需的各种信息和工具函数
180
+ ) {
181
+ // 返回一个对象方法类型的AST节点
182
+ markImport(ctx, 'unref'); // 标记需要导入unref函数
183
+ const unrefAlias = getAlias(ctx.vitarxAliases, 'unref'); // 获取unref函数的别名
184
+ return createGetter(
185
+ // 创建并返回一个getter方法
186
+ keyNode, t.returnStatement(t.callExpression(t.identifier(unrefAlias), [value])) // 返回对value进行unref处理后的结果
187
+ );
138
188
  }
139
189
  /**
140
190
  * 处理单个 JSX 属性
package/dist/transform.js CHANGED
@@ -7,7 +7,7 @@ import generate from '@babel/generator';
7
7
  import { parse } from '@babel/parser';
8
8
  import traverse from '@babel/traverse';
9
9
  import { createContext } from './context.js';
10
- import { collectExistingImports, collectLocalBindings, collectRefApiAliases, collectRefVariables, injectHMRSupport, injectImports, processJSXElement, processJSXFragment, processPureCompileComponent, processVIfChain, transformJSXElement } from './passes/index.js';
10
+ import { collectExistingImports, collectLocalBindings, collectNonRefVariables, collectRefApiAliases, collectRefVariables, injectHMRSupport, injectImports, processJSXElement, processJSXFragment, processPureCompileComponent, processVIfChain, transformJSXElement } from './passes/index.js';
11
11
  import { collectComponentFunctions, generateUniqueAlias, getJSXElementName, isPureCompileComponent } from './utils/index.js';
12
12
  /**
13
13
  * 创建解析器选项
@@ -68,6 +68,7 @@ function collectRefInfo(ctx, program) {
68
68
  const refApiAliases = collectRefApiAliases(program);
69
69
  ctx.refApiAliases = refApiAliases;
70
70
  ctx.refVariables = collectRefVariables(program, refApiAliases);
71
+ ctx.nonRefVariables = collectNonRefVariables(program);
71
72
  }
72
73
  /**
73
74
  * 转换 AST
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitarx/plugin-vite",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.4",
4
4
  "description": "The official plugin for Vitarx support in Vite.",
5
5
  "author": "ZhuChongLin <8210856@qq.com>",
6
6
  "license": "MIT",