@vitarx/plugin-vite 0.0.1-alpha.0 → 0.0.1-alpha.2
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/components.d.ts +86 -0
- package/dist/constants/index.d.ts +58 -0
- package/dist/context.d.ts +118 -0
- package/dist/error.d.ts +117 -0
- package/dist/hmr-client/index.d.ts +56 -0
- package/dist/hmr-client/update.d.ts +7 -0
- package/dist/hmr-client/utils.d.ts +23 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +3 -4
- package/dist/passes/components/ifBlock.d.ts +12 -0
- package/dist/passes/components/index.d.ts +17 -0
- package/dist/passes/components/switch.d.ts +14 -0
- package/dist/passes/directives/index.d.ts +6 -0
- package/dist/passes/directives/processDirectives.d.ts +17 -0
- package/dist/passes/directives/processDirectives.js +14 -9
- package/dist/passes/directives/vIf.d.ts +20 -0
- package/dist/passes/hmr/index.d.ts +5 -0
- package/dist/passes/hmr/inject.d.ts +15 -0
- package/dist/passes/imports/collectImports.d.ts +22 -0
- package/dist/passes/imports/collectRefVariables.d.ts +22 -0
- package/dist/passes/imports/index.d.ts +7 -0
- package/dist/passes/imports/injectImports.d.ts +14 -0
- package/dist/passes/index.d.ts +10 -0
- package/dist/passes/jsx/index.d.ts +7 -0
- package/dist/passes/jsx/processChildren.d.ts +14 -0
- package/dist/passes/jsx/processJSXElement.d.ts +22 -0
- package/dist/passes/jsx/processJSXFragment.d.ts +14 -0
- package/dist/passes/props/attribute.d.ts +30 -0
- package/dist/passes/props/attribute.js +21 -25
- package/dist/passes/props/index.d.ts +40 -0
- package/dist/passes/props/index.js +1 -1
- package/dist/passes/props/types.d.ts +52 -0
- package/dist/passes/props/vmodel.d.ts +27 -0
- package/dist/transform.d.ts +15 -0
- package/dist/utils/ast-builders.d.ts +75 -0
- package/dist/utils/ast-guards.d.ts +12 -0
- package/dist/utils/branch-factory.d.ts +47 -0
- package/dist/utils/component-collect.d.ts +17 -0
- package/dist/utils/generate.d.ts +5 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/jsx-helpers.d.ts +67 -0
- package/dist/utils/pattern-helpers.d.ts +20 -0
- package/dist/utils/vif-helpers.d.ts +31 -0
- package/package.json +1 -1
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 导入收集模块
|
|
3
|
+
* 负责收集现有导入信息和本地变量绑定
|
|
4
|
+
* @module passes/imports/collectImports
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
/**
|
|
8
|
+
* 收集现有导入信息
|
|
9
|
+
* @param program - AST Program 节点
|
|
10
|
+
* @returns 本地变量名集合和 vitarx 导入映射
|
|
11
|
+
*/
|
|
12
|
+
export declare function collectExistingImports(program: t.Program): {
|
|
13
|
+
localNames: Set<string>;
|
|
14
|
+
vitarxImports: Map<string, string>;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* 收集本地变量绑定
|
|
18
|
+
* 包括导入绑定、变量声明、函数声明、类声明
|
|
19
|
+
* @param program - AST Program 节点
|
|
20
|
+
* @returns 本地变量名集合
|
|
21
|
+
*/
|
|
22
|
+
export declare function collectLocalBindings(program: t.Program): Set<string>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ref 变量收集模块
|
|
3
|
+
* 负责识别 ref API 别名和收集 ref 变量
|
|
4
|
+
* @module passes/imports/collectRefVariables
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
import type { RefApiAliases } from '../../context.js';
|
|
8
|
+
/**
|
|
9
|
+
* 收集 ref API 的别名
|
|
10
|
+
* 识别从 vitarx 或 @vitarx/responsive 导入的 ref/toRef/toRefs/shallowRef/computed
|
|
11
|
+
* @param program - AST Program 节点
|
|
12
|
+
* @returns ref API 别名映射
|
|
13
|
+
*/
|
|
14
|
+
export declare function collectRefApiAliases(program: t.Program): RefApiAliases;
|
|
15
|
+
/**
|
|
16
|
+
* 收集通过 ref API 定义的变量
|
|
17
|
+
* 包括直接赋值和 toRefs 解构
|
|
18
|
+
* @param program - AST Program 节点
|
|
19
|
+
* @param refApiAliases - ref API 别名映射
|
|
20
|
+
* @returns ref 变量名集合
|
|
21
|
+
*/
|
|
22
|
+
export declare function collectRefVariables(program: t.Program, refApiAliases: RefApiAliases): Set<string>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 导入处理模块
|
|
3
|
+
* @module passes/imports
|
|
4
|
+
*/
|
|
5
|
+
export { collectExistingImports, collectLocalBindings } from './collectImports.js';
|
|
6
|
+
export { collectRefApiAliases, collectRefVariables } from './collectRefVariables.js';
|
|
7
|
+
export { injectImports } from './injectImports.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 导入注入模块
|
|
3
|
+
* 负责动态注入 vitarx 导入语句
|
|
4
|
+
* @module passes/imports/injectImports
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
import type { TransformContext } from '../../context.js';
|
|
8
|
+
/**
|
|
9
|
+
* 注入 vitarx 导入
|
|
10
|
+
* 根据已使用的 API 动态生成导入语句
|
|
11
|
+
* @param program - AST Program 节点
|
|
12
|
+
* @param ctx - 转换上下文
|
|
13
|
+
*/
|
|
14
|
+
export declare function injectImports(program: t.Program, ctx: TransformContext): void;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 编译转换处理模块
|
|
3
|
+
* @module passes
|
|
4
|
+
*/
|
|
5
|
+
export { collectExistingImports, collectLocalBindings, collectRefApiAliases, collectRefVariables, injectImports } from './imports/index.js';
|
|
6
|
+
export { processPureCompileComponent, processSwitch, processIfBlock } from './components/index.js';
|
|
7
|
+
export { processVIfChain, processDirectives } from './directives/index.js';
|
|
8
|
+
export { processChildren, processJSXElement, transformJSXElement, processJSXFragment } from './jsx/index.js';
|
|
9
|
+
export { processProps, type PropsResult } from './props/index.js';
|
|
10
|
+
export { injectHMRSupport } from './hmr/index.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 子元素处理模块
|
|
3
|
+
* 处理 JSX 元素的子节点
|
|
4
|
+
* @module passes/jsx/processChildren
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
import { TransformContext } from '../../context.js';
|
|
8
|
+
/**
|
|
9
|
+
* 处理子节点数组
|
|
10
|
+
* @param children - 子节点数组
|
|
11
|
+
* @param ctx - 转换上下文
|
|
12
|
+
* @returns 处理后的表达式数组
|
|
13
|
+
*/
|
|
14
|
+
export declare function processChildren(children: t.Node[], ctx: TransformContext): t.Expression[];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSX 元素处理模块
|
|
3
|
+
* 将 JSX 元素转换为 createView 调用
|
|
4
|
+
* @module passes/jsx/processJSXElement
|
|
5
|
+
*/
|
|
6
|
+
import type { NodePath } from '@babel/traverse';
|
|
7
|
+
import * as t from '@babel/types';
|
|
8
|
+
import { TransformContext } from '../../context.js';
|
|
9
|
+
/**
|
|
10
|
+
* 处理 JSX 元素
|
|
11
|
+
* @param path - JSX 元素路径
|
|
12
|
+
* @param ctx - 转换上下文
|
|
13
|
+
*/
|
|
14
|
+
export declare function processJSXElement(path: NodePath<t.JSXElement>, ctx: TransformContext): void;
|
|
15
|
+
/**
|
|
16
|
+
* 转换 JSX 元素为表达式
|
|
17
|
+
* @param node - JSX 元素节点
|
|
18
|
+
* @param ctx - 转换上下文
|
|
19
|
+
* @param handleVIf - 是否处理 v-if 指令
|
|
20
|
+
* @returns 转换后的表达式
|
|
21
|
+
*/
|
|
22
|
+
export declare function transformJSXElement(node: t.JSXElement, ctx: TransformContext, handleVIf?: boolean): t.Expression | null;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSX Fragment 处理模块
|
|
3
|
+
* 将 JSX Fragment 转换为 createView(Fragment, ...) 调用
|
|
4
|
+
* @module passes/jsx/processJSXFragment
|
|
5
|
+
*/
|
|
6
|
+
import type { NodePath } from '@babel/traverse';
|
|
7
|
+
import * as t from '@babel/types';
|
|
8
|
+
import { TransformContext } from '../../context.js';
|
|
9
|
+
/**
|
|
10
|
+
* 处理 JSX Fragment
|
|
11
|
+
* @param path - JSX Fragment 路径
|
|
12
|
+
* @param ctx - 转换上下文
|
|
13
|
+
*/
|
|
14
|
+
export declare function processJSXFragment(path: NodePath<t.JSXFragment>, ctx: TransformContext): void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 属性处理模块
|
|
3
|
+
* 负责处理 JSX 元素的各类属性,包括普通属性、展开属性、命名空间属性等
|
|
4
|
+
* @module passes/props/attribute
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
import { TransformContext } from '../../context.js';
|
|
8
|
+
import type { AttributeResult } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* 处理展开属性 {...props}
|
|
11
|
+
* 将展开属性转换为 v-bind 特殊属性格式
|
|
12
|
+
* @param attr - JSX 展开属性节点
|
|
13
|
+
* @param hasVBind - 是否已存在 v-bind
|
|
14
|
+
* @param node - JSX 元素节点(用于错误定位)
|
|
15
|
+
* @returns 转换后的属性对象,或 null(如果已存在 v-bind)
|
|
16
|
+
* @throws {Error} E001 - 当已存在 v-bind 时抛出
|
|
17
|
+
*/
|
|
18
|
+
export declare function processSpreadAttribute(attr: t.JSXSpreadAttribute, hasVBind: boolean, node: t.JSXElement): {
|
|
19
|
+
property: t.ObjectProperty;
|
|
20
|
+
hasVBind: boolean;
|
|
21
|
+
} | null;
|
|
22
|
+
/**
|
|
23
|
+
* 处理单个 JSX 属性
|
|
24
|
+
* 根据属性名类型分发到不同的处理逻辑
|
|
25
|
+
* @param attr - JSX 属性节点
|
|
26
|
+
* @param existingPropNames - 已存在的属性名集合
|
|
27
|
+
* @param ctx - 转换上下文
|
|
28
|
+
* @returns 属性处理结果
|
|
29
|
+
*/
|
|
30
|
+
export declare function processAttribute(attr: t.JSXAttribute, existingPropNames: Set<string>, ctx: TransformContext): AttributeResult;
|
|
@@ -71,7 +71,7 @@ export function processAttribute(attr, existingPropNames, ctx) {
|
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
73
|
* 处理命名空间属性
|
|
74
|
-
*
|
|
74
|
+
*
|
|
75
75
|
* @param attr - JSX 属性节点
|
|
76
76
|
* @param attrName - 命名空间名称
|
|
77
77
|
* @param existingPropNames - 已存在的属性名集合
|
|
@@ -82,22 +82,24 @@ function processNamespacedAttribute(attr, attrName, existingPropNames, ctx) {
|
|
|
82
82
|
const namespace = attrName.namespace.name;
|
|
83
83
|
const name = attrName.name.name;
|
|
84
84
|
const fullName = `${namespace}:${name}`;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
// v
|
|
90
|
-
if (
|
|
91
|
-
return {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
85
|
+
const value = getAttributeValue(attr.value);
|
|
86
|
+
// v-xxx:arg 格式的带参数指令 (如 v-test:t)
|
|
87
|
+
if (namespace.startsWith('v-')) {
|
|
88
|
+
const directiveName = namespace;
|
|
89
|
+
// 排除 v-if 系列和 v-model
|
|
90
|
+
if (!['v-if', 'v-else-if', 'v-else', 'v-model', 'v-bind'].includes(directiveName)) {
|
|
91
|
+
return {
|
|
92
|
+
type: 'directive',
|
|
93
|
+
name: directiveName,
|
|
94
|
+
value,
|
|
95
|
+
isVBind: false,
|
|
96
|
+
isVModel: false,
|
|
97
|
+
arg: name
|
|
98
|
+
};
|
|
96
99
|
}
|
|
97
100
|
}
|
|
98
101
|
// 其他命名空间属性作为普通属性处理
|
|
99
102
|
existingPropNames.add(fullName);
|
|
100
|
-
const value = getAttributeValue(attr.value);
|
|
101
103
|
const property = createProperty(fullName, value, ctx);
|
|
102
104
|
return { type: 'property', property };
|
|
103
105
|
}
|
|
@@ -140,26 +142,20 @@ function getAttributeValue(value) {
|
|
|
140
142
|
* @returns 对象属性或方法节点
|
|
141
143
|
*/
|
|
142
144
|
function createProperty(key, value, ctx) {
|
|
143
|
-
|
|
145
|
+
const keyNode = t.stringLiteral(key);
|
|
144
146
|
if (isStringLiteral(value) || isNumericLiteral(value) || isBooleanLiteral(value)) {
|
|
145
|
-
return t.objectProperty(
|
|
147
|
+
return t.objectProperty(keyNode, value);
|
|
146
148
|
}
|
|
147
|
-
// Identifier: 检查是否为已知的 ref 变量
|
|
148
149
|
if (isIdentifier(value)) {
|
|
149
|
-
// 已知 ref 变量:直接访问 .value
|
|
150
150
|
if (ctx.refVariables.has(value.name)) {
|
|
151
|
-
return t.objectMethod('get',
|
|
151
|
+
return t.objectMethod('get', keyNode, [], t.blockStatement([t.returnStatement(t.memberExpression(value, t.identifier('value')))]));
|
|
152
152
|
}
|
|
153
|
-
// children 属性:直接引用,不使用 unref
|
|
154
|
-
// 原因:children 中的 ref 是合法的可变渲染源
|
|
155
153
|
if (key === 'children') {
|
|
156
|
-
return t.objectProperty(
|
|
154
|
+
return t.objectProperty(keyNode, value);
|
|
157
155
|
}
|
|
158
|
-
// 其他未知变量:使用 unref 进行解包
|
|
159
156
|
markImport(ctx, 'unref');
|
|
160
157
|
const unrefAlias = getAlias(ctx.vitarxAliases, 'unref');
|
|
161
|
-
return t.objectMethod('get',
|
|
158
|
+
return t.objectMethod('get', keyNode, [], t.blockStatement([t.returnStatement(t.callExpression(t.identifier(unrefAlias), [value]))]));
|
|
162
159
|
}
|
|
163
|
-
|
|
164
|
-
return t.objectMethod('get', t.identifier(key), [], t.blockStatement([t.returnStatement(value)]));
|
|
160
|
+
return t.objectMethod('get', keyNode, [], t.blockStatement([t.returnStatement(value)]));
|
|
165
161
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Props 处理模块入口
|
|
3
|
+
* 负责处理 JSX 元素的属性,包括 v-model 特殊指令
|
|
4
|
+
* @module passes/props
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
import { TransformContext } from '../../context.js';
|
|
8
|
+
import type { PropsResult } from './types.js';
|
|
9
|
+
export type { PropsResult, VModelState, AttributeResult } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* 处理 JSX 元素的属性
|
|
12
|
+
* 将 JSX 属性转换为运行时 props 对象,并提取指令
|
|
13
|
+
*
|
|
14
|
+
* 处理流程:
|
|
15
|
+
* 1. 遍历所有属性,分类处理
|
|
16
|
+
* 2. 展开属性转换为 v-bind 格式
|
|
17
|
+
* 3. 普通属性转换为对象属性或 getter
|
|
18
|
+
* 4. 指令属性提取到 directives Map
|
|
19
|
+
* 5. v-model 特殊处理,生成 modelValue 和 onUpdate:modelValue
|
|
20
|
+
*
|
|
21
|
+
* @param node - JSX 元素节点
|
|
22
|
+
* @param ctx - 转换上下文
|
|
23
|
+
* @param hasChildren - 是否有子元素(有子元素时跳过 children 属性)
|
|
24
|
+
* @returns 处理结果,包含 props 对象、指令映射和 v-bind 标记
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* // 输入
|
|
29
|
+
* <Input v-model={value} placeholder="text" disabled />
|
|
30
|
+
*
|
|
31
|
+
* // 输出 props
|
|
32
|
+
* {
|
|
33
|
+
* modelValue: get() { return value.value },
|
|
34
|
+
* 'onUpdate:modelValue': v => value.value = v,
|
|
35
|
+
* placeholder: "text",
|
|
36
|
+
* disabled: true
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function processProps(node: t.JSXElement, ctx: TransformContext, hasChildren?: boolean): PropsResult;
|
|
@@ -75,7 +75,7 @@ export function processProps(node, ctx, hasChildren = false) {
|
|
|
75
75
|
}
|
|
76
76
|
// 其他指令:添加到指令映射
|
|
77
77
|
else {
|
|
78
|
-
directives.set(result.name, result.value);
|
|
78
|
+
directives.set(result.name, { value: result.value, arg: result.arg });
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
else if (result.type === 'property') {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Props 处理模块 - 类型定义
|
|
3
|
+
* @module passes/props/types
|
|
4
|
+
*/
|
|
5
|
+
import * as t from '@babel/types';
|
|
6
|
+
/**
|
|
7
|
+
* 指令信息
|
|
8
|
+
*/
|
|
9
|
+
export interface DirectiveInfo {
|
|
10
|
+
/** 指令值表达式 */
|
|
11
|
+
value: t.Expression;
|
|
12
|
+
/** 指令参数 (如 v-test:t 中的 t) */
|
|
13
|
+
arg?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Props 处理结果
|
|
17
|
+
*/
|
|
18
|
+
export interface PropsResult {
|
|
19
|
+
/** 处理后的 props 对象表达式,无属性时为 null */
|
|
20
|
+
props: t.ObjectExpression | null;
|
|
21
|
+
/** 指令映射表,key 为指令名,value 为指令信息 */
|
|
22
|
+
directives: Map<string, DirectiveInfo>;
|
|
23
|
+
/** 是否包含 v-bind 展开属性 */
|
|
24
|
+
hasVBind: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* v-model 指令状态
|
|
28
|
+
*/
|
|
29
|
+
export interface VModelState {
|
|
30
|
+
/** 是否存在 v-model 指令 */
|
|
31
|
+
hasVModel: boolean;
|
|
32
|
+
/** v-model 绑定的值表达式 */
|
|
33
|
+
value: t.Expression | null;
|
|
34
|
+
/** v-model 属性节点,用于错误定位 */
|
|
35
|
+
node: t.JSXAttribute | null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 属性处理结果联合类型
|
|
39
|
+
* - directive: 指令类型属性
|
|
40
|
+
* - property: 普通属性
|
|
41
|
+
*/
|
|
42
|
+
export type AttributeResult = {
|
|
43
|
+
type: 'directive';
|
|
44
|
+
name: string;
|
|
45
|
+
value: t.Expression;
|
|
46
|
+
isVBind: boolean;
|
|
47
|
+
isVModel: boolean;
|
|
48
|
+
arg?: string;
|
|
49
|
+
} | {
|
|
50
|
+
type: 'property';
|
|
51
|
+
property: t.ObjectProperty | t.ObjectMethod;
|
|
52
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v-model 指令处理模块
|
|
3
|
+
* 负责处理 JSX 元素的 v-model 双向绑定指令
|
|
4
|
+
* @module passes/props/vmodel
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
import { TransformContext } from '../../context.js';
|
|
8
|
+
import type { VModelState } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* 从属性列表中提取 v-model 状态
|
|
11
|
+
* @param attributes - JSX 元素属性列表
|
|
12
|
+
* @returns v-model 状态对象
|
|
13
|
+
*/
|
|
14
|
+
export declare function extractVModelState(attributes: (t.JSXAttribute | t.JSXSpreadAttribute)[]): VModelState;
|
|
15
|
+
/**
|
|
16
|
+
* 创建 v-model 相关的 props
|
|
17
|
+
* 生成 modelValue getter 和 onUpdate:modelValue 回调
|
|
18
|
+
* @param value - v-model 绑定的值表达式
|
|
19
|
+
* @param attrNode - v-model 属性节点(用于错误定位)
|
|
20
|
+
* @param existingPropNames - 已存在的属性名集合(用于冲突检测)
|
|
21
|
+
* @param node - JSX 元素节点
|
|
22
|
+
* @param ctx - 转换上下文
|
|
23
|
+
* @returns 包含 modelValue 和 onUpdate:modelValue 的属性数组
|
|
24
|
+
* @throws {Error} E009 - 当 modelValue 或 onUpdate:modelValue 已存在时
|
|
25
|
+
* @throws {Error} E010 - 当 v-model 绑定值不是 Identifier 或 MemberExpression 时
|
|
26
|
+
*/
|
|
27
|
+
export declare function createVModelProps(value: t.Expression, attrNode: t.JSXAttribute | null, existingPropNames: Set<string>, node: t.JSXElement, ctx: TransformContext): (t.ObjectMethod | t.ObjectProperty)[];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface TransformResult {
|
|
2
|
+
code: string;
|
|
3
|
+
map: any;
|
|
4
|
+
}
|
|
5
|
+
export interface CompileOptions {
|
|
6
|
+
hmr: boolean;
|
|
7
|
+
dev: boolean;
|
|
8
|
+
ssr: boolean;
|
|
9
|
+
runtimeModule: string;
|
|
10
|
+
sourceMap: boolean | 'inline' | 'both';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 转换 JSX/TSX 代码
|
|
14
|
+
*/
|
|
15
|
+
export declare function transform(code: string, id: string, options: CompileOptions): Promise<TransformResult | null>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { type ArrowFunctionExpression, type CallExpression, type Expression, type ObjectExpression, type SourceLocation } from '@babel/types';
|
|
2
|
+
import type { VitarxImportAliases } from '../context.js';
|
|
3
|
+
/**
|
|
4
|
+
* 创建 unref 调用
|
|
5
|
+
* @param argument - 参数表达式
|
|
6
|
+
* @param alias - unref 别名
|
|
7
|
+
* @returns CallExpression
|
|
8
|
+
*/
|
|
9
|
+
export declare function createUnrefCall(argument: Expression, alias?: string): CallExpression;
|
|
10
|
+
/**
|
|
11
|
+
* 创建 access 调用
|
|
12
|
+
* @param object - 对象表达式
|
|
13
|
+
* @param key - 键表达式
|
|
14
|
+
* @param alias - access 别名
|
|
15
|
+
* @returns CallExpression
|
|
16
|
+
*/
|
|
17
|
+
export declare function createAccessCall(object: Expression, key: Expression, alias?: string): CallExpression;
|
|
18
|
+
/**
|
|
19
|
+
* 创建 dynamic 调用
|
|
20
|
+
* @param argument - 参数表达式
|
|
21
|
+
* @param alias - dynamic 别名
|
|
22
|
+
* @returns CallExpression
|
|
23
|
+
*/
|
|
24
|
+
export declare function createDynamicCall(argument: Expression, alias?: string): CallExpression;
|
|
25
|
+
/**
|
|
26
|
+
* 创建 branch 调用
|
|
27
|
+
* @param condition - 条件函数
|
|
28
|
+
* @param branches - 分支函数数组
|
|
29
|
+
* @param alias - branch 别名
|
|
30
|
+
* @returns CallExpression
|
|
31
|
+
*/
|
|
32
|
+
export declare function createBranchCall(condition: ArrowFunctionExpression, branches: ArrowFunctionExpression[], alias?: string): CallExpression;
|
|
33
|
+
/**
|
|
34
|
+
* 创建 createView 调用
|
|
35
|
+
* @param type - 元素类型
|
|
36
|
+
* @param props - props 对象
|
|
37
|
+
* @param locInfo - 位置信息对象
|
|
38
|
+
* @param alias - createView 别名
|
|
39
|
+
* @returns CallExpression
|
|
40
|
+
*/
|
|
41
|
+
export declare function createCreateViewCall(type: Expression, props: ObjectExpression | null, locInfo?: ObjectExpression | null, alias?: string): CallExpression;
|
|
42
|
+
/**
|
|
43
|
+
* 创建 withDirectives 调用
|
|
44
|
+
* @param view - 视图节点
|
|
45
|
+
* @param directives - 指令数组,每项为 [指令名, 指令值]
|
|
46
|
+
* @param alias - withDirectives 别名
|
|
47
|
+
* @returns CallExpression
|
|
48
|
+
*/
|
|
49
|
+
export declare function createWithDirectivesCall(view: Expression, directives: Array<[string, Expression]>, alias?: string): CallExpression;
|
|
50
|
+
/**
|
|
51
|
+
* 创建箭头函数
|
|
52
|
+
* @param body - 函数体表达式
|
|
53
|
+
* @returns ArrowFunctionExpression
|
|
54
|
+
*/
|
|
55
|
+
export declare function createArrowFunction(body: Expression): ArrowFunctionExpression;
|
|
56
|
+
/**
|
|
57
|
+
* 创建位置信息对象
|
|
58
|
+
* @param filename - 文件名
|
|
59
|
+
* @param loc - 源码位置
|
|
60
|
+
* @returns ObjectExpression
|
|
61
|
+
*/
|
|
62
|
+
export declare function createLocationObject(filename: string, loc: SourceLocation): ObjectExpression;
|
|
63
|
+
/**
|
|
64
|
+
* 为调用表达式添加 @__PURE__ 注释
|
|
65
|
+
* @param node - 调用表达式节点
|
|
66
|
+
* @returns 添加注释后的节点
|
|
67
|
+
*/
|
|
68
|
+
export declare function addPureComment<T extends CallExpression>(node: T): T;
|
|
69
|
+
/**
|
|
70
|
+
* 获取 API 别名
|
|
71
|
+
* @param aliases - 别名映射
|
|
72
|
+
* @param name - API 名称
|
|
73
|
+
* @returns 别名或原名
|
|
74
|
+
*/
|
|
75
|
+
export declare function getAlias(aliases: VitarxImportAliases, name: keyof VitarxImportAliases): string;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST 类型守卫函数
|
|
3
|
+
* 用于判断节点类型
|
|
4
|
+
* @module utils/ast-guards
|
|
5
|
+
*/
|
|
6
|
+
import { type Node } from '@babel/types';
|
|
7
|
+
/**
|
|
8
|
+
* 判断 JSXText 是否为纯空白文本
|
|
9
|
+
* @param node - AST 节点
|
|
10
|
+
* @returns 是否为纯空白文本
|
|
11
|
+
*/
|
|
12
|
+
export declare function isWhitespaceJSXText(node: Node): boolean;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Branch 调用工厂模块
|
|
3
|
+
* 统一处理条件分支的生成
|
|
4
|
+
* @module utils/branch-factory
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
import type { TransformContext } from '../context.js';
|
|
8
|
+
/**
|
|
9
|
+
* 条件分支配置
|
|
10
|
+
*/
|
|
11
|
+
export interface BranchConfig {
|
|
12
|
+
/** 条件表达式数组 */
|
|
13
|
+
conditions: t.Expression[];
|
|
14
|
+
/** 分支函数数组 */
|
|
15
|
+
branches: t.ArrowFunctionExpression[];
|
|
16
|
+
/** 是否使用 unref 包装标识符条件 */
|
|
17
|
+
useRef?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 创建 branch 调用
|
|
21
|
+
* @param config - 分支配置
|
|
22
|
+
* @param ctx - 转换上下文
|
|
23
|
+
* @returns branch 调用表达式
|
|
24
|
+
*/
|
|
25
|
+
export declare function createBranch(config: BranchConfig, ctx: TransformContext): t.CallExpression;
|
|
26
|
+
/**
|
|
27
|
+
* 构建嵌套条件表达式
|
|
28
|
+
* 从后向前构建三元表达式链
|
|
29
|
+
*/
|
|
30
|
+
export declare function buildNestedCondition(conditions: t.Expression[], ctx: TransformContext, useRef?: boolean): t.Expression;
|
|
31
|
+
/**
|
|
32
|
+
* 从条件数组和分支节点创建完整的 branch 调用
|
|
33
|
+
* @param conditions - 条件表达式数组
|
|
34
|
+
* @param branchNodes - 分支节点数组(会被包装为箭头函数)
|
|
35
|
+
* @param ctx - 转换上下文
|
|
36
|
+
* @returns branch 调用表达式
|
|
37
|
+
*/
|
|
38
|
+
export declare function createBranchFromNodes(conditions: t.Expression[], branchNodes: t.Expression[], ctx: TransformContext): t.CallExpression;
|
|
39
|
+
/**
|
|
40
|
+
* 创建简单的二元条件 branch(用于三元表达式)
|
|
41
|
+
* @param condition - 条件表达式
|
|
42
|
+
* @param consequent - 真值分支
|
|
43
|
+
* @param alternate - 假值分支
|
|
44
|
+
* @param ctx - 转换上下文
|
|
45
|
+
* @returns branch 调用表达式
|
|
46
|
+
*/
|
|
47
|
+
export declare function createBinaryBranch(condition: t.Expression, consequent: t.Expression, alternate: t.Expression, ctx: TransformContext): t.CallExpression;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST 收集模块
|
|
3
|
+
* 负责收集导入、导出、组件等信息的收集
|
|
4
|
+
* @module passes/transform/collect
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
/**
|
|
8
|
+
* 组件信息
|
|
9
|
+
*/
|
|
10
|
+
export interface ComponentInfo {
|
|
11
|
+
name: string;
|
|
12
|
+
node: t.FunctionDeclaration | t.ArrowFunctionExpression | t.FunctionExpression;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 收集模块中的组件函数
|
|
16
|
+
*/
|
|
17
|
+
export declare function collectComponentFunctions(program: t.Program): ComponentInfo[];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { isWhitespaceJSXText } from './ast-guards.js';
|
|
2
|
+
export { getJSXElementName, isPureCompileComponent, isComponent, isNativeElement, getJSXAttributeByName, hasDirective, getDirectiveValue, isVIfChain, isVIf, isVElseIf, isVElse, removeVDirectives, removeAttribute, filterWhitespaceChildren, validateMatchInSwitch } from './jsx-helpers.js';
|
|
3
|
+
export { createUnrefCall, createAccessCall, createDynamicCall, createBranchCall, createCreateViewCall, createWithDirectivesCall, createArrowFunction, createLocationObject, addPureComment, getAlias } from './ast-builders.js';
|
|
4
|
+
export { collectPatternBindings, collectObjectPatternBindings } from './pattern-helpers.js';
|
|
5
|
+
export { createBranch, createBinaryBranch, buildNestedCondition, type BranchConfig } from './branch-factory.js';
|
|
6
|
+
export { validateVIfChain, collectVIfChainInfo, collectFragmentVIfChains, type VIfChainInfo } from './vif-helpers.js';
|
|
7
|
+
export { collectComponentFunctions, type ComponentInfo } from './component-collect.js';
|
|
8
|
+
export { generateUniqueAlias } from './generate.js';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSX 相关工具函数
|
|
3
|
+
* @module utils/jsx-helpers
|
|
4
|
+
*/
|
|
5
|
+
import * as t from '@babel/types';
|
|
6
|
+
import { type Expression, type JSXAttribute, type JSXElement } from '@babel/types';
|
|
7
|
+
/**
|
|
8
|
+
* 获取 JSX 元素的名称
|
|
9
|
+
*/
|
|
10
|
+
export declare function getJSXElementName(node: JSXElement): string | null;
|
|
11
|
+
/**
|
|
12
|
+
* 判断名称是否为纯编译组件
|
|
13
|
+
*/
|
|
14
|
+
export declare function isPureCompileComponent(name: string): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 判断名称是否为组件(首字母大写)
|
|
17
|
+
*/
|
|
18
|
+
export declare function isComponent(name: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* 判断名称是否为原生元素
|
|
21
|
+
*/
|
|
22
|
+
export declare function isNativeElement(name: string): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* 根据名称获取 JSX 属性
|
|
25
|
+
*/
|
|
26
|
+
export declare function getJSXAttributeByName(node: JSXElement, name: string): JSXAttribute | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* 检查元素是否具有指定指令
|
|
29
|
+
*/
|
|
30
|
+
export declare function hasDirective(node: JSXElement, directiveName: string): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* 获取指令的值
|
|
33
|
+
*/
|
|
34
|
+
export declare function getDirectiveValue(node: JSXElement, directiveName: string): Expression | null;
|
|
35
|
+
/**
|
|
36
|
+
* 检查元素是否为 v-if 链的一部分
|
|
37
|
+
*/
|
|
38
|
+
export declare function isVIfChain(node: JSXElement): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* 检查元素是否有 v-if 指令
|
|
41
|
+
*/
|
|
42
|
+
export declare function isVIf(node: JSXElement): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* 检查元素是否有 v-else-if 指令
|
|
45
|
+
*/
|
|
46
|
+
export declare function isVElseIf(node: JSXElement): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* 检查元素是否有 v-else 指令
|
|
49
|
+
*/
|
|
50
|
+
export declare function isVElse(node: JSXElement): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* 移除元素上所有 v- 开头的指令属性
|
|
53
|
+
*/
|
|
54
|
+
export declare function removeVDirectives(node: JSXElement): void;
|
|
55
|
+
/**
|
|
56
|
+
* 移除元素上指定名称的属性
|
|
57
|
+
*/
|
|
58
|
+
export declare function removeAttribute(node: JSXElement, attrName: string): void;
|
|
59
|
+
/**
|
|
60
|
+
* 过滤掉空白文本子节点
|
|
61
|
+
*/
|
|
62
|
+
export declare function filterWhitespaceChildren(children: t.Node[]): t.Node[];
|
|
63
|
+
/**
|
|
64
|
+
* 校验 Match 组件必须在 Switch 内使用
|
|
65
|
+
* @param children - 子节点数组
|
|
66
|
+
*/
|
|
67
|
+
export declare function validateMatchInSwitch(children: t.Node[]): void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST 模式处理辅助函数
|
|
3
|
+
* 用于处理解构模式、绑定模式等
|
|
4
|
+
* @module utils/pattern-helpers
|
|
5
|
+
*/
|
|
6
|
+
import type { LVal, ObjectPattern } from '@babel/types';
|
|
7
|
+
/**
|
|
8
|
+
* 从绑定模式中收集变量名
|
|
9
|
+
* 支持标识符、对象模式、数组模式
|
|
10
|
+
* @param pattern - 绑定模式
|
|
11
|
+
* @param variables - 变量名集合
|
|
12
|
+
*/
|
|
13
|
+
export declare function collectPatternBindings(pattern: LVal, variables: Set<string>): void;
|
|
14
|
+
/**
|
|
15
|
+
* 从对象解构模式中收集变量名
|
|
16
|
+
* 专门用于 toRefs 解构场景
|
|
17
|
+
* @param pattern - 对象模式
|
|
18
|
+
* @param variables - 变量名集合
|
|
19
|
+
*/
|
|
20
|
+
export declare function collectObjectPatternBindings(pattern: ObjectPattern, variables: Set<string>): void;
|