From 533f1a0e127b72416b3866bac7e3f036c73938ba Mon Sep 17 00:00:00 2001 From: zhiyuanzmj <260480378@qq.com> Date: Fri, 26 Sep 2025 11:29:37 +0800 Subject: [PATCH 1/4] refactor(compiler-vapor): remove duplicate text processing --- .../src/transforms/transformText.ts | 44 +++++-------------- .../compiler-vapor/src/transforms/vText.ts | 20 +-------- packages/compiler-vapor/src/utils.ts | 3 +- 3 files changed, 14 insertions(+), 53 deletions(-) diff --git a/packages/compiler-vapor/src/transforms/transformText.ts b/packages/compiler-vapor/src/transforms/transformText.ts index dd81bec1e80..612227a99cb 100644 --- a/packages/compiler-vapor/src/transforms/transformText.ts +++ b/packages/compiler-vapor/src/transforms/transformText.ts @@ -11,14 +11,10 @@ import { } from '@vue/compiler-dom' import type { NodeTransform, TransformContext } from '../transform' import { DynamicFlag, IRNodeTypes } from '../ir' -import { - getLiteralExpressionValue, - isConstantExpression, - isStaticExpression, -} from '../utils' +import { getLiteralExpressionValue } from '../utils' import { escapeHtml } from '@vue/shared' -type TextLike = TextNode | InterpolationNode +export type TextLike = TextNode | InterpolationNode const seen = new WeakMap< TransformContext, WeakSet @@ -62,7 +58,7 @@ export const transformText: NodeTransform = (node, context) => { // all text like with interpolation if (!isFragment && isAllTextLike && hasInterp) { processTextContainer( - node.children as TextLike[], + processTextLikeChildren(node.children as TextLike[], context), context as TransformContext, ) } else if (hasInterp) { @@ -112,35 +108,17 @@ function processInterpolation(context: TransformContext) { return } - const nonConstantExps = values.filter(v => !isConstantExpression(v)) - const isStatic = - !nonConstantExps.length || - nonConstantExps.every(e => - isStaticExpression(e, context.options.bindingMetadata), - ) || - context.inVOnce - - if (isStatic) { - context.registerOperation({ - type: IRNodeTypes.SET_TEXT, - element: id, - values, - }) - } else { - context.registerEffect(values, { - type: IRNodeTypes.SET_TEXT, - element: id, - values, - }) - } + context.registerEffect(values, { + type: IRNodeTypes.SET_TEXT, + element: id, + values, + }) } -function processTextContainer( - children: TextLike[], +export function processTextContainer( + values: SimpleExpressionNode[], context: TransformContext, -) { - const values = processTextLikeChildren(children, context) - +): void { const literals = values.map(getLiteralExpressionValue) if (literals.every(l => l != null)) { diff --git a/packages/compiler-vapor/src/transforms/vText.ts b/packages/compiler-vapor/src/transforms/vText.ts index 0832398e12a..7685cdba22f 100644 --- a/packages/compiler-vapor/src/transforms/vText.ts +++ b/packages/compiler-vapor/src/transforms/vText.ts @@ -1,9 +1,8 @@ import { DOMErrorCodes, createDOMCompilerError } from '@vue/compiler-dom' -import { IRNodeTypes } from '../ir' import { EMPTY_EXPRESSION } from './utils' import type { DirectiveTransform } from '../transform' -import { getLiteralExpressionValue } from '../utils' import { isVoidTag } from '../../../shared/src' +import { processTextContainer } from './transformText' export const transformVText: DirectiveTransform = (dir, node, context) => { let { exp, loc } = dir @@ -25,20 +24,5 @@ export const transformVText: DirectiveTransform = (dir, node, context) => { return } - const literal = getLiteralExpressionValue(exp) - if (literal != null) { - context.childrenTemplate = [String(literal)] - } else { - context.childrenTemplate = [' '] - context.registerOperation({ - type: IRNodeTypes.GET_TEXT_CHILD, - parent: context.reference(), - }) - context.registerEffect([exp], { - type: IRNodeTypes.SET_TEXT, - element: context.reference(), - values: [exp], - generated: true, - }) - } + processTextContainer([exp], context) } diff --git a/packages/compiler-vapor/src/utils.ts b/packages/compiler-vapor/src/utils.ts index 728281914fd..115cea97f40 100644 --- a/packages/compiler-vapor/src/utils.ts +++ b/packages/compiler-vapor/src/utils.ts @@ -1,4 +1,3 @@ -import type { BigIntLiteral, NumericLiteral, StringLiteral } from '@babel/types' import { isGloballyAllowed } from '@vue/shared' import { type AttributeNode, @@ -78,7 +77,7 @@ export function getLiteralExpressionValue( ): number | string | boolean | null { if (exp.ast) { if (exp.ast.type === 'StringLiteral') { - return (exp.ast as StringLiteral | NumericLiteral | BigIntLiteral).value + return exp.ast.value } else if ( exp.ast.type === 'TemplateLiteral' && exp.ast.expressions.length === 0 From 2c70cb2ab853c8e58bb610c25e180ee41e70f222 Mon Sep 17 00:00:00 2001 From: zhiyuanzmj <260480378@qq.com> Date: Fri, 26 Sep 2025 11:31:06 +0800 Subject: [PATCH 2/4] chore: update --- packages/compiler-vapor/src/transforms/transformText.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-vapor/src/transforms/transformText.ts b/packages/compiler-vapor/src/transforms/transformText.ts index 612227a99cb..da4845e02d0 100644 --- a/packages/compiler-vapor/src/transforms/transformText.ts +++ b/packages/compiler-vapor/src/transforms/transformText.ts @@ -14,7 +14,7 @@ import { DynamicFlag, IRNodeTypes } from '../ir' import { getLiteralExpressionValue } from '../utils' import { escapeHtml } from '@vue/shared' -export type TextLike = TextNode | InterpolationNode +type TextLike = TextNode | InterpolationNode const seen = new WeakMap< TransformContext, WeakSet From fc13a78bff1d08aa4bebbcf29ba898b6011f922d Mon Sep 17 00:00:00 2001 From: zhiyuanzmj <260480378@qq.com> Date: Sun, 28 Sep 2025 18:11:50 +0800 Subject: [PATCH 3/4] chore: update types --- packages/compiler-vapor/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-vapor/src/utils.ts b/packages/compiler-vapor/src/utils.ts index 115cea97f40..e61cfe6d693 100644 --- a/packages/compiler-vapor/src/utils.ts +++ b/packages/compiler-vapor/src/utils.ts @@ -74,7 +74,7 @@ export function resolveExpression( export function getLiteralExpressionValue( exp: SimpleExpressionNode, -): number | string | boolean | null { +): string | null { if (exp.ast) { if (exp.ast.type === 'StringLiteral') { return exp.ast.value From 52a6340afddeb974df7e407fc8fbe7d28c9acc50 Mon Sep 17 00:00:00 2001 From: zhiyuanzmj <260480378@qq.com> Date: Thu, 16 Oct 2025 17:24:36 +0800 Subject: [PATCH 4/4] chore: update --- packages/compiler-vapor/src/utils.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/compiler-vapor/src/utils.ts b/packages/compiler-vapor/src/utils.ts index e61cfe6d693..728281914fd 100644 --- a/packages/compiler-vapor/src/utils.ts +++ b/packages/compiler-vapor/src/utils.ts @@ -1,3 +1,4 @@ +import type { BigIntLiteral, NumericLiteral, StringLiteral } from '@babel/types' import { isGloballyAllowed } from '@vue/shared' import { type AttributeNode, @@ -74,10 +75,10 @@ export function resolveExpression( export function getLiteralExpressionValue( exp: SimpleExpressionNode, -): string | null { +): number | string | boolean | null { if (exp.ast) { if (exp.ast.type === 'StringLiteral') { - return exp.ast.value + return (exp.ast as StringLiteral | NumericLiteral | BigIntLiteral).value } else if ( exp.ast.type === 'TemplateLiteral' && exp.ast.expressions.length === 0