diff --git a/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts b/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts index 768ce03be..d8dfce788 100644 --- a/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts @@ -44,7 +44,9 @@ describe('compiler: v-for', () => { index: undefined, render: { type: IRNodeTypes.BLOCK, - templateIndex: 0, + dynamic: { + template: 0, + }, }, keyProperty: { type: NodeTypes.SIMPLE_EXPRESSION, diff --git a/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts b/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts index b0875db09..ead465ca8 100644 --- a/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts @@ -43,7 +43,9 @@ describe('compiler: v-if', () => { }, positive: { type: IRNodeTypes.BLOCK, - templateIndex: 0, + dynamic: { + template: 0, + }, }, }, ]) @@ -125,11 +127,15 @@ describe('compiler: v-if', () => { }, positive: { type: IRNodeTypes.BLOCK, - templateIndex: 0, + dynamic: { + template: 0, + }, }, negative: { type: IRNodeTypes.BLOCK, - templateIndex: 1, + dynamic: { + template: 1, + }, }, }, ]) @@ -154,7 +160,9 @@ describe('compiler: v-if', () => { }, positive: { type: IRNodeTypes.BLOCK, - templateIndex: 0, + dynamic: { + template: 0, + }, }, negative: { type: IRNodeTypes.IF, @@ -165,7 +173,9 @@ describe('compiler: v-if', () => { }, positive: { type: IRNodeTypes.BLOCK, - templateIndex: 1, + dynamic: { + template: 1, + }, }, }, }, @@ -187,17 +197,23 @@ describe('compiler: v-if', () => { id: 1, positive: { type: IRNodeTypes.BLOCK, - templateIndex: 0, + dynamic: { + template: 0, + }, }, negative: { type: IRNodeTypes.IF, positive: { type: IRNodeTypes.BLOCK, - templateIndex: 1, + dynamic: { + template: 1, + }, }, negative: { type: IRNodeTypes.BLOCK, - templateIndex: 2, + dynamic: { + template: 2, + }, }, }, }, diff --git a/packages/compiler-vapor/src/generators/block.ts b/packages/compiler-vapor/src/generators/block.ts index 0b7f0e2ee..2da1e5435 100644 --- a/packages/compiler-vapor/src/generators/block.ts +++ b/packages/compiler-vapor/src/generators/block.ts @@ -31,16 +31,13 @@ export function genBlockFunction( } export function genBlockFunctionContent( - { dynamic, effect, operation, templateIndex, returns }: BlockIRNode, + { dynamic, effect, operation, returns }: BlockIRNode, context: CodegenContext, customReturns?: (returns: CodeFragment[]) => CodeFragment[], ): CodeFragment[] { const [frag, push] = buildCodeFragment() - if (templateIndex > -1) { - push(NEWLINE, `const n${dynamic.id} = t${templateIndex}()`) - push(...genChildren(dynamic, context, dynamic.id!)) - } + push(...genChildren(dynamic, context, dynamic.id!)) const directiveOps = operation.filter( (oper): oper is WithDirectiveIRNode => diff --git a/packages/compiler-vapor/src/generators/template.ts b/packages/compiler-vapor/src/generators/template.ts index 77383ff1d..72ef647e3 100644 --- a/packages/compiler-vapor/src/generators/template.ts +++ b/packages/compiler-vapor/src/generators/template.ts @@ -23,7 +23,11 @@ export function genChildren( const { vaporHelper } = context const [frag, push] = buildCodeFragment() let offset = 0 - const { children } = dynamic + const { children, id, template } = dynamic + + if (id !== undefined && template !== undefined) { + push(NEWLINE, `const n${id} = t${template}()`) + } for (const [index, child] of children.entries()) { if (child.flags & DynamicFlag.NON_TEMPLATE) { @@ -36,11 +40,11 @@ export function genChildren( ? child.flags & DynamicFlag.INSERT ? child.anchor : child.id - : null + : undefined const newPaths = [...paths, elementIndex] - if (id !== null) { + if (id !== undefined) { push( NEWLINE, `const n${id} = `, diff --git a/packages/compiler-vapor/src/ir.ts b/packages/compiler-vapor/src/ir.ts index 70307cbef..9ca16c14c 100644 --- a/packages/compiler-vapor/src/ir.ts +++ b/packages/compiler-vapor/src/ir.ts @@ -45,7 +45,6 @@ export type VaporHelper = keyof typeof import('@vue/runtime-vapor') export interface BlockIRNode extends BaseIRNode { type: IRNodeTypes.BLOCK node: RootNode | TemplateChildNode - templateIndex: number dynamic: IRDynamicInfo effect: IREffect[] operation: OperationNode[] @@ -205,10 +204,11 @@ export enum DynamicFlag { } export interface IRDynamicInfo { - id: number | null + id?: number flags: DynamicFlag - anchor: number | null + anchor?: number children: IRDynamicInfo[] + template?: number } export interface IREffect { diff --git a/packages/compiler-vapor/src/transform.ts b/packages/compiler-vapor/src/transform.ts index 1aacc1ee3..fd3120167 100644 --- a/packages/compiler-vapor/src/transform.ts +++ b/packages/compiler-vapor/src/transform.ts @@ -139,7 +139,7 @@ function createRootContext( increaseId: () => globalId++, reference() { - if (this.dynamic.id !== null) return this.dynamic.id + if (this.dynamic.id !== undefined) return this.dynamic.id this.dynamic.flags |= DynamicFlag.REFERENCED return (this.dynamic.id = this.increaseId()) }, @@ -181,11 +181,11 @@ function createRootContext( template => template === this.template, ) if (existing !== -1) { - return (this.block.templateIndex = existing) + return (this.dynamic.template = existing) } root.template.push(this.template) - return (this.block.templateIndex = root.template.length - 1) + return (this.dynamic.template = root.template.length - 1) }, registerOperation(...node) { this.block.operation.push(...node) @@ -225,7 +225,6 @@ export function transform( block: { type: IRNodeTypes.BLOCK, node: root, - templateIndex: -1, dynamic: extend(genDefaultDynamic(), { flags: DynamicFlag.REFERENCED, } satisfies Partial), diff --git a/packages/compiler-vapor/src/transforms/utils.ts b/packages/compiler-vapor/src/transforms/utils.ts index d80dd65fd..e340f1bce 100644 --- a/packages/compiler-vapor/src/transforms/utils.ts +++ b/packages/compiler-vapor/src/transforms/utils.ts @@ -12,9 +12,7 @@ import { extend } from '@vue/shared' import { DynamicFlag, type IRDynamicInfo } from '../ir' export const genDefaultDynamic = (): IRDynamicInfo => ({ - id: null, flags: DynamicFlag.NONE, - anchor: null, children: [], }) diff --git a/packages/compiler-vapor/src/transforms/vFor.ts b/packages/compiler-vapor/src/transforms/vFor.ts index fab676a67..a750e8c4a 100644 --- a/packages/compiler-vapor/src/transforms/vFor.ts +++ b/packages/compiler-vapor/src/transforms/vFor.ts @@ -53,7 +53,6 @@ export function processFor( const render: BlockIRNode = { type: IRNodeTypes.BLOCK, node, - templateIndex: -1, dynamic: extend(genDefaultDynamic(), { flags: DynamicFlag.REFERENCED, } satisfies Partial), diff --git a/packages/compiler-vapor/src/transforms/vIf.ts b/packages/compiler-vapor/src/transforms/vIf.ts index 05ae0553d..71a417d8b 100644 --- a/packages/compiler-vapor/src/transforms/vIf.ts +++ b/packages/compiler-vapor/src/transforms/vIf.ts @@ -146,7 +146,6 @@ export function createIfBranch( const branch: BlockIRNode = { type: IRNodeTypes.BLOCK, node, - templateIndex: -1, dynamic: extend(genDefaultDynamic(), { flags: DynamicFlag.REFERENCED, } satisfies Partial),