@vitarx/plugin-vite 1.0.0-beta.2 → 1.0.0-beta.3
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 +2 -0
- package/dist/context.js +1 -0
- package/dist/passes/imports/collectRefVariables.d.ts +7 -0
- package/dist/passes/imports/collectRefVariables.js +47 -0
- package/dist/passes/imports/index.d.ts +1 -1
- package/dist/passes/imports/index.js +1 -1
- package/dist/passes/index.d.ts +1 -1
- package/dist/passes/index.js +1 -1
- package/dist/passes/props/attribute.d.ts +2 -2
- package/dist/passes/props/attribute.js +90 -36
- package/dist/transform.js +2 -1
- package/package.json +1 -1
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
|
@@ -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';
|
package/dist/passes/index.d.ts
CHANGED
|
@@ -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';
|
package/dist/passes/index.js
CHANGED
|
@@ -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 {
|
|
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,101 @@ 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 (
|
|
104
|
+
// 如果是静态值,直接创建对象属性
|
|
105
|
+
if (isStaticValue(value)) {
|
|
107
106
|
return t.objectProperty(keyNode, value);
|
|
108
107
|
}
|
|
109
|
-
//
|
|
110
|
-
if (isIdentifier(value)) {
|
|
111
|
-
//
|
|
112
|
-
if (ctx.
|
|
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
|
-
//
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
//
|
|
125
|
-
return
|
|
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
|
-
//
|
|
128
|
-
if (isMemberExpression(value)) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
+
// 对象表达式是静态值
|
|
154
|
+
case 'ObjectExpression':
|
|
155
|
+
// 数组表达式是静态值
|
|
156
|
+
case 'ArrayExpression':
|
|
157
|
+
return true;
|
|
158
|
+
// 其他类型的表达式都不是静态值
|
|
159
|
+
default:
|
|
160
|
+
return false;
|
|
135
161
|
}
|
|
136
|
-
|
|
137
|
-
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* 创建一个getter方法
|
|
165
|
+
* @param keyNode - 属性键的字符串字面量节点
|
|
166
|
+
* @param statement - getter方法体中的语句
|
|
167
|
+
* @returns 返回一个ObjectMethod类型的AST节点,表示一个getter方法
|
|
168
|
+
*/
|
|
169
|
+
function createGetter(keyNode, statement) {
|
|
170
|
+
// 使用objectMethod方法创建一个getter方法
|
|
171
|
+
// 参数分别为: 方法类型('get')、属性键、参数数组(空数组)、方法体(包含传入的语句)
|
|
172
|
+
return t.objectMethod('get', keyNode, [], t.blockStatement([statement]));
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 创建一个getter方法,该方法会对返回值进行unref处理
|
|
176
|
+
* @param keyNode - 对象的键节点,是一个字符串字面量
|
|
177
|
+
* @param value - 需要进行unref处理的值表达式
|
|
178
|
+
* @param ctx - 转换上下文对象,包含转换所需的各种信息和工具函数
|
|
179
|
+
* @returns 返回一个ObjectMethod类型的AST节点,表示一个getter方法
|
|
180
|
+
*/
|
|
181
|
+
function createUnrefGetter(keyNode, // 对象的键,使用字符串字面量表示
|
|
182
|
+
value, // 需要进行unref处理的值表达式
|
|
183
|
+
ctx // 转换上下文,包含转换所需的各种信息和工具函数
|
|
184
|
+
) {
|
|
185
|
+
// 返回一个对象方法类型的AST节点
|
|
186
|
+
markImport(ctx, 'unref'); // 标记需要导入unref函数
|
|
187
|
+
const unrefAlias = getAlias(ctx.vitarxAliases, 'unref'); // 获取unref函数的别名
|
|
188
|
+
return createGetter(
|
|
189
|
+
// 创建并返回一个getter方法
|
|
190
|
+
keyNode, t.returnStatement(t.callExpression(t.identifier(unrefAlias), [value])) // 返回对value进行unref处理后的结果
|
|
191
|
+
);
|
|
138
192
|
}
|
|
139
193
|
/**
|
|
140
194
|
* 处理单个 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
|