@vitarx/plugin-vite 1.0.0-beta.1 → 1.0.0-beta.3

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.
Files changed (41) hide show
  1. package/dist/constants/index.d.ts +1 -0
  2. package/dist/constants/index.js +1 -0
  3. package/dist/context.d.ts +2 -0
  4. package/dist/context.js +1 -0
  5. package/dist/hmr-client/update.js +21 -4
  6. package/dist/hmr-client/utils.js +11 -3
  7. package/dist/index.js +9 -10
  8. package/dist/passes/components/ifBlock.d.ts +3 -0
  9. package/dist/passes/components/ifBlock.js +24 -8
  10. package/dist/passes/components/switch.js +49 -21
  11. package/dist/passes/directives/processDirectives.d.ts +3 -1
  12. package/dist/passes/directives/processDirectives.js +36 -9
  13. package/dist/passes/directives/vIf.d.ts +7 -2
  14. package/dist/passes/directives/vIf.js +43 -30
  15. package/dist/passes/hmr/inject-state.js +64 -7
  16. package/dist/passes/hmr/inject.js +4 -3
  17. package/dist/passes/imports/collectImports.d.ts +1 -4
  18. package/dist/passes/imports/collectImports.js +4 -5
  19. package/dist/passes/imports/collectRefVariables.d.ts +7 -0
  20. package/dist/passes/imports/collectRefVariables.js +69 -3
  21. package/dist/passes/imports/index.d.ts +1 -1
  22. package/dist/passes/imports/index.js +1 -1
  23. package/dist/passes/index.d.ts +1 -1
  24. package/dist/passes/index.js +1 -1
  25. package/dist/passes/jsx/processChildren.js +30 -1
  26. package/dist/passes/jsx/processJSXElement.js +27 -25
  27. package/dist/passes/jsx/processJSXFragment.d.ts +1 -1
  28. package/dist/passes/jsx/processJSXFragment.js +3 -2
  29. package/dist/passes/props/attribute.d.ts +3 -1
  30. package/dist/passes/props/attribute.js +92 -16
  31. package/dist/transform.js +3 -2
  32. package/dist/utils/ast-builders.js +2 -11
  33. package/dist/utils/branch-factory.js +1 -1
  34. package/dist/utils/collect-names.js +29 -0
  35. package/dist/utils/index.d.ts +1 -1
  36. package/dist/utils/index.js +1 -1
  37. package/dist/utils/jsx-helpers.d.ts +6 -0
  38. package/dist/utils/jsx-helpers.js +28 -3
  39. package/dist/utils/vif-helpers.d.ts +1 -3
  40. package/dist/utils/vif-helpers.js +2 -2
  41. package/package.json +1 -1
package/dist/transform.js CHANGED
@@ -7,7 +7,7 @@ import generate from '@babel/generator';
7
7
  import { parse } from '@babel/parser';
8
8
  import traverse from '@babel/traverse';
9
9
  import { createContext } from './context.js';
10
- import { collectExistingImports, collectLocalBindings, collectRefApiAliases, collectRefVariables, injectHMRSupport, injectImports, processJSXElement, processJSXFragment, processPureCompileComponent, processVIfChain, transformJSXElement } from './passes/index.js';
10
+ import { collectExistingImports, collectLocalBindings, collectNonRefVariables, collectRefApiAliases, collectRefVariables, injectHMRSupport, injectImports, processJSXElement, processJSXFragment, processPureCompileComponent, processVIfChain, transformJSXElement } from './passes/index.js';
11
11
  import { collectComponentFunctions, generateUniqueAlias, getJSXElementName, isPureCompileComponent } from './utils/index.js';
12
12
  /**
13
13
  * 创建解析器选项
@@ -33,7 +33,7 @@ function createParserOptions() {
33
33
  * 设置 API 别名
34
34
  */
35
35
  function setupAliases(ctx, program) {
36
- const { vitarxImports } = collectExistingImports(program);
36
+ const vitarxImports = collectExistingImports(program);
37
37
  const localBindings = collectLocalBindings(program);
38
38
  const allNames = new Set([...localBindings]);
39
39
  const apiNames = [
@@ -68,6 +68,7 @@ function collectRefInfo(ctx, program) {
68
68
  const refApiAliases = collectRefApiAliases(program);
69
69
  ctx.refApiAliases = refApiAliases;
70
70
  ctx.refVariables = collectRefVariables(program, refApiAliases);
71
+ ctx.nonRefVariables = collectNonRefVariables(program);
71
72
  }
72
73
  /**
73
74
  * 转换 AST
@@ -3,7 +3,7 @@
3
3
  * @module utils/ast-builders
4
4
  */
5
5
  import * as t from '@babel/types';
6
- import { isIdentifier, isStringLiteral } from '@babel/types';
6
+ import { isIdentifier } from '@babel/types';
7
7
  import { PURE_COMMENT } from '../constants/index.js';
8
8
  /**
9
9
  * 创建 unref 调用
@@ -22,16 +22,7 @@ export function createUnrefCall(argument, alias) {
22
22
  * @returns CallExpression
23
23
  */
24
24
  export function createAccessorCall(object, key, alias) {
25
- let keyArg;
26
- if (isIdentifier(key)) {
27
- keyArg = t.stringLiteral(key.name);
28
- }
29
- else if (isStringLiteral(key)) {
30
- keyArg = key;
31
- }
32
- else {
33
- keyArg = key;
34
- }
25
+ const keyArg = isIdentifier(key) ? t.stringLiteral(key.name) : key;
35
26
  return t.callExpression(t.identifier(alias || 'accessor'), [object, keyArg]);
36
27
  }
37
28
  /**
@@ -16,7 +16,7 @@ import { addPureComment, createArrowFunction, createBranchCall, createUnrefCall,
16
16
  export function createBranch(config, ctx) {
17
17
  const { conditions, branches, useRef = true } = config;
18
18
  markImport(ctx, 'branch');
19
- if (useRef && conditions.some(c => isIdentifier(c))) {
19
+ if (useRef && conditions.some(c => isIdentifier(c) && ctx.refVariables.has(c.name))) {
20
20
  markImport(ctx, 'unref');
21
21
  }
22
22
  const conditionExpr = buildNestedCondition(conditions, ctx, useRef);
@@ -124,6 +124,35 @@ export function collectBuilderWrappedNames(program, builderAlias) {
124
124
  }
125
125
  }
126
126
  }
127
+ else if (node.type === 'ExportDefaultDeclaration' && node.declaration.type === 'CallExpression') {
128
+ const callExpr = node.declaration;
129
+ if (isBuilderCall(callExpr, builderAlias)) {
130
+ const arg = callExpr.arguments[0];
131
+ if (arg?.type === 'Identifier') {
132
+ wrappedNames.add(arg.name);
133
+ }
134
+ }
135
+ }
136
+ else if (node.type === 'VariableDeclaration') {
137
+ for (const decl of node.declarations) {
138
+ if (decl.init?.type === 'CallExpression' && isBuilderCall(decl.init, builderAlias)) {
139
+ const arg = decl.init.arguments[0];
140
+ if (arg?.type === 'Identifier') {
141
+ wrappedNames.add(arg.name);
142
+ }
143
+ }
144
+ }
145
+ }
146
+ else if (node.type === 'ExportNamedDeclaration' && node.declaration?.type === 'VariableDeclaration') {
147
+ for (const decl of node.declaration.declarations) {
148
+ if (decl.init?.type === 'CallExpression' && isBuilderCall(decl.init, builderAlias)) {
149
+ const arg = decl.init.arguments[0];
150
+ if (arg?.type === 'Identifier') {
151
+ wrappedNames.add(arg.name);
152
+ }
153
+ }
154
+ }
155
+ }
127
156
  }
128
157
  return wrappedNames;
129
158
  }
@@ -1,5 +1,5 @@
1
1
  export { isWhitespaceJSXText } from './ast-guards.js';
2
- export { getJSXElementName, isPureCompileComponent, isComponent, isNativeElement, getJSXAttributeByName, hasDirective, getDirectiveValue, isVIfChain, isVIf, isVElseIf, isVElse, removeVDirectives, removeVIfChainDirectives, removeAttribute, filterWhitespaceChildren, hasEffectiveChildren, filterEffectiveChildren, validateNoDirectMatchChild } from './jsx-helpers.js';
2
+ export { getJSXElementName, resolveJSXElementType, isPureCompileComponent, isComponent, isNativeElement, getJSXAttributeByName, hasDirective, getDirectiveValue, isVIfChain, isVIf, isVElseIf, isVElse, removeVDirectives, removeVIfChainDirectives, removeAttribute, filterWhitespaceChildren, hasEffectiveChildren, filterEffectiveChildren, validateNoDirectMatchChild } from './jsx-helpers.js';
3
3
  export { createUnrefCall, createAccessorCall, createExprCall, createBranchCall, createCreateViewCall, createWithDirectivesCall, createArrowFunction, createLocationObject, addPureComment, getAlias, getDevLocInfo } from './ast-builders.js';
4
4
  export { collectPatternBindings, collectObjectPatternBindings } from './pattern-helpers.js';
5
5
  export { createBranch, createBinaryBranch, buildNestedCondition, type BranchConfig } from './branch-factory.js';
@@ -1,7 +1,7 @@
1
1
  // 类型守卫
2
2
  export { isWhitespaceJSXText } from './ast-guards.js';
3
3
  // JSX 辅助函数
4
- export { getJSXElementName, isPureCompileComponent, isComponent, isNativeElement, getJSXAttributeByName, hasDirective, getDirectiveValue, isVIfChain, isVIf, isVElseIf, isVElse, removeVDirectives, removeVIfChainDirectives, removeAttribute, filterWhitespaceChildren, hasEffectiveChildren, filterEffectiveChildren, validateNoDirectMatchChild } from './jsx-helpers.js';
4
+ export { getJSXElementName, resolveJSXElementType, isPureCompileComponent, isComponent, isNativeElement, getJSXAttributeByName, hasDirective, getDirectiveValue, isVIfChain, isVIf, isVElseIf, isVElse, removeVDirectives, removeVIfChainDirectives, removeAttribute, filterWhitespaceChildren, hasEffectiveChildren, filterEffectiveChildren, validateNoDirectMatchChild } from './jsx-helpers.js';
5
5
  // AST 构建函数
6
6
  export { createUnrefCall, createAccessorCall, createExprCall, createBranchCall, createCreateViewCall, createWithDirectivesCall, createArrowFunction, createLocationObject, addPureComment, getAlias, getDevLocInfo } from './ast-builders.js';
7
7
  // 模式处理辅助函数
@@ -8,6 +8,12 @@ import { type Expression, type JSXAttribute, type JSXElement } from '@babel/type
8
8
  * 获取 JSX 元素的名称
9
9
  */
10
10
  export declare function getJSXElementName(node: JSXElement): string | null;
11
+ /**
12
+ * 将 JSX 元素名称节点转换为 createView 的类型表达式
13
+ * JSXIdentifier → Identifier(组件)或 StringLiteral(原生元素)
14
+ * JSXMemberExpression → MemberExpression(如 Obj.Key → Obj.Key)
15
+ */
16
+ export declare function resolveJSXElementType(node: JSXElement): t.Expression | null;
11
17
  /**
12
18
  * 判断名称是否为纯编译组件
13
19
  */
@@ -4,7 +4,7 @@
4
4
  */
5
5
  import * as t from '@babel/types';
6
6
  import { isJSXElement, isJSXText } from '@babel/types';
7
- import { DIRECTIVE_PREFIX, PURE_COMPILE_COMPONENTS } from '../constants/index.js';
7
+ import { DIRECTIVE_PREFIX, PURE_COMPILE_COMPONENTS, V_IF_CHAIN_DIRECTIVES } from '../constants/index.js';
8
8
  import { createError } from '../error.js';
9
9
  import { isWhitespaceJSXText } from './ast-guards.js';
10
10
  /**
@@ -14,6 +14,32 @@ export function getJSXElementName(node) {
14
14
  const nameNode = node.openingElement.name;
15
15
  return nameNode.type === 'JSXIdentifier' ? nameNode.name : null;
16
16
  }
17
+ /**
18
+ * 将 JSX 元素名称节点转换为 createView 的类型表达式
19
+ * JSXIdentifier → Identifier(组件)或 StringLiteral(原生元素)
20
+ * JSXMemberExpression → MemberExpression(如 Obj.Key → Obj.Key)
21
+ */
22
+ export function resolveJSXElementType(node) {
23
+ const nameNode = node.openingElement.name;
24
+ if (nameNode.type === 'JSXIdentifier') {
25
+ const name = nameNode.name;
26
+ return isNativeElement(name) ? t.stringLiteral(name) : t.identifier(name);
27
+ }
28
+ if (nameNode.type === 'JSXMemberExpression') {
29
+ return jsxMemberExprToMemberExpr(nameNode);
30
+ }
31
+ return null;
32
+ }
33
+ /**
34
+ * 递归转换 JSXMemberExpression 为 MemberExpression
35
+ * Obj.Key → Obj.Key, A.B.C → A.B.C
36
+ */
37
+ function jsxMemberExprToMemberExpr(node) {
38
+ const object = node.object.type === 'JSXMemberExpression'
39
+ ? jsxMemberExprToMemberExpr(node.object)
40
+ : t.identifier(node.object.name);
41
+ return t.memberExpression(object, t.identifier(node.property.name));
42
+ }
17
43
  /**
18
44
  * 判断名称是否为纯编译组件
19
45
  */
@@ -24,7 +50,7 @@ export function isPureCompileComponent(name) {
24
50
  * 判断名称是否为组件(首字母大写)
25
51
  */
26
52
  export function isComponent(name) {
27
- return name[0] === name[0]?.toUpperCase();
53
+ return name.length > 0 && name[0] === name[0].toUpperCase();
28
54
  }
29
55
  /**
30
56
  * 判断名称是否为原生元素
@@ -45,7 +71,6 @@ export function getJSXAttributeByName(node, name) {
45
71
  }
46
72
  return undefined;
47
73
  }
48
- const V_IF_CHAIN_DIRECTIVES = new Set(['v-if', 'v-else-if', 'v-else']);
49
74
  /**
50
75
  * 检查元素是否具有指定指令
51
76
  * v-if/v-else-if/v-else 仅支持 JSXIdentifier 格式(v-if),不支持命名空间格式(v:if)
@@ -8,11 +8,9 @@ import * as t from '@babel/types';
8
8
  * v-if 链信息
9
9
  */
10
10
  export interface VIfChainInfo {
11
- /** 链中的元素节点 */
12
11
  nodes: t.JSXElement[];
13
- /** 条件表达式数组 */
14
12
  conditions: t.Expression[];
15
- /** 结束索引 */
13
+ startIndex: number;
16
14
  endIndex: number;
17
15
  }
18
16
  /**
@@ -55,7 +55,7 @@ export function collectVIfChainInfo(nodes) {
55
55
  }
56
56
  throw createError('E008', node, 'Element missing v-if/v-else-if/v-else directive');
57
57
  }
58
- return { nodes, conditions, endIndex: nodes.length - 1 };
58
+ return { nodes, conditions, startIndex: 0, endIndex: nodes.length - 1 };
59
59
  }
60
60
  /**
61
61
  * 收集 Fragment 中的 v-if 链
@@ -123,5 +123,5 @@ function collectSingleChainFromFragment(children, startIndex) {
123
123
  break;
124
124
  }
125
125
  }
126
- return { nodes, conditions, endIndex: j - 1 };
126
+ return { nodes, conditions, startIndex, endIndex: j - 1 };
127
127
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitarx/plugin-vite",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.3",
4
4
  "description": "The official plugin for Vitarx support in Vite.",
5
5
  "author": "ZhuChongLin <8210856@qq.com>",
6
6
  "license": "MIT",