Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 00c6e6d

Browse files
committed
refactor(compiler-vapor): inline literal value into template
1 parent 107569b commit 00c6e6d

File tree

6 files changed

+46
-29
lines changed

6 files changed

+46
-29
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`compiler: children transform > children & sibling references 1`] = `
4-
"import { next as _next, setText as _setText, createTextNode as _createTextNode, insert as _insert, template as _template } from 'vue/vapor';
4+
"import { next as _next, createTextNode as _createTextNode, insert as _insert, renderEffect as _renderEffect, setText as _setText, template as _template } from 'vue/vapor';
55
const t0 = _template("<div><p></p> <!><p></p></div>")
66
77
export function render(_ctx) {
88
const n4 = t0()
99
const n0 = n4.firstChild
1010
const n3 = _next(n0, 2)
1111
const n2 = n3.nextSibling
12-
_setText(n0, 'first')
13-
const n1 = _createTextNode(['second', " ", 'third', " "])
14-
_setText(n2, 'forth')
12+
const n1 = _createTextNode(() => [_ctx.second, " ", _ctx.third, " "])
1513
_insert(n1, n4, n3)
14+
_renderEffect(() => _setText(n0, _ctx.first))
15+
_renderEffect(() => _setText(n2, _ctx.forth))
1616
return n4
1717
}"
1818
`;

packages/compiler-vapor/__tests__/transforms/__snapshots__/vText.spec.ts.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ export function render(_ctx) {
2323
`;
2424

2525
exports[`v-text > should raise error if has no expression 1`] = `
26-
"import { setText as _setText, template as _template } from 'vue/vapor';
26+
"import { template as _template } from 'vue/vapor';
2727
const t0 = _template("<div></div>")
2828
2929
export function render(_ctx) {
3030
const n0 = t0()
31-
_setText(n0, "")
3231
return n0
3332
}"
3433
`;

packages/compiler-vapor/__tests__/transforms/transformChildren.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ describe('compiler: children transform', () => {
2121
test('children & sibling references', () => {
2222
const { code, vaporHelpers } = compileWithElementTransform(
2323
`<div>
24-
<p>{{'first'}}</p>
25-
{{'second'}}
26-
{{'third'}}
27-
<p>{{'forth'}}</p>
24+
<p>{{ first }}</p>
25+
{{ second }}
26+
{{ third }}
27+
<p>{{ forth }}</p>
2828
</div>`,
2929
)
3030
expect(code).toMatchSnapshot()

packages/compiler-vapor/src/transforms/transformText.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@vue/compiler-dom'
1212
import type { NodeTransform, TransformContext } from '../transform'
1313
import { DynamicFlag, IRNodeTypes } from '../ir'
14-
import { isConstantExpression } from '../utils'
14+
import { getLiteralExpressionValue, isConstantExpression } from '../utils'
1515

1616
type TextLike = TextNode | InterpolationNode
1717
const seen = new WeakMap<
@@ -65,11 +65,16 @@ function processTextLikeContainer(
6565
context: TransformContext<ElementNode>,
6666
) {
6767
const values = children.map(child => createTextLikeExpression(child, context))
68-
context.registerEffect(values, {
69-
type: IRNodeTypes.SET_TEXT,
70-
element: context.reference(),
71-
values,
72-
})
68+
const literals = values.map(getLiteralExpressionValue)
69+
if (literals.every(l => l != null)) {
70+
context.childrenTemplate = literals.map(l => String(l))
71+
} else {
72+
context.registerEffect(values, {
73+
type: IRNodeTypes.SET_TEXT,
74+
element: context.reference(),
75+
values,
76+
})
77+
}
7378
}
7479

7580
function createTextLikeExpression(node: TextLike, context: TransformContext) {

packages/compiler-vapor/src/transforms/vText.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DOMErrorCodes, createDOMCompilerError } from '@vue/compiler-dom'
22
import { IRNodeTypes } from '../ir'
33
import { EMPTY_EXPRESSION } from './utils'
44
import type { DirectiveTransform } from '../transform'
5+
import { getLiteralExpressionValue } from '../utils'
56

67
export const transformVText: DirectiveTransform = (dir, node, context) => {
78
let { exp, loc } = dir
@@ -18,9 +19,14 @@ export const transformVText: DirectiveTransform = (dir, node, context) => {
1819
context.childrenTemplate.length = 0
1920
}
2021

21-
context.registerEffect([exp], {
22-
type: IRNodeTypes.SET_TEXT,
23-
element: context.reference(),
24-
values: [exp],
25-
})
22+
const literal = getLiteralExpressionValue(exp)
23+
if (literal != null) {
24+
context.childrenTemplate = [String(literal)]
25+
} else {
26+
context.registerEffect([exp], {
27+
type: IRNodeTypes.SET_TEXT,
28+
element: context.reference(),
29+
values: [exp],
30+
})
31+
}
2632
}

packages/compiler-vapor/src/utils.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { NumericLiteral, StringLiteral } from '@babel/types'
1+
import type { BigIntLiteral, NumericLiteral, StringLiteral } from '@babel/types'
22
import { isGloballyAllowed } from '@vue/shared'
33
import {
44
type AttributeNode,
@@ -55,13 +55,20 @@ export function resolveExpression(exp: SimpleExpressionNode) {
5555

5656
export function getLiteralExpressionValue(
5757
exp: SimpleExpressionNode,
58-
): number | string | null {
59-
if (
60-
!__BROWSER__ &&
61-
exp.ast &&
62-
['StringLiteral', 'NumericLiteral'].includes(exp.ast.type)
63-
) {
64-
return (exp.ast as StringLiteral | NumericLiteral).value
58+
): number | string | boolean | null {
59+
if (!__BROWSER__ && exp.ast) {
60+
if (
61+
['StringLiteral', 'NumericLiteral', 'BigIntLiteral'].includes(
62+
exp.ast.type,
63+
)
64+
) {
65+
return (exp.ast as StringLiteral | NumericLiteral | BigIntLiteral).value
66+
} else if (
67+
exp.ast.type === 'TemplateLiteral' &&
68+
exp.ast.expressions.length === 0
69+
) {
70+
return exp.ast.quasis[0].value.cooked!
71+
}
6572
}
6673
return exp.isStatic ? exp.content : null
6774
}

0 commit comments

Comments
 (0)