@vitarx/plugin-vite 1.0.5 → 1.0.7
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/constants/index.d.ts +0 -9
- package/dist/constants/index.js +0 -9
- package/dist/hmr-client/index.d.ts +27 -1
- package/dist/hmr-client/index.js +108 -12
- package/dist/hmr-client/update.d.ts +11 -2
- package/dist/hmr-client/update.js +25 -59
- package/dist/hmr-client/weak-refs.d.ts +37 -0
- package/dist/hmr-client/weak-refs.js +41 -0
- package/dist/passes/hmr/index.d.ts +1 -1
- package/dist/passes/hmr/index.js +1 -1
- package/dist/passes/hmr/inject-state.d.ts +16 -10
- package/dist/passes/hmr/inject-state.js +145 -120
- package/dist/passes/hmr/inject.d.ts +10 -0
- package/dist/passes/hmr/inject.js +47 -10
- package/dist/passes/index.d.ts +1 -1
- package/dist/passes/index.js +1 -1
- package/dist/passes/jsx/processChildren.js +10 -3
- package/dist/transform.js +11 -4
- package/dist/utils/forward-export.d.ts +28 -0
- package/dist/utils/forward-export.js +76 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +2 -0
- package/package.json +5 -4
- package/dist/hmr-client/utils.d.ts +0 -23
- package/dist/hmr-client/utils.js +0 -137
|
@@ -1,23 +0,0 @@
|
|
|
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/expr 等 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/hmr-client/utils.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HMR 代码变更检测工具
|
|
3
|
-
* 用于判断组件代码中 UI 描述部分和非 UI 部分的变更
|
|
4
|
-
* @module hmr-client/utils
|
|
5
|
-
*/
|
|
6
|
-
import { parse } from 'acorn';
|
|
7
|
-
import { simple as walkSimple } from 'acorn-walk';
|
|
8
|
-
import { UI_API_NAMES } from '../constants/index.js';
|
|
9
|
-
/**
|
|
10
|
-
* UI 相关的运行时 API 名称
|
|
11
|
-
* 这些 API 调用代表 UI 描述代码
|
|
12
|
-
*/
|
|
13
|
-
const UI_APIS = new Set([...UI_API_NAMES, 'jsxDEV', 'h']);
|
|
14
|
-
/**
|
|
15
|
-
* 判断标识符名称是否为 UI API
|
|
16
|
-
* 支持原始名称和带后缀的名称(如 jsxDEV$1、branch$2、expr$1)
|
|
17
|
-
*/
|
|
18
|
-
function isUIApi(name) {
|
|
19
|
-
if (UI_APIS.has(name))
|
|
20
|
-
return true;
|
|
21
|
-
for (const api of UI_APIS) {
|
|
22
|
-
if (name.startsWith(api + '$'))
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* 从函数代码中分离 UI 代码和非 UI 代码
|
|
29
|
-
* @param functionCode 完整函数代码
|
|
30
|
-
* @returns 包含逻辑代码和渲染代码的对象
|
|
31
|
-
*/
|
|
32
|
-
function separateLogicAndRender(functionCode) {
|
|
33
|
-
const uiNodes = [];
|
|
34
|
-
// 包装代码以便解析
|
|
35
|
-
const wrappedCode = `(${functionCode})`;
|
|
36
|
-
try {
|
|
37
|
-
const ast = parse(wrappedCode, {
|
|
38
|
-
ecmaVersion: 'latest',
|
|
39
|
-
sourceType: 'module',
|
|
40
|
-
allowReturnOutsideFunction: true,
|
|
41
|
-
allowAwaitOutsideFunction: true,
|
|
42
|
-
allowImportExportEverywhere: true,
|
|
43
|
-
allowHashBang: true,
|
|
44
|
-
allowReserved: true
|
|
45
|
-
});
|
|
46
|
-
// 遍历 AST 收集所有 UI API 调用节点
|
|
47
|
-
walkSimple(ast, {
|
|
48
|
-
CallExpression(node) {
|
|
49
|
-
const callee = node.callee;
|
|
50
|
-
// 直接调用:createView(...)
|
|
51
|
-
if (callee.type === 'Identifier' && isUIApi(callee.name)) {
|
|
52
|
-
uiNodes.push({ start: node.start, end: node.end });
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
// 成员调用:exports.createView(...)
|
|
56
|
-
if (callee.type === 'MemberExpression' &&
|
|
57
|
-
callee.property.type === 'Identifier' &&
|
|
58
|
-
isUIApi(callee.property.name)) {
|
|
59
|
-
uiNodes.push({ start: node.start, end: node.end });
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
catch {
|
|
65
|
-
// 解析失败时,返回原始代码作为逻辑代码
|
|
66
|
-
return {
|
|
67
|
-
logicCode: functionCode.trim(),
|
|
68
|
-
renderCode: ''
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
// 按位置排序,从后向前替换以保持索引正确
|
|
72
|
-
uiNodes.sort((a, b) => a.start - b.start);
|
|
73
|
-
const filteredNodes = [];
|
|
74
|
-
for (const node of uiNodes) {
|
|
75
|
-
const isContained = filteredNodes.some(parent => node.start >= parent.start && node.end <= parent.end);
|
|
76
|
-
if (!isContained) {
|
|
77
|
-
filteredNodes.push(node);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
filteredNodes.sort((a, b) => b.start - a.start);
|
|
81
|
-
// 提取 UI 代码
|
|
82
|
-
const renderCode = filteredNodes
|
|
83
|
-
.map(n => wrappedCode.slice(n.start, n.end))
|
|
84
|
-
.reverse()
|
|
85
|
-
.join('\n');
|
|
86
|
-
// 移除 UI 代码后的逻辑代码
|
|
87
|
-
let logicCode = wrappedCode;
|
|
88
|
-
for (const node of filteredNodes) {
|
|
89
|
-
logicCode = logicCode.slice(0, node.start) + logicCode.slice(node.end);
|
|
90
|
-
}
|
|
91
|
-
return {
|
|
92
|
-
logicCode: logicCode.trim(),
|
|
93
|
-
renderCode
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* 规范化代码字符串
|
|
98
|
-
* 移除多余空白、注释等,用于比较
|
|
99
|
-
*/
|
|
100
|
-
function normalizeCode(code) {
|
|
101
|
-
// 移除多行注释
|
|
102
|
-
let result = code.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
103
|
-
// 移除单行注释
|
|
104
|
-
result = result.replace(/\/\/.*$/gm, '');
|
|
105
|
-
// 移除多余空白
|
|
106
|
-
result = result.replace(/\s+/g, ' ');
|
|
107
|
-
// 移除首尾空白
|
|
108
|
-
return result.trim();
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* 判断两个函数组件的代码差异
|
|
112
|
-
*
|
|
113
|
-
* 分析策略:
|
|
114
|
-
* 1. 解析函数代码,提取所有 createView/branch/expr 等 UI API 调用
|
|
115
|
-
* 2. 将这些调用识别为 UI 描述代码
|
|
116
|
-
* 3. 其余代码识别为非 UI 代码(逻辑代码)
|
|
117
|
-
* 4. 分别比较 UI 代码和非 UI 代码是否变化
|
|
118
|
-
*
|
|
119
|
-
* @param newCode 新函数代码
|
|
120
|
-
* @param oldCode 旧函数代码
|
|
121
|
-
* @returns {ChangeCode} 变更检测结果
|
|
122
|
-
*/
|
|
123
|
-
export function diffComponentChange(newCode, oldCode) {
|
|
124
|
-
const { renderCode: newRenderCode, logicCode: newLogicCode } = separateLogicAndRender(newCode);
|
|
125
|
-
const { renderCode: oldRenderCode, logicCode: oldLogicCode } = separateLogicAndRender(oldCode);
|
|
126
|
-
// 规范化后比较,避免格式差异导致误判
|
|
127
|
-
const normalizedNewLogic = normalizeCode(newLogicCode);
|
|
128
|
-
const normalizedOldLogic = normalizeCode(oldLogicCode);
|
|
129
|
-
const normalizedNewRender = normalizeCode(newRenderCode);
|
|
130
|
-
const normalizedOldRender = normalizeCode(oldRenderCode);
|
|
131
|
-
return {
|
|
132
|
-
// UI 代码变化:需要重新构建视图
|
|
133
|
-
build: normalizedNewRender !== normalizedOldRender,
|
|
134
|
-
// 非 UI 代码变化:需要完全重新挂载组件
|
|
135
|
-
logic: normalizedNewLogic !== normalizedOldLogic
|
|
136
|
-
};
|
|
137
|
-
}
|