@vitarx/plugin-vite 1.0.3 → 1.0.5

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.
@@ -1,3 +1,9 @@
1
+ /**
2
+ * 名称生成工具模块
3
+ * 提供唯一别名和默认导出名称的生成功能
4
+ * @module utils/generate
5
+ */
6
+ import { DEFAULT_EXPORT_BASE_NAME } from '../constants/index.js';
1
7
  /**
2
8
  * 生成唯一的别名
3
9
  * 从 $1 开始递增,直到找到不冲突的名称
@@ -13,3 +19,17 @@ export function generateUniqueAlias(baseName, existingNames) {
13
19
  }
14
20
  return `${baseName}$${index}`;
15
21
  }
22
+ /**
23
+ * 生成唯一的默认导出组件名称
24
+ * 优先使用基础名称,若冲突则生成唯一别名
25
+ * @param existingNames - 已存在的名称集合
26
+ * @returns 唯一的默认导出名称
27
+ */
28
+ export function generateUniqueDefaultName(existingNames) {
29
+ // 如果默认名称未被占用,直接返回
30
+ if (!existingNames.has(DEFAULT_EXPORT_BASE_NAME)) {
31
+ return DEFAULT_EXPORT_BASE_NAME;
32
+ }
33
+ // 否则生成唯一别名
34
+ return generateUniqueAlias(DEFAULT_EXPORT_BASE_NAME, existingNames);
35
+ }
@@ -1,8 +1,8 @@
1
- export { isWhitespaceJSXText } from './ast-guards.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';
1
+ export { isWhitespaceJSXText, isJSXComment } from './ast-guards.js';
2
+ export { getJSXElementName, resolveJSXElementType, isPureCompileComponent, isComponent, isNativeElement, getJSXAttributeByName, hasDirective, getDirectiveValue, isVIfChain, isVIf, isVElseIf, isVElse, removeVDirectives, removeVIfChainDirectives, removeAttribute, filterWhitespaceChildren, hasEffectiveChildren, filterEffectiveChildren } 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';
6
6
  export { validateVIfChain, collectVIfChainInfo, collectFragmentVIfChains, type VIfChainInfo } from './vif-helpers.js';
7
7
  export { collectComponentFunctions, type ComponentInfo } from './component-collect.js';
8
- export { generateUniqueAlias } from './generate.js';
8
+ export { generateUniqueAlias, generateUniqueDefaultName } from './generate.js';
@@ -1,7 +1,7 @@
1
1
  // 类型守卫
2
- export { isWhitespaceJSXText } from './ast-guards.js';
2
+ export { isWhitespaceJSXText, isJSXComment } from './ast-guards.js';
3
3
  // JSX 辅助函数
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';
4
+ export { getJSXElementName, resolveJSXElementType, isPureCompileComponent, isComponent, isNativeElement, getJSXAttributeByName, hasDirective, getDirectiveValue, isVIfChain, isVIf, isVElseIf, isVElse, removeVDirectives, removeVIfChainDirectives, removeAttribute, filterWhitespaceChildren, hasEffectiveChildren, filterEffectiveChildren } 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
  // 模式处理辅助函数
@@ -13,4 +13,4 @@ export { validateVIfChain, collectVIfChainInfo, collectFragmentVIfChains } from
13
13
  // 组件收集
14
14
  export { collectComponentFunctions } from './component-collect.js';
15
15
  // 生成器
16
- export { generateUniqueAlias } from './generate.js';
16
+ export { generateUniqueAlias, generateUniqueDefaultName } from './generate.js';
@@ -101,8 +101,5 @@ export declare function hasEffectiveChildren(node: JSXElement): boolean;
101
101
  */
102
102
  export declare function filterEffectiveChildren(node: JSXElement): t.Node[];
103
103
  /**
104
- * 校验子节点中不能直接包含 Match 组件
105
- * Match 必须在 Switch 内使用,非 Switch 元素的子节点中不允许出现 Match
106
- * @param children - 子节点数组
104
+ * 过滤掉空白文本子节点
107
105
  */
108
- export declare function validateNoDirectMatchChild(children: t.Node[]): void;
@@ -4,9 +4,8 @@
4
4
  * @module utils/jsx-helpers
5
5
  */
6
6
  import * as t from '@babel/types';
7
- import { isJSXElement, isJSXText } from '@babel/types';
7
+ import { isJSXText } from '@babel/types';
8
8
  import { DIRECTIVE_PREFIX, PURE_COMPILE_COMPONENTS, V_IF_CHAIN_DIRECTIVES } from '../constants/index.js';
9
- import { createError } from '../error.js';
10
9
  import { isWhitespaceJSXText } from './ast-guards.js';
11
10
  /**
12
11
  * 获取 JSX 元素的名称
@@ -253,17 +252,5 @@ export function filterEffectiveChildren(node) {
253
252
  return node.children.filter(isEffectiveChild);
254
253
  }
255
254
  /**
256
- * 校验子节点中不能直接包含 Match 组件
257
- * Match 必须在 Switch 内使用,非 Switch 元素的子节点中不允许出现 Match
258
- * @param children - 子节点数组
255
+ * 过滤掉空白文本子节点
259
256
  */
260
- export function validateNoDirectMatchChild(children) {
261
- for (const child of children) {
262
- if (isJSXElement(child)) {
263
- const childName = getJSXElementName(child);
264
- if (childName === 'Match') {
265
- throw createError('E012', child);
266
- }
267
- }
268
- }
269
- }
@@ -6,7 +6,7 @@
6
6
  import * as t from '@babel/types';
7
7
  import { isJSXElement, isJSXText } from '@babel/types';
8
8
  import { createError } from '../error.js';
9
- import { isWhitespaceJSXText } from './ast-guards.js';
9
+ import { isJSXComment, isWhitespaceJSXText } from './ast-guards.js';
10
10
  import { getDirectiveValue, hasDirective, isVElse, isVElseIf, isVIf, isVIfChain } from './jsx-helpers.js';
11
11
  /**
12
12
  * 验证 v-if 链的合法性
@@ -125,7 +125,9 @@ export function collectFragmentVIfChains(children) {
125
125
  */
126
126
  function collectSingleChainFromFragment(children, startIndex) {
127
127
  const nodes = [children[startIndex]];
128
- const conditions = [getDirectiveValue(nodes[0], 'v-if')];
128
+ // v-if 指令值已由 validateVIfChain 保证存在,fallback 为 true(无表达式时)
129
+ const vIfValue = getDirectiveValue(nodes[0], 'v-if') ?? t.booleanLiteral(true);
130
+ const conditions = [vIfValue];
129
131
  let j = startIndex + 1;
130
132
  // 继续收集后续的 v-else-if 和 v-else
131
133
  while (j < children.length) {
@@ -135,6 +137,11 @@ function collectSingleChainFromFragment(children, startIndex) {
135
137
  j++;
136
138
  continue;
137
139
  }
140
+ // 跳过 JSX 注释节点(允许链元素间有注释,如 {/* comment *\/})
141
+ if (isJSXComment(nextChild)) {
142
+ j++;
143
+ continue;
144
+ }
138
145
  // 遇到非 JSXElement,链结束
139
146
  if (!isJSXElement(nextChild))
140
147
  break;
@@ -142,9 +149,10 @@ function collectSingleChainFromFragment(children, startIndex) {
142
149
  if (!isVIfChain(nextChild))
143
150
  break;
144
151
  if (isVElseIf(nextChild)) {
145
- // 收集 v-else-if
152
+ // 收集 v-else-if,值已由 validateVIfChain 保证存在
146
153
  nodes.push(nextChild);
147
- conditions.push(getDirectiveValue(nextChild, 'v-else-if'));
154
+ const vElseIfValue = getDirectiveValue(nextChild, 'v-else-if') ?? t.booleanLiteral(true);
155
+ conditions.push(vElseIfValue);
148
156
  j++;
149
157
  }
150
158
  else if (isVElse(nextChild)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitarx/plugin-vite",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "The official plugin for Vitarx support in Vite.",
5
5
  "author": "ZhuChongLin <8210856@qq.com>",
6
6
  "license": "MIT",
@@ -61,7 +61,7 @@
61
61
  "rimraf": "^6.1.3",
62
62
  "vitest": "^4.0.18",
63
63
  "vite": "^8.0.5",
64
- "vitarx": "^4.0.0"
64
+ "vitarx": "^4.0.6"
65
65
  },
66
66
  "scripts": {
67
67
  "build": "vitest run && rimraf dist && tsc -p tsconfig.build.json",