@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,86 @@
|
|
|
1
|
+
import type { RenderUnit, View } from 'vitarx';
|
|
2
|
+
interface IfBlockProps {
|
|
3
|
+
/**
|
|
4
|
+
* 子元素列表
|
|
5
|
+
*
|
|
6
|
+
* 所有子元素必须带有 v-if / v-else-if / v-else 指令,且组合顺序必须正确!
|
|
7
|
+
*/
|
|
8
|
+
children: View[];
|
|
9
|
+
}
|
|
10
|
+
interface SwitchProps {
|
|
11
|
+
/**
|
|
12
|
+
* 默认渲染内容
|
|
13
|
+
*
|
|
14
|
+
* 当所有 `Match` 都不匹配时,渲染该内容
|
|
15
|
+
*/
|
|
16
|
+
fallback?: RenderUnit;
|
|
17
|
+
/**
|
|
18
|
+
* 子元素列表
|
|
19
|
+
*
|
|
20
|
+
* 必须是 `Match` 组件
|
|
21
|
+
*/
|
|
22
|
+
children: View | View[];
|
|
23
|
+
}
|
|
24
|
+
interface MatchProps {
|
|
25
|
+
/**
|
|
26
|
+
* 匹配条件
|
|
27
|
+
*/
|
|
28
|
+
when: unknown;
|
|
29
|
+
/**
|
|
30
|
+
* 匹配成功时渲染的内容
|
|
31
|
+
*/
|
|
32
|
+
children: RenderUnit;
|
|
33
|
+
}
|
|
34
|
+
declare global {
|
|
35
|
+
/**
|
|
36
|
+
* IfBlock - 编译宏组件,无需import导入
|
|
37
|
+
*
|
|
38
|
+
* IfBlock 组件不具有运行时效果,它只是为了兼容tsx类型校验,
|
|
39
|
+
* 例如 `<Comp><h1 v-if={cond} /><h2 v-else /><Comp>`,Comp组件的children要求传入单个元素时tsx类型会误报,使用 `IfBlock` 包裹则可以使 v-if 组合链通过 children 类型校验
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```tsx
|
|
43
|
+
* import { View } from 'vitarx';
|
|
44
|
+
* function TestComp(props:{children:View}) {
|
|
45
|
+
* return <div>{props.children}</div>
|
|
46
|
+
* }
|
|
47
|
+
* function App() {
|
|
48
|
+
* return (
|
|
49
|
+
* <TestComp>
|
|
50
|
+
* <IfBlock>
|
|
51
|
+
* <h1 v-if={cond}/>
|
|
52
|
+
* <h2 v-else/>
|
|
53
|
+
* </IfBlock>
|
|
54
|
+
* </TestComp>
|
|
55
|
+
* )
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
const IfBlock: (props: IfBlockProps) => View;
|
|
60
|
+
/**
|
|
61
|
+
* Switch - 编译宏组件,无需import导入
|
|
62
|
+
*
|
|
63
|
+
* Switch 组件用于条件渲染,`Match` 是其唯一合法子元素,
|
|
64
|
+
* 通过 `when` 属性判断是否匹配,匹配则渲染 `Match` 子元素,否则渲染 `fallback`
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```tsx
|
|
68
|
+
* function App() {
|
|
69
|
+
* return (
|
|
70
|
+
* <Switch fallback="Default">
|
|
71
|
+
* <Match when={a}>A</Match>
|
|
72
|
+
* <Match when={b}>B</Match>
|
|
73
|
+
* </Switch>
|
|
74
|
+
* )
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
const Switch: (props: SwitchProps) => View;
|
|
79
|
+
/**
|
|
80
|
+
* Match - 编译宏组件,无需import导入
|
|
81
|
+
*
|
|
82
|
+
* 需 `Switch` 组件搭配使用,不允许单独使用。
|
|
83
|
+
*/
|
|
84
|
+
const Match: (props: MatchProps) => View;
|
|
85
|
+
}
|
|
86
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vitarx 运行时 API 名称
|
|
3
|
+
* 这些 API 需要从 vitarx 包导入
|
|
4
|
+
*/
|
|
5
|
+
export declare const VITARX_APIS: {
|
|
6
|
+
readonly CREATE_VIEW: "createView";
|
|
7
|
+
readonly FRAGMENT: "Fragment";
|
|
8
|
+
readonly BRANCH: "branch";
|
|
9
|
+
readonly DYNAMIC: "dynamic";
|
|
10
|
+
readonly ACCESS: "access";
|
|
11
|
+
readonly WITH_DIRECTIVES: "withDirectives";
|
|
12
|
+
readonly UNREF: "unref";
|
|
13
|
+
readonly IS_REF: "isRef";
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Ref 相关 API 名称
|
|
17
|
+
* 用于识别 ref 变量
|
|
18
|
+
*/
|
|
19
|
+
export declare const REF_APIS: {
|
|
20
|
+
readonly REF: "ref";
|
|
21
|
+
readonly TO_REF: "toRef";
|
|
22
|
+
readonly TO_REFS: "toRefs";
|
|
23
|
+
readonly SHALLOW_REF: "shallowRef";
|
|
24
|
+
readonly COMPUTED: "computed";
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* 支持的响应式模块
|
|
28
|
+
* 从这些模块导入的 ref API 会被识别
|
|
29
|
+
*/
|
|
30
|
+
export declare const RESPONSIVE_MODULES: readonly ["vitarx", "@vitarx/responsive"];
|
|
31
|
+
/**
|
|
32
|
+
* 纯编译组件名称
|
|
33
|
+
* 这些组件在编译时会被完全转换
|
|
34
|
+
*/
|
|
35
|
+
export declare const PURE_COMPILE_COMPONENTS: readonly ["Switch", "Match", "IfBlock"];
|
|
36
|
+
/**
|
|
37
|
+
* 指令前缀
|
|
38
|
+
*/
|
|
39
|
+
export declare const DIRECTIVE_PREFIX = "v-";
|
|
40
|
+
/**
|
|
41
|
+
* 纯函数注释
|
|
42
|
+
*/
|
|
43
|
+
export declare const PURE_COMMENT = "@__PURE__";
|
|
44
|
+
/**
|
|
45
|
+
* vitarx 模块名称
|
|
46
|
+
*/
|
|
47
|
+
export declare const VITARX_MODULE = "vitarx";
|
|
48
|
+
/**
|
|
49
|
+
* 标识符
|
|
50
|
+
*/
|
|
51
|
+
export declare enum HMR {
|
|
52
|
+
manager = "__$VITARX_HMR$__",
|
|
53
|
+
id = "__$VITARX_HMR_COMPONENT_ID$__",
|
|
54
|
+
view = "__$VITARX_HMR_VIEW_NODE$__",
|
|
55
|
+
state = "__$VITARX_HMR_VIEW_STATE$__"
|
|
56
|
+
}
|
|
57
|
+
/** 默认导出组件基础名称 */
|
|
58
|
+
export declare const DEFAULT_EXPORT_BASE_NAME = "_defaultExport";
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 转换上下文模块
|
|
3
|
+
* 定义编译转换过程中的上下文数据结构
|
|
4
|
+
* @module context
|
|
5
|
+
*/
|
|
6
|
+
import type { File } from '@babel/types';
|
|
7
|
+
import type { CompileOptions } from './transform.js';
|
|
8
|
+
import type { CompilerWarning } from './error.js';
|
|
9
|
+
/**
|
|
10
|
+
* UI 相关的 API 名称列表
|
|
11
|
+
* 这些 API 用于创建视图,在 HMR 时需要识别
|
|
12
|
+
*/
|
|
13
|
+
export declare const UI_API_NAMES: readonly ["createView", "branch", "dynamic", "access", "withDirectives"];
|
|
14
|
+
export type UIApiName = (typeof UI_API_NAMES)[number];
|
|
15
|
+
/**
|
|
16
|
+
* 导入信息
|
|
17
|
+
* 追踪需要注入的运行时 API
|
|
18
|
+
*/
|
|
19
|
+
export interface ImportInfo {
|
|
20
|
+
/** createView - 视图创建函数 */
|
|
21
|
+
createView: boolean;
|
|
22
|
+
/** Fragment - 片段组件 */
|
|
23
|
+
Fragment: boolean;
|
|
24
|
+
/** branch - 条件分支函数 */
|
|
25
|
+
branch: boolean;
|
|
26
|
+
/** dynamic - 动态值包装函数 */
|
|
27
|
+
dynamic: boolean;
|
|
28
|
+
/** access - 属性访问函数 */
|
|
29
|
+
access: boolean;
|
|
30
|
+
/** withDirectives - 指令包装函数 */
|
|
31
|
+
withDirectives: boolean;
|
|
32
|
+
/** unref - ref 解包函数 */
|
|
33
|
+
unref: boolean;
|
|
34
|
+
/** isRef - ref 类型检查函数 */
|
|
35
|
+
isRef: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Vitarx 导入别名映射
|
|
39
|
+
* 记录运行时 API 的本地别名
|
|
40
|
+
*/
|
|
41
|
+
export interface VitarxImportAliases {
|
|
42
|
+
createView: string | null;
|
|
43
|
+
Fragment: string | null;
|
|
44
|
+
branch: string | null;
|
|
45
|
+
dynamic: string | null;
|
|
46
|
+
access: string | null;
|
|
47
|
+
withDirectives: string | null;
|
|
48
|
+
unref: string | null;
|
|
49
|
+
isRef: string | null;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Ref API 别名映射
|
|
53
|
+
* 记录响应式 API 的本地别名
|
|
54
|
+
*/
|
|
55
|
+
export interface RefApiAliases {
|
|
56
|
+
ref: string | null;
|
|
57
|
+
toRef: string | null;
|
|
58
|
+
toRefs: string | null;
|
|
59
|
+
shallowRef: string | null;
|
|
60
|
+
computed: string | null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 转换上下文
|
|
64
|
+
* 包含编译过程中需要的所有状态信息
|
|
65
|
+
*/
|
|
66
|
+
export interface TransformContext {
|
|
67
|
+
/** 原始源代码 */
|
|
68
|
+
code: string;
|
|
69
|
+
/** 文件标识符(可能包含查询参数) */
|
|
70
|
+
id: string;
|
|
71
|
+
/** 文件名(不含查询参数) */
|
|
72
|
+
filename: string;
|
|
73
|
+
/** 编译选项 */
|
|
74
|
+
options: CompileOptions;
|
|
75
|
+
/** 解析后的 AST */
|
|
76
|
+
ast: File;
|
|
77
|
+
/** 需要注入的导入信息 */
|
|
78
|
+
imports: ImportInfo;
|
|
79
|
+
/** 已存在的导入名称集合 */
|
|
80
|
+
existingImports: Set<string>;
|
|
81
|
+
/** Vitarx API 别名映射 */
|
|
82
|
+
vitarxAliases: VitarxImportAliases;
|
|
83
|
+
/** Ref API 别名映射 */
|
|
84
|
+
refApiAliases: RefApiAliases;
|
|
85
|
+
/** ref 变量名集合 */
|
|
86
|
+
refVariables: Set<string>;
|
|
87
|
+
/** UI API 别名集合(用于 HMR 代码分离识别) */
|
|
88
|
+
uiApiAliases: Set<string>;
|
|
89
|
+
/** 编译警告列表 */
|
|
90
|
+
warnings: CompilerWarning[];
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* 创建转换上下文
|
|
94
|
+
* @param code - 源代码
|
|
95
|
+
* @param id - 文件标识符
|
|
96
|
+
* @param options - 编译选项
|
|
97
|
+
* @param ast - AST 节点
|
|
98
|
+
* @returns 转换上下文对象
|
|
99
|
+
*/
|
|
100
|
+
export declare function createContext(code: string, id: string, options: CompileOptions, ast: File): TransformContext;
|
|
101
|
+
/**
|
|
102
|
+
* 标记需要注入的导入
|
|
103
|
+
* @param ctx - 转换上下文
|
|
104
|
+
* @param name - 导入名称
|
|
105
|
+
*/
|
|
106
|
+
export declare function markImport(ctx: TransformContext, name: keyof ImportInfo): void;
|
|
107
|
+
/**
|
|
108
|
+
* 记录 UI API 别名
|
|
109
|
+
* @param ctx - 转换上下文
|
|
110
|
+
* @param alias - API 别名
|
|
111
|
+
*/
|
|
112
|
+
export declare function markUiApiAlias(ctx: TransformContext, alias: string): void;
|
|
113
|
+
/**
|
|
114
|
+
* 添加编译警告
|
|
115
|
+
* @param ctx - 转换上下文
|
|
116
|
+
* @param warning - 编译警告
|
|
117
|
+
*/
|
|
118
|
+
export declare function addWarning(ctx: TransformContext, warning: CompilerWarning): void;
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 编译错误处理模块
|
|
3
|
+
* 定义编译器错误类型和错误消息
|
|
4
|
+
* @module error
|
|
5
|
+
*/
|
|
6
|
+
import type { Node } from '@babel/types';
|
|
7
|
+
/**
|
|
8
|
+
* 错误位置信息
|
|
9
|
+
*/
|
|
10
|
+
export interface ErrorLocation {
|
|
11
|
+
/** 行号 */
|
|
12
|
+
line: number;
|
|
13
|
+
/** 列号 */
|
|
14
|
+
column: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 编译错误选项
|
|
18
|
+
*/
|
|
19
|
+
export interface CompilerErrorOptions {
|
|
20
|
+
/** 错误代码 */
|
|
21
|
+
code: string;
|
|
22
|
+
/** 错误消息 */
|
|
23
|
+
message: string;
|
|
24
|
+
/** 错误位置 */
|
|
25
|
+
loc?: ErrorLocation;
|
|
26
|
+
/** 相关 AST 节点 */
|
|
27
|
+
node?: Node;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 编译器错误类
|
|
31
|
+
* 包含错误代码和位置信息
|
|
32
|
+
*/
|
|
33
|
+
export declare class CompilerError extends Error {
|
|
34
|
+
/** 错误代码 */
|
|
35
|
+
code: string;
|
|
36
|
+
/** 错误位置 */
|
|
37
|
+
loc?: ErrorLocation;
|
|
38
|
+
constructor(options: CompilerErrorOptions);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 错误代码枚举
|
|
42
|
+
*/
|
|
43
|
+
export declare const ErrorCodes: {
|
|
44
|
+
/** 不能同时使用展开属性和 v-bind */
|
|
45
|
+
readonly E001: "E001";
|
|
46
|
+
/** 重复的展开属性或属性键 */
|
|
47
|
+
readonly E002: "E002";
|
|
48
|
+
/** v-else 没有前置的 v-if */
|
|
49
|
+
readonly E003: "E003";
|
|
50
|
+
/** v-else-if 没有前置的 v-if */
|
|
51
|
+
readonly E004: "E004";
|
|
52
|
+
/** v-if 链不连续 */
|
|
53
|
+
readonly E005: "E005";
|
|
54
|
+
/** Switch 组件有无效子节点 */
|
|
55
|
+
readonly E006: "E006";
|
|
56
|
+
/** Match 组件缺少 when 属性 */
|
|
57
|
+
readonly E007: "E007";
|
|
58
|
+
/** IfBlock 有无效子元素 */
|
|
59
|
+
readonly E008: "E008";
|
|
60
|
+
/** v-model 与 modelValue 或 onUpdate:modelValue 冲突 */
|
|
61
|
+
readonly E009: "E009";
|
|
62
|
+
/** v-model 值必须是 Identifier 或 MemberExpression */
|
|
63
|
+
readonly E010: "E010";
|
|
64
|
+
/** v-model Identifier 必须是 ref */
|
|
65
|
+
readonly E011: "E011";
|
|
66
|
+
/** Match 组件必须在 Switch 内使用 */
|
|
67
|
+
readonly E012: "E012";
|
|
68
|
+
/** Match 组件必须有子元素 */
|
|
69
|
+
readonly E013: "E013";
|
|
70
|
+
/** IfBlock 组件必须有子元素 */
|
|
71
|
+
readonly E014: "E014";
|
|
72
|
+
/** Switch 组件必须有 Match 子元素 */
|
|
73
|
+
readonly E015: "E015";
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* 警告代码枚举
|
|
77
|
+
*/
|
|
78
|
+
export declare const WarningCodes: {
|
|
79
|
+
/** children 属性和子元素同时存在 */
|
|
80
|
+
readonly W001: "W001";
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* 错误消息映射
|
|
84
|
+
*/
|
|
85
|
+
export declare const ErrorMessages: Record<string, string>;
|
|
86
|
+
/**
|
|
87
|
+
* 警告消息映射
|
|
88
|
+
*/
|
|
89
|
+
export declare const WarningMessages: Record<string, string>;
|
|
90
|
+
/**
|
|
91
|
+
* 创建编译错误
|
|
92
|
+
* @param code - 错误代码
|
|
93
|
+
* @param node - 相关 AST 节点
|
|
94
|
+
* @param additionalMessage - 附加消息
|
|
95
|
+
* @returns 编译错误实例
|
|
96
|
+
*/
|
|
97
|
+
export declare function createError(code: keyof typeof ErrorCodes, node?: Node, additionalMessage?: string): CompilerError;
|
|
98
|
+
/**
|
|
99
|
+
* 编译警告类
|
|
100
|
+
*/
|
|
101
|
+
export declare class CompilerWarning {
|
|
102
|
+
/** 警告代码 */
|
|
103
|
+
code: string;
|
|
104
|
+
/** 警告消息 */
|
|
105
|
+
message: string;
|
|
106
|
+
/** 警告位置 */
|
|
107
|
+
loc?: ErrorLocation;
|
|
108
|
+
constructor(code: string, message: string, loc?: ErrorLocation);
|
|
109
|
+
toString(): string;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 创建编译警告
|
|
113
|
+
* @param code - 警告代码
|
|
114
|
+
* @param node - 相关 AST 节点
|
|
115
|
+
* @returns 编译警告实例
|
|
116
|
+
*/
|
|
117
|
+
export declare function createWarning(code: keyof typeof WarningCodes, node?: Node): CompilerWarning;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Component, ComponentView } from 'vitarx';
|
|
2
|
+
import type { ModuleNamespace } from 'vite/types/hot.js';
|
|
3
|
+
import { HMR } from '../constants/index.js';
|
|
4
|
+
declare global {
|
|
5
|
+
interface Window {
|
|
6
|
+
[HMR.manager]: HMRManager;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
declare module 'vitarx' {
|
|
10
|
+
interface ComponentView {
|
|
11
|
+
[HMR.state]?: Record<string, any>;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export default class HMRManager {
|
|
15
|
+
#private;
|
|
16
|
+
/**
|
|
17
|
+
* 获取单实例
|
|
18
|
+
*/
|
|
19
|
+
static get instance(): HMRManager;
|
|
20
|
+
/**
|
|
21
|
+
* 给组件绑定唯一id
|
|
22
|
+
*
|
|
23
|
+
* @param component
|
|
24
|
+
* @param id
|
|
25
|
+
*/
|
|
26
|
+
bindId(component: Function, id: string): void;
|
|
27
|
+
/**
|
|
28
|
+
* 置换新模块
|
|
29
|
+
*
|
|
30
|
+
* 此方法提供给`jsxDev`函数调用,保持每次创建组件实例都是最新的模块!
|
|
31
|
+
* @param component
|
|
32
|
+
*/
|
|
33
|
+
resolveComponent(component: Component): Component;
|
|
34
|
+
/**
|
|
35
|
+
* 恢复状态
|
|
36
|
+
*
|
|
37
|
+
* @param {ComponentView} view - 组件视图
|
|
38
|
+
* @param {string} name - 变量名称
|
|
39
|
+
* @example
|
|
40
|
+
* const state = __$VITARX_HMR$__.instance.memo(__$VITARX_HMR_VIEW_NODE$__, 'state') ?? ref(0)
|
|
41
|
+
*/
|
|
42
|
+
memo(view: ComponentView, name: string): any;
|
|
43
|
+
/**
|
|
44
|
+
* 注册节点
|
|
45
|
+
*
|
|
46
|
+
* @param view - 组件视图节点
|
|
47
|
+
*/
|
|
48
|
+
register(view: ComponentView): void;
|
|
49
|
+
/**
|
|
50
|
+
* 获取模块id
|
|
51
|
+
*
|
|
52
|
+
* @param component
|
|
53
|
+
*/
|
|
54
|
+
getId(component: Component): string;
|
|
55
|
+
update(mod: ModuleNamespace): void;
|
|
56
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 代码变更类型
|
|
3
|
+
*/
|
|
4
|
+
export interface ChangeCode {
|
|
5
|
+
/** UI 描述代码是否变化(需要重新构建视图) */
|
|
6
|
+
build: boolean;
|
|
7
|
+
/** 非 UI 代码是否变化(需要完全重新挂载) */
|
|
8
|
+
logic: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 判断两个函数组件的代码差异
|
|
12
|
+
*
|
|
13
|
+
* 分析策略:
|
|
14
|
+
* 1. 解析函数代码,提取所有 createView/branch/dynamic 等 UI API 调用
|
|
15
|
+
* 2. 将这些调用识别为 UI 描述代码
|
|
16
|
+
* 3. 其余代码识别为非 UI 代码(逻辑代码)
|
|
17
|
+
* 4. 分别比较 UI 代码和非 UI 代码是否变化
|
|
18
|
+
*
|
|
19
|
+
* @param newCode 新函数代码
|
|
20
|
+
* @param oldCode 旧函数代码
|
|
21
|
+
* @returns {ChangeCode} 变更检测结果
|
|
22
|
+
*/
|
|
23
|
+
export declare function diffComponentChange(newCode: string, oldCode: string): ChangeCode;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Plugin } from 'vite';
|
|
2
|
+
export type * from './components.js';
|
|
3
|
+
/**
|
|
4
|
+
* vite-plugin-vitarx 配置选项
|
|
5
|
+
*
|
|
6
|
+
* 暂无配置选项
|
|
7
|
+
*/
|
|
8
|
+
export interface VitePluginVitarxOptions {
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* vite-plugin-vitarx
|
|
12
|
+
*
|
|
13
|
+
* 功能:
|
|
14
|
+
* - jsx -> createView 编译转换
|
|
15
|
+
* - 支持 v-if、v-else-if、v-else 、v-model 等编译宏指令
|
|
16
|
+
* - 支持 Switch , IfBlock 等编译宏组件
|
|
17
|
+
* - 开发时 HMR 热更新相关代码注入与功能支持
|
|
18
|
+
*
|
|
19
|
+
* @param _options - 暂无可选配置。
|
|
20
|
+
* @returns - vite插件对象。
|
|
21
|
+
*/
|
|
22
|
+
export default function vitarx(_options?: VitePluginVitarxOptions): Plugin;
|
package/dist/index.js
CHANGED
|
@@ -43,13 +43,12 @@ export default function vitarx(_options) {
|
|
|
43
43
|
},
|
|
44
44
|
configResolved(config) {
|
|
45
45
|
viteConfig = config;
|
|
46
|
-
const sourcemap = config.build.sourcemap;
|
|
47
46
|
compileOptions = {
|
|
48
47
|
dev: isDEV,
|
|
49
48
|
ssr: isSSR,
|
|
50
49
|
hmr: isDEV && !isSSR,
|
|
51
50
|
runtimeModule: 'vitarx',
|
|
52
|
-
sourceMap:
|
|
51
|
+
sourceMap: false
|
|
53
52
|
};
|
|
54
53
|
},
|
|
55
54
|
async transform(code, id) {
|
|
@@ -57,9 +56,9 @@ export default function vitarx(_options) {
|
|
|
57
56
|
if (result) {
|
|
58
57
|
return await transformWithOxc(result.code, id, {
|
|
59
58
|
jsx: 'preserve'
|
|
60
|
-
},
|
|
59
|
+
}, this.getCombinedSourcemap(), viteConfig);
|
|
61
60
|
}
|
|
62
|
-
return
|
|
61
|
+
return null;
|
|
63
62
|
}
|
|
64
63
|
};
|
|
65
64
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IfBlock 组件处理器
|
|
3
|
+
* 将 <IfBlock><div v-if>...</div><div v-else>...</div></IfBlock> 编译为 branch 调用
|
|
4
|
+
* @module passes/components/ifBlock
|
|
5
|
+
*/
|
|
6
|
+
import type { NodePath } from '@babel/traverse';
|
|
7
|
+
import * as t from '@babel/types';
|
|
8
|
+
import { TransformContext } from '../../context.js';
|
|
9
|
+
/**
|
|
10
|
+
* 处理 IfBlock 组件
|
|
11
|
+
*/
|
|
12
|
+
export declare function processIfBlock(path: NodePath<t.JSXElement>, ctx: TransformContext): void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 编译宏组件处理模块
|
|
3
|
+
* 包含 Switch、IfBlock 等纯编译组件的转换
|
|
4
|
+
* @module passes/components
|
|
5
|
+
*/
|
|
6
|
+
import type { NodePath } from '@babel/traverse';
|
|
7
|
+
import * as t from '@babel/types';
|
|
8
|
+
import { TransformContext } from '../../context.js';
|
|
9
|
+
/**
|
|
10
|
+
* 处理纯编译组件
|
|
11
|
+
* 根据组件名称分发到对应的处理器
|
|
12
|
+
* @param path - JSX 元素路径
|
|
13
|
+
* @param ctx - 转换上下文
|
|
14
|
+
*/
|
|
15
|
+
export declare function processPureCompileComponent(path: NodePath<t.JSXElement>, ctx: TransformContext): void;
|
|
16
|
+
export { processSwitch } from './switch.js';
|
|
17
|
+
export { processIfBlock } from './ifBlock.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Switch 组件处理器
|
|
3
|
+
* 将 <Switch><Match when={cond}>...</Match></Switch> 编译为 branch 调用
|
|
4
|
+
* @module passes/components/switch
|
|
5
|
+
*/
|
|
6
|
+
import type { NodePath } from '@babel/traverse';
|
|
7
|
+
import * as t from '@babel/types';
|
|
8
|
+
import { TransformContext } from '../../context.js';
|
|
9
|
+
/**
|
|
10
|
+
* 处理 Switch 组件
|
|
11
|
+
* @param path - JSX 元素路径
|
|
12
|
+
* @param ctx - 转换上下文
|
|
13
|
+
*/
|
|
14
|
+
export declare function processSwitch(path: NodePath<t.JSXElement>, ctx: TransformContext): void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 指令处理模块
|
|
3
|
+
* 处理 v-show 等指令
|
|
4
|
+
* @module passes/directives/processDirectives
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
import { TransformContext } from '../../context.js';
|
|
8
|
+
import type { DirectiveInfo } from '../../passes/props/types.js';
|
|
9
|
+
/**
|
|
10
|
+
* 处理指令
|
|
11
|
+
* 将指令转换为 withDirectives 调用
|
|
12
|
+
* @param viewCall - 视图调用表达式
|
|
13
|
+
* @param directives - 指令映射
|
|
14
|
+
* @param ctx - 转换上下文
|
|
15
|
+
* @returns 处理后的调用表达式
|
|
16
|
+
*/
|
|
17
|
+
export declare function processDirectives(viewCall: t.CallExpression, directives: Map<string, DirectiveInfo>, ctx: TransformContext): t.CallExpression;
|
|
@@ -20,9 +20,9 @@ export function processDirectives(viewCall, directives, ctx) {
|
|
|
20
20
|
return viewCall;
|
|
21
21
|
}
|
|
22
22
|
const directiveArray = [];
|
|
23
|
-
for (const [name,
|
|
23
|
+
for (const [name, info] of directives) {
|
|
24
24
|
const directiveName = name.slice(2); // 移除 'v-' 前缀
|
|
25
|
-
const directiveValue = buildDirectiveValue(value, ctx);
|
|
25
|
+
const directiveValue = buildDirectiveValue(info.value, info.arg, ctx);
|
|
26
26
|
directiveArray.push([directiveName, directiveValue]);
|
|
27
27
|
}
|
|
28
28
|
markImport(ctx, 'withDirectives');
|
|
@@ -32,15 +32,20 @@ export function processDirectives(viewCall, directives, ctx) {
|
|
|
32
32
|
/**
|
|
33
33
|
* 构建指令值对象
|
|
34
34
|
*/
|
|
35
|
-
function buildDirectiveValue(value, ctx) {
|
|
35
|
+
function buildDirectiveValue(value, arg, ctx) {
|
|
36
|
+
const properties = [];
|
|
37
|
+
// 添加 value 属性
|
|
36
38
|
if (isIdentifier(value)) {
|
|
37
39
|
markImport(ctx, 'unref');
|
|
38
40
|
const unrefAlias = getAlias(ctx.vitarxAliases, 'unref');
|
|
39
|
-
|
|
40
|
-
t.objectMethod('get', t.identifier('value'), [], t.blockStatement([t.returnStatement(t.callExpression(t.identifier(unrefAlias), [value]))]))
|
|
41
|
-
]);
|
|
41
|
+
properties.push(t.objectMethod('get', t.identifier('value'), [], t.blockStatement([t.returnStatement(t.callExpression(t.identifier(unrefAlias), [value]))])));
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
t.objectMethod('get', t.identifier('value'), [], t.blockStatement([t.returnStatement(value)]))
|
|
45
|
-
|
|
43
|
+
else {
|
|
44
|
+
properties.push(t.objectMethod('get', t.identifier('value'), [], t.blockStatement([t.returnStatement(value)])));
|
|
45
|
+
}
|
|
46
|
+
// 添加 arg 属性
|
|
47
|
+
if (arg) {
|
|
48
|
+
properties.push(t.objectProperty(t.identifier('arg'), t.stringLiteral(arg)));
|
|
49
|
+
}
|
|
50
|
+
return t.objectExpression(properties);
|
|
46
51
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v-if 链处理器
|
|
3
|
+
* 处理 Fragment 中的 v-if/v-else-if/v-else 链
|
|
4
|
+
* @module passes/directives/vIf
|
|
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
|
+
*/
|
|
12
|
+
type TransformJSXElementFn = (node: t.JSXElement, ctx: TransformContext, handleVIf: boolean) => t.Expression | null;
|
|
13
|
+
/**
|
|
14
|
+
* 处理 Fragment 中的 v-if 链
|
|
15
|
+
* @param path - JSX Fragment 路径
|
|
16
|
+
* @param ctx - 转换上下文
|
|
17
|
+
* @param transformJSXElement - JSX 元素转换函数
|
|
18
|
+
*/
|
|
19
|
+
export declare function processVIfChain(path: NodePath<t.JSXFragment>, ctx: TransformContext, transformJSXElement: TransformJSXElementFn): void;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HMR 代码注入模块
|
|
3
|
+
* 在 HMR 模式下为组件函数注入热更新支持代码
|
|
4
|
+
* @module passes/hmr
|
|
5
|
+
*/
|
|
6
|
+
import * as t from '@babel/types';
|
|
7
|
+
import { type ComponentInfo } from '../../utils/index.js';
|
|
8
|
+
/**
|
|
9
|
+
* 注入 HMR 支持
|
|
10
|
+
* 主入口函数,为所有组件注入完整的 HMR 支持
|
|
11
|
+
* @param program - AST Program 节点
|
|
12
|
+
* @param components - 组件信息列表
|
|
13
|
+
* @param filename - 文件名
|
|
14
|
+
*/
|
|
15
|
+
export declare function injectHMRSupport(program: t.Program, components: ComponentInfo[], filename: string): void;
|