@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 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: sourcemap === 'inline' ? 'inline' : sourcemap === true ? 'both' : false
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
- }, result.map, viteConfig);
59
+ }, this.getCombinedSourcemap(), viteConfig);
61
60
  }
62
- return result;
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, t.Expression>, ctx: TransformContext): t.CallExpression;
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, value] of directives) {
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
- return t.objectExpression([
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
- return t.objectExpression([
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
- * 支持 v:xxx 格式的指令语法
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
- // v:xxx 指令
86
- if (namespace === 'v') {
87
- const directiveName = `v-${name}`;
88
- const value = getAttributeValue(attr.value);
89
- // v:bind 等同于 v-bind
90
- if (directiveName === 'v-bind') {
91
- return { type: 'directive', name: 'v-bind', value, isVBind: true, isVModel: false };
92
- }
93
- // 其他指令(排除 v-if 系列和 v-model)
94
- if (!['v-if', 'v-else-if', 'v-else', 'v-model'].includes(directiveName)) {
95
- return { type: 'directive', name: directiveName, value, isVBind: false, isVModel: false };
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(t.identifier(key), value);
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', t.identifier(key), [], t.blockStatement([t.returnStatement(t.memberExpression(value, t.identifier('value')))]));
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(t.identifier(key), value);
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', t.identifier(key), [], t.blockStatement([t.returnStatement(t.callExpression(t.identifier(unrefAlias), [value]))]));
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, t.Expression>;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitarx/plugin-vite",
3
- "version": "0.0.1-alpha.1",
3
+ "version": "0.0.1-alpha.2",
4
4
  "description": "The official plugin for Vitarx support in Vite.",
5
5
  "author": "ZhuChongLin <8210856@qq.com>",
6
6
  "license": "MIT",