@vitarx/plugin-vite 0.0.1-alpha.1 → 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/index.js +3 -4
- package/dist/passes/directives/processDirectives.d.ts +2 -1
- package/dist/passes/directives/processDirectives.js +14 -9
- package/dist/passes/props/attribute.js +21 -25
- package/dist/passes/props/index.js +1 -1
- package/dist/passes/props/types.d.ts +12 -2
- package/package.json +1 -1
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
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import * as t from '@babel/types';
|
|
7
7
|
import { TransformContext } from '../../context.js';
|
|
8
|
+
import type { DirectiveInfo } from '../../passes/props/types.js';
|
|
8
9
|
/**
|
|
9
10
|
* 处理指令
|
|
10
11
|
* 将指令转换为 withDirectives 调用
|
|
@@ -13,4 +14,4 @@ import { TransformContext } from '../../context.js';
|
|
|
13
14
|
* @param ctx - 转换上下文
|
|
14
15
|
* @returns 处理后的调用表达式
|
|
15
16
|
*/
|
|
16
|
-
export declare function processDirectives(viewCall: t.CallExpression, directives: Map<string,
|
|
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
|
}
|
|
@@ -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
|
}
|
|
@@ -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') {
|
|
@@ -3,14 +3,23 @@
|
|
|
3
3
|
* @module passes/props/types
|
|
4
4
|
*/
|
|
5
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
|
+
}
|
|
6
15
|
/**
|
|
7
16
|
* Props 处理结果
|
|
8
17
|
*/
|
|
9
18
|
export interface PropsResult {
|
|
10
19
|
/** 处理后的 props 对象表达式,无属性时为 null */
|
|
11
20
|
props: t.ObjectExpression | null;
|
|
12
|
-
/** 指令映射表,key 为指令名,value
|
|
13
|
-
directives: Map<string,
|
|
21
|
+
/** 指令映射表,key 为指令名,value 为指令信息 */
|
|
22
|
+
directives: Map<string, DirectiveInfo>;
|
|
14
23
|
/** 是否包含 v-bind 展开属性 */
|
|
15
24
|
hasVBind: boolean;
|
|
16
25
|
}
|
|
@@ -36,6 +45,7 @@ export type AttributeResult = {
|
|
|
36
45
|
value: t.Expression;
|
|
37
46
|
isVBind: boolean;
|
|
38
47
|
isVModel: boolean;
|
|
48
|
+
arg?: string;
|
|
39
49
|
} | {
|
|
40
50
|
type: 'property';
|
|
41
51
|
property: t.ObjectProperty | t.ObjectMethod;
|