diff --git a/packages/compiler-core/src/component/component.ts b/packages/compiler-core/src/component/component.ts index 78eb385..a273499 100644 --- a/packages/compiler-core/src/component/component.ts +++ b/packages/compiler-core/src/component/component.ts @@ -4,9 +4,9 @@ * @Description: Coding something */ import type { NodePath } from '@babel/traverse'; -import type { Identifier, JSXAttribute, JSXElement, JSXOpeningElement, Node } from '@babel/types'; +import type { Identifier, JSXAttribute, JSXElement, JSXOpeningElement } from '@babel/types'; import { AlinsVar, ImportManager } from '../controller/import-manager'; -import { isOriginJSXElement } from '../is'; +import { isEmptyText, isOriginJSXElement } from '../is'; import { createCtxCall, findAttributes, getT, parseAttributes } from '../parse-utils'; import { transformSwitchChildren } from './logic-attribute'; @@ -41,9 +41,6 @@ function wrapChildren ( return t.arrowFunctionExpression(args, content); } -function isEmptyText (node: Node) { - return node?.type === 'JSXText' && node.value.trim() === ''; -} function createNext (path: NodePath) { let index = 0; diff --git a/packages/compiler-core/src/component/logic-attribute.ts b/packages/compiler-core/src/component/logic-attribute.ts index 6914fe3..8c325d6 100644 --- a/packages/compiler-core/src/component/logic-attribute.ts +++ b/packages/compiler-core/src/component/logic-attribute.ts @@ -1,7 +1,7 @@ import type { NodePath } from '@babel/traverse'; import type { JSXAttribute, JSXElement } from '@babel/types'; import { findAttributes, getT } from '../parse-utils'; -import { isMountAttr } from '../is'; +import { isEmptyText, isMountAttr } from '../is'; /* * @Author: chenzhongsheng @@ -55,7 +55,7 @@ function transformIfAttr ( for (;;) { next = next.getNextSibling(); - if (!next.node || (next.type === 'JSXText' && !!next.node.value.trim())) { + if (!next.node || (isEmptyText(next.node))) { break; } if (next.type === 'JSXElement') { @@ -116,6 +116,11 @@ export function transformSwitchChildren ( for (let i = 0; i < children.length; i++) { const child = children[i]; + if (child.type === 'JSXText') { + if (isEmptyText(child)) continue; + throw new Error('switch 中只能包含jsxElement'); + } + const attrs = child.openingElement?.attributes; if (!attrs) continue; @@ -149,7 +154,7 @@ export function transformSwitchChildren ( child ); children[i] = newNode; - return children; + if (!isCase) return children; } } } diff --git a/packages/compiler-core/src/is.ts b/packages/compiler-core/src/is.ts index edd3ffb..59aa81a 100644 --- a/packages/compiler-core/src/is.ts +++ b/packages/compiler-core/src/is.ts @@ -270,4 +270,8 @@ export function isMemberExp (node: Node) { export function isMountAttr (name: string) { return name === '$mount' || name.startsWith('$$'); +} + +export function isEmptyText (node: Node) { + return node?.type === 'JSXText' && node.value.trim() === ''; } \ No newline at end of file diff --git a/scripts/build/rollup.config.js b/scripts/build/rollup.config.js index 1072642..abd00cc 100644 --- a/scripts/build/rollup.config.js +++ b/scripts/build/rollup.config.js @@ -99,7 +99,11 @@ function parseBuildConfig () { packageName: 'eslint-config-alins', format: 'cjs', }, - 'plugin-babel-preset': { + 'plugin-babel-preset': isDev ? { + packageName: 'babel-preset-alins', + format: 'cjs', + external: false, + } : { packageName: 'babel-preset-alins', type: 'node', // format: 'esm cjs', diff --git a/scripts/dev/samples/playground.jsx b/scripts/dev/samples/playground.jsx index 111efbe..6d66ad3 100644 --- a/scripts/dev/samples/playground.jsx +++ b/scripts/dev/samples/playground.jsx @@ -1,4 +1,10 @@ - console.log('cc', e)} - $mounted={e => console.log('mm', e)} ->11; \ No newline at end of file +function Main () { + let count = 0; + const add = () => {count++;}; + return
+ + + +
; +} +
; \ No newline at end of file