@vitarx/plugin-vite 0.0.1-beta.6 → 0.0.1-beta.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/context.d.ts +1 -14
- package/dist/context.js +0 -14
- package/dist/hmr-client/index.js +1 -1
- package/dist/hmr-client/utils.js +1 -1
- package/dist/index.js +2 -3
- package/dist/transform.d.ts +7 -3
- package/dist/transform.js +2 -2
- package/dist/utils/component-collect.js +2 -59
- package/package.json +3 -2
package/dist/context.d.ts
CHANGED
|
@@ -4,15 +4,8 @@
|
|
|
4
4
|
* @module context
|
|
5
5
|
*/
|
|
6
6
|
import type { File } from '@babel/types';
|
|
7
|
-
import type { CompileOptions } from './transform.js';
|
|
8
7
|
import type { CompilerWarning } from './error.js';
|
|
9
|
-
import {
|
|
10
|
-
/**
|
|
11
|
-
* UI 相关的 API 名称列表
|
|
12
|
-
* 这些 API 用于创建视图,在 HMR 时需要识别
|
|
13
|
-
*/
|
|
14
|
-
export declare const UI_API_NAMES_CONST: readonly ["createView", "branch", "dynamic", "access", "withDirectives"];
|
|
15
|
-
export type UIApiName = (typeof UI_API_NAMES)[number];
|
|
8
|
+
import type { CompileOptions } from './transform.js';
|
|
16
9
|
/**
|
|
17
10
|
* 导入信息
|
|
18
11
|
* 追踪需要注入的运行时 API
|
|
@@ -105,12 +98,6 @@ export declare function createContext(code: string, id: string, options: Compile
|
|
|
105
98
|
* @param name - 导入名称
|
|
106
99
|
*/
|
|
107
100
|
export declare function markImport(ctx: TransformContext, name: keyof ImportInfo): void;
|
|
108
|
-
/**
|
|
109
|
-
* 记录 UI API 别名
|
|
110
|
-
* @param ctx - 转换上下文
|
|
111
|
-
* @param alias - API 别名
|
|
112
|
-
*/
|
|
113
|
-
export declare function markUiApiAlias(ctx: TransformContext, alias: string): void;
|
|
114
101
|
/**
|
|
115
102
|
* 添加编译警告
|
|
116
103
|
* @param ctx - 转换上下文
|
package/dist/context.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
import { UI_API_NAMES } from './constants/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* UI 相关的 API 名称列表
|
|
4
|
-
* 这些 API 用于创建视图,在 HMR 时需要识别
|
|
5
|
-
*/
|
|
6
|
-
export const UI_API_NAMES_CONST = UI_API_NAMES;
|
|
7
1
|
/**
|
|
8
2
|
* 创建转换上下文
|
|
9
3
|
* @param code - 源代码
|
|
@@ -61,14 +55,6 @@ export function createContext(code, id, options, ast) {
|
|
|
61
55
|
export function markImport(ctx, name) {
|
|
62
56
|
ctx.imports[name] = true;
|
|
63
57
|
}
|
|
64
|
-
/**
|
|
65
|
-
* 记录 UI API 别名
|
|
66
|
-
* @param ctx - 转换上下文
|
|
67
|
-
* @param alias - API 别名
|
|
68
|
-
*/
|
|
69
|
-
export function markUiApiAlias(ctx, alias) {
|
|
70
|
-
ctx.uiApiAliases.add(alias);
|
|
71
|
-
}
|
|
72
58
|
/**
|
|
73
59
|
* 添加编译警告
|
|
74
60
|
* @param ctx - 转换上下文
|
package/dist/hmr-client/index.js
CHANGED
|
@@ -124,7 +124,7 @@ class HMRManager {
|
|
|
124
124
|
if (id)
|
|
125
125
|
__classPrivateFieldGet(this, _HMRManager_idMapToComponent, "f").set(id, newComponent);
|
|
126
126
|
// 更新节点视图
|
|
127
|
-
if (node.active && node.isMounted) {
|
|
127
|
+
if ((node.isActive || node.active) && node.isMounted) {
|
|
128
128
|
processUpdate(node, newComponent);
|
|
129
129
|
}
|
|
130
130
|
}
|
package/dist/hmr-client/utils.js
CHANGED
|
@@ -10,7 +10,7 @@ import { UI_API_NAMES } from '../constants/index.js';
|
|
|
10
10
|
* UI 相关的运行时 API 名称
|
|
11
11
|
* 这些 API 调用代表 UI 描述代码
|
|
12
12
|
*/
|
|
13
|
-
const UI_APIS = new Set([...UI_API_NAMES, 'jsxDEV']);
|
|
13
|
+
const UI_APIS = new Set([...UI_API_NAMES, 'jsxDEV', 'h']);
|
|
14
14
|
/**
|
|
15
15
|
* 判断标识符名称是否为 UI API
|
|
16
16
|
* 支持原始名称和带后缀的名称(如 jsxDEV$1)
|
package/dist/index.js
CHANGED
|
@@ -32,7 +32,6 @@ export default function vitarx(options) {
|
|
|
32
32
|
let viteConfig;
|
|
33
33
|
return {
|
|
34
34
|
name: 'vite-plugin-vitarx',
|
|
35
|
-
enforce: 'pre',
|
|
36
35
|
config(config, env) {
|
|
37
36
|
isDEV = env.command === 'serve' && !env.isPreview;
|
|
38
37
|
const configSSR = !!config.build?.ssr;
|
|
@@ -59,7 +58,7 @@ export default function vitarx(options) {
|
|
|
59
58
|
ssr: isSSR,
|
|
60
59
|
hmr: isDEV && !isSSR,
|
|
61
60
|
runtimeModule: 'vitarx',
|
|
62
|
-
sourceMap:
|
|
61
|
+
sourceMap: true,
|
|
63
62
|
transformClassNameToClass: options?.transformClassNameToClass ?? false
|
|
64
63
|
};
|
|
65
64
|
},
|
|
@@ -67,7 +66,7 @@ export default function vitarx(options) {
|
|
|
67
66
|
const result = await transform(code, id, compileOptions);
|
|
68
67
|
if (!result)
|
|
69
68
|
return null;
|
|
70
|
-
return
|
|
69
|
+
return viteTransform(result.code, id, undefined, result.map || undefined, viteConfig);
|
|
71
70
|
}
|
|
72
71
|
};
|
|
73
72
|
}
|
package/dist/transform.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 主转换模块
|
|
3
|
+
* 负责解析 JSX/TSX 代码并转换为 createView 调用
|
|
4
|
+
* @module transform
|
|
5
|
+
*/
|
|
6
|
+
import { type GeneratorResult } from '@babel/generator';
|
|
7
|
+
export interface TransformResult extends GeneratorResult {
|
|
4
8
|
}
|
|
5
9
|
export interface CompileOptions {
|
|
6
10
|
hmr: boolean;
|
package/dist/transform.js
CHANGED
|
@@ -119,8 +119,8 @@ function transformAST(ast, ctx) {
|
|
|
119
119
|
*/
|
|
120
120
|
function generateCode(ast, code, id, sourceMap) {
|
|
121
121
|
const babelGenerate = typeof generate === 'object' ? generate.default : generate;
|
|
122
|
-
const output = babelGenerate(ast, { sourceMaps: !!sourceMap, filename: id }, code);
|
|
123
|
-
return { code: output.code, map: output.map
|
|
122
|
+
const output = babelGenerate(ast, { sourceMaps: !!sourceMap, filename: id, sourceFileName: id }, code);
|
|
123
|
+
return { code: output.code, map: output.map };
|
|
124
124
|
}
|
|
125
125
|
/**
|
|
126
126
|
* 转换 JSX/TSX 代码
|
|
@@ -4,10 +4,8 @@
|
|
|
4
4
|
* @module passes/transform/collect
|
|
5
5
|
*/
|
|
6
6
|
import * as t from '@babel/types';
|
|
7
|
-
import { DEFAULT_EXPORT_BASE_NAME
|
|
7
|
+
import { DEFAULT_EXPORT_BASE_NAME } from '../constants/index.js';
|
|
8
8
|
import { generateUniqueAlias } from './generate.js';
|
|
9
|
-
/** 纯编译组件名称集合 */
|
|
10
|
-
const PURE_COMPILE_COMPONENT_SET = new Set(PURE_COMPILE_COMPONENTS);
|
|
11
9
|
/**
|
|
12
10
|
* 生成唯一的默认导出组件名称
|
|
13
11
|
* 避免与已存在的名称冲突
|
|
@@ -62,54 +60,8 @@ function collectFromDefaultExport(node, names) {
|
|
|
62
60
|
}
|
|
63
61
|
// 匿名函数不在此处预留名称,而是在处理时动态生成唯一名称
|
|
64
62
|
}
|
|
65
|
-
/**
|
|
66
|
-
* 检查函数是否为组件函数
|
|
67
|
-
* 通过遍历 AST 检查是否包含 JSX 元素
|
|
68
|
-
*/
|
|
69
|
-
function isComponentFunction(node) {
|
|
70
|
-
let result = false;
|
|
71
|
-
const check = (n) => {
|
|
72
|
-
if (result)
|
|
73
|
-
return;
|
|
74
|
-
// 直接包含 JSX 元素
|
|
75
|
-
if (n.type === 'JSXElement' || n.type === 'JSXFragment') {
|
|
76
|
-
result = true;
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
// 检查 return 语句中的纯编译组件
|
|
80
|
-
if (n.type === 'ReturnStatement' && n.argument?.type === 'JSXElement') {
|
|
81
|
-
const opening = n.argument.openingElement;
|
|
82
|
-
if (opening.name.type === 'JSXIdentifier' &&
|
|
83
|
-
PURE_COMPILE_COMPONENT_SET.has(opening.name.name)) {
|
|
84
|
-
result = true;
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
// 递归遍历子节点
|
|
89
|
-
for (const key of Object.keys(n)) {
|
|
90
|
-
const child = n[key];
|
|
91
|
-
if (child && typeof child === 'object') {
|
|
92
|
-
if (Array.isArray(child)) {
|
|
93
|
-
for (const c of child) {
|
|
94
|
-
if (c && typeof child === 'object')
|
|
95
|
-
check(c);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
check(child);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
check(node);
|
|
105
|
-
return result;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* 尝试添加组件到列表
|
|
109
|
-
* 检查名称有效性、是否导出、是否为组件函数
|
|
110
|
-
*/
|
|
111
63
|
function tryAddComponent(name, node, exportedNames, components) {
|
|
112
|
-
if (isValidComponentName(name) && exportedNames.has(name)
|
|
64
|
+
if (isValidComponentName(name) && exportedNames.has(name)) {
|
|
113
65
|
components.push({ name, node });
|
|
114
66
|
}
|
|
115
67
|
}
|
|
@@ -149,26 +101,17 @@ function processAnonymousDefaultExport(node, exportedNames, components) {
|
|
|
149
101
|
const decl = node.declaration;
|
|
150
102
|
let funcNode = null;
|
|
151
103
|
let name;
|
|
152
|
-
// 匿名函数声明
|
|
153
104
|
if (decl.type === 'FunctionDeclaration' && !decl.id) {
|
|
154
|
-
if (!isComponentFunction(decl))
|
|
155
|
-
return;
|
|
156
105
|
name = generateUniqueDefaultName(exportedNames);
|
|
157
106
|
decl.id = t.identifier(name);
|
|
158
107
|
funcNode = decl;
|
|
159
108
|
}
|
|
160
|
-
// 函数表达式
|
|
161
109
|
else if (decl.type === 'FunctionExpression') {
|
|
162
|
-
if (!isComponentFunction(decl))
|
|
163
|
-
return;
|
|
164
110
|
name = generateUniqueDefaultName(exportedNames);
|
|
165
111
|
funcNode = convertToNamedFunctionDeclaration(decl, name);
|
|
166
112
|
node.declaration = funcNode;
|
|
167
113
|
}
|
|
168
|
-
// 箭头函数
|
|
169
114
|
else if (decl.type === 'ArrowFunctionExpression') {
|
|
170
|
-
if (!isComponentFunction(decl))
|
|
171
|
-
return;
|
|
172
115
|
name = generateUniqueDefaultName(exportedNames);
|
|
173
116
|
funcNode = convertToNamedFunctionDeclaration(decl, name);
|
|
174
117
|
node.declaration = funcNode;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitarx/plugin-vite",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.7",
|
|
4
4
|
"description": "The official plugin for Vitarx support in Vite.",
|
|
5
5
|
"author": "ZhuChongLin <8210856@qq.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -53,7 +53,8 @@
|
|
|
53
53
|
"prettier": "^3.8.1",
|
|
54
54
|
"rimraf": "^6.1.3",
|
|
55
55
|
"vitest": "^4.0.18",
|
|
56
|
-
"vite": "^8.0.2"
|
|
56
|
+
"vite": "^8.0.2",
|
|
57
|
+
"vitarx": "^4.0.0-beta.8"
|
|
57
58
|
},
|
|
58
59
|
"scripts": {
|
|
59
60
|
"build": "rimraf dist && tsc",
|