From e2f84fc0754ca7defe5d68ce9d0419bb41b31169 Mon Sep 17 00:00:00 2001 From: daiwei Date: Sat, 26 Jul 2025 16:32:03 +0800 Subject: [PATCH] wip: save --- .../transforms/transformElement.spec.ts | 18 ++++++++----- .../transforms/transformSlotOutlet.spec.ts | 8 +++--- .../transforms/transformTemplateRef.spec.ts | 6 ++--- .../__tests__/transforms/vBind.spec.ts | 4 +-- .../__tests__/transforms/vFor.spec.ts | 4 +-- .../__tests__/transforms/vHtml.spec.ts | 2 +- .../__tests__/transforms/vIf.spec.ts | 18 ++++++++----- .../__tests__/transforms/vSlot.spec.ts | 4 +-- .../__tests__/transforms/vText.spec.ts | 2 +- .../compiler-vapor/src/generators/template.ts | 21 ++++++++------- packages/compiler-vapor/src/ir/index.ts | 4 +-- packages/compiler-vapor/src/transform.ts | 26 +++++++++---------- .../src/transforms/transformElement.ts | 2 +- 13 files changed, 64 insertions(+), 55 deletions(-) diff --git a/packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts b/packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts index 4c9e5c5c2e..43280f6061 100644 --- a/packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts @@ -579,7 +579,7 @@ describe('compiler: element transform', () => { const template = '
' expect(code).toMatchSnapshot() expect(code).contains(JSON.stringify(template)) - expect(ir.template).toMatchObject([template]) + expect([...ir.template.keys()]).toMatchObject([template]) expect(ir.block.effect).lengthOf(0) }) @@ -591,7 +591,7 @@ describe('compiler: element transform', () => { const template = '
' expect(code).toMatchSnapshot() expect(code).contains(JSON.stringify(template)) - expect(ir.template).toMatchObject([template]) + expect([...ir.template.keys()]).toMatchObject([template]) expect(ir.block.effect).lengthOf(0) }) @@ -937,7 +937,11 @@ describe('compiler: element transform', () => {
`, ) expect(code).toMatchSnapshot() - expect(ir.template).toEqual(['
123
', '

', '
']) + expect([...ir.template.keys()]).toEqual([ + '
123
', + '

', + '
', + ]) expect(ir.block.dynamic).toMatchObject({ children: [ { id: 1, template: 1, children: [{ id: 0, template: 0 }] }, @@ -964,8 +968,8 @@ describe('compiler: element transform', () => { expect(code).contains( '_template("", true, 1)', ) - expect(ir.template).toMatchObject([t]) - expect(ir.templateNS.get(t)).toBe(1) + expect([...ir.template.keys()]).toMatchObject([t]) + expect(ir.template.get(t)).toBe(1) }) test('MathML', () => { @@ -975,7 +979,7 @@ describe('compiler: element transform', () => { expect(code).contains( '_template("x", true, 2)', ) - expect(ir.template).toMatchObject([t]) - expect(ir.templateNS.get(t)).toBe(2) + expect([...ir.template.keys()]).toMatchObject([t]) + expect(ir.template.get(t)).toBe(2) }) }) diff --git a/packages/compiler-vapor/__tests__/transforms/transformSlotOutlet.spec.ts b/packages/compiler-vapor/__tests__/transforms/transformSlotOutlet.spec.ts index 389c665a12..99daa571f6 100644 --- a/packages/compiler-vapor/__tests__/transforms/transformSlotOutlet.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/transformSlotOutlet.spec.ts @@ -155,7 +155,7 @@ describe('compiler: transform outlets', () => { test('default slot outlet with fallback', () => { const { ir, code } = compileWithSlotsOutlet(`
`) expect(code).toMatchSnapshot() - expect(ir.template[0]).toBe('
') + expect([...ir.template.keys()][0]).toBe('
') expect(ir.block.dynamic.children[0].operation).toMatchObject({ type: IRNodeTypes.SLOT_OUTLET_NODE, id: 0, @@ -175,7 +175,7 @@ describe('compiler: transform outlets', () => { `
`, ) expect(code).toMatchSnapshot() - expect(ir.template[0]).toBe('
') + expect([...ir.template.keys()][0]).toBe('
') expect(ir.block.dynamic.children[0].operation).toMatchObject({ type: IRNodeTypes.SLOT_OUTLET_NODE, id: 0, @@ -195,7 +195,7 @@ describe('compiler: transform outlets', () => { `
`, ) expect(code).toMatchSnapshot() - expect(ir.template[0]).toBe('
') + expect([...ir.template.keys()][0]).toBe('
') expect(ir.block.dynamic.children[0].operation).toMatchObject({ type: IRNodeTypes.SLOT_OUTLET_NODE, id: 0, @@ -216,7 +216,7 @@ describe('compiler: transform outlets', () => { `
`, ) expect(code).toMatchSnapshot() - expect(ir.template[0]).toBe('
') + expect([...ir.template.keys()][0]).toBe('
') expect(ir.block.dynamic.children[0].operation).toMatchObject({ type: IRNodeTypes.SLOT_OUTLET_NODE, id: 0, diff --git a/packages/compiler-vapor/__tests__/transforms/transformTemplateRef.spec.ts b/packages/compiler-vapor/__tests__/transforms/transformTemplateRef.spec.ts index 2c883d10cc..82d07ce0ab 100644 --- a/packages/compiler-vapor/__tests__/transforms/transformTemplateRef.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/transformTemplateRef.spec.ts @@ -30,7 +30,7 @@ describe('compiler: template ref transform', () => { id: 0, flags: DynamicFlag.REFERENCED, }) - expect(ir.template).toEqual(['
']) + expect([...ir.template.keys()]).toEqual(['
']) expect(ir.block.operation).lengthOf(1) expect(ir.block.operation[0]).toMatchObject({ type: IRNodeTypes.SET_TEMPLATE_REF, @@ -66,7 +66,7 @@ describe('compiler: template ref transform', () => { id: 0, flags: DynamicFlag.REFERENCED, }) - expect(ir.template).toEqual(['
']) + expect([...ir.template.keys()]).toEqual(['
']) expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.DECLARE_OLD_REF, @@ -104,7 +104,7 @@ describe('compiler: template ref transform', () => { id: 0, flags: DynamicFlag.REFERENCED, }) - expect(ir.template).toEqual(['
']) + expect([...ir.template.keys()]).toEqual(['
']) expect(ir.block.operation).toMatchObject([ { type: IRNodeTypes.DECLARE_OLD_REF, diff --git a/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts b/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts index a36a76d838..60cd9d986e 100644 --- a/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vBind.spec.ts @@ -23,7 +23,7 @@ describe('compiler v-bind', () => { id: 0, flags: DynamicFlag.REFERENCED, }) - expect(ir.template).toEqual(['
']) + expect([...ir.template.keys()]).toEqual(['
']) expect(ir.block.effect).lengthOf(1) expect(ir.block.effect[0].expressions).lengthOf(1) expect(ir.block.effect[0].operations).lengthOf(1) @@ -241,7 +241,7 @@ describe('compiler v-bind', () => { end: { line: 1, column: 19 }, }, }) - expect(ir.template).toEqual(['
']) + expect([...ir.template.keys()]).toEqual(['
']) expect(code).matchSnapshot() expect(code).contains(JSON.stringify('
')) diff --git a/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts b/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts index 7357ad84fe..011eb0ca92 100644 --- a/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vFor.spec.ts @@ -32,7 +32,7 @@ describe('compiler: v-for', () => { expect(code).matchSnapshot() expect(helpers).contains('createFor') - expect(ir.template).toEqual(['
']) + expect([...ir.template.keys()]).toEqual(['
']) expect(ir.block.dynamic.children[0].operation).toMatchObject({ type: IRNodeTypes.FOR, id: 0, @@ -156,7 +156,7 @@ describe('compiler: v-for', () => { `_createFor(() => (_for_item0.value), (_for_item1) => {`, ) expect(code).contains(`_for_item1.value+_for_item0.value`) - expect(ir.template).toEqual([' ', '
']) + expect([...ir.template.keys()]).toEqual([' ', '
']) const parentOp = ir.block.dynamic.children[0].operation expect(parentOp).toMatchObject({ type: IRNodeTypes.FOR, diff --git a/packages/compiler-vapor/__tests__/transforms/vHtml.spec.ts b/packages/compiler-vapor/__tests__/transforms/vHtml.spec.ts index 0de0b6abca..f297362b2c 100644 --- a/packages/compiler-vapor/__tests__/transforms/vHtml.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vHtml.spec.ts @@ -66,7 +66,7 @@ describe('v-html', () => { expect(helpers).contains('setHtml') // children should have been removed - expect(ir.template).toEqual(['
']) + expect([...ir.template.keys()]).toEqual(['
']) expect(ir.block.operation).toMatchObject([]) expect(ir.block.effect).toMatchObject([ diff --git a/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts b/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts index e5fd61add2..76a1bde612 100644 --- a/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vIf.spec.ts @@ -32,7 +32,7 @@ describe('compiler: v-if', () => { expect(helpers).contains('createIf') - expect(ir.template).toEqual(['
']) + expect([...ir.template.keys()]).toEqual(['
']) const op = ir.block.dynamic.children[0].operation expect(op).toMatchObject({ @@ -68,7 +68,11 @@ describe('compiler: v-if', () => { ) expect(code).matchSnapshot() - expect(ir.template).toEqual(['
', 'hello', '

']) + expect([...ir.template.keys()]).toEqual([ + '
', + 'hello', + '

', + ]) expect(ir.block.effect).toEqual([]) const op = ir.block.dynamic.children[0].operation as IfIRNode expect(op.positive.effect).toMatchObject([ @@ -103,7 +107,7 @@ describe('compiler: v-if', () => { `
hello
hello
`, ) expect(code).matchSnapshot() - expect(ir.template).toEqual(['
hello
']) + expect([...ir.template.keys()]).toEqual(['
hello
']) expect(ir.block.returns).toEqual([0, 3]) }) @@ -113,7 +117,7 @@ describe('compiler: v-if', () => { test('v-if + v-else', () => { const { code, ir, helpers } = compileWithVIf(`

`) expect(code).matchSnapshot() - expect(ir.template).toEqual(['

', '

']) + expect([...ir.template.keys()]).toEqual(['
', '

']) expect(helpers).contains('createIf') expect(ir.block.effect).lengthOf(0) @@ -146,7 +150,7 @@ describe('compiler: v-if', () => { `

`, ) expect(code).matchSnapshot() - expect(ir.template).toEqual(['

', '

']) + expect([...ir.template.keys()]).toEqual(['
', '

']) expect(ir.block.dynamic.children[0].operation).toMatchObject({ type: IRNodeTypes.IF, @@ -185,7 +189,7 @@ describe('compiler: v-if', () => { `

`, ) expect(code).matchSnapshot() - expect(ir.template).toEqual(['

', '

', 'fine']) + expect([...ir.template.keys()]).toEqual(['
', '

', 'fine']) expect(ir.block.returns).toEqual([0]) expect(ir.block.dynamic.children[0].operation).toMatchObject({ @@ -236,7 +240,7 @@ describe('compiler: v-if', () => {
`) expect(code).matchSnapshot() - expect(ir.template).toEqual([ + expect([...ir.template.keys()]).toEqual([ '
', '', '

', diff --git a/packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts b/packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts index 909162fe3c..2a462479c3 100644 --- a/packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts @@ -35,7 +35,7 @@ describe('compiler: transform slot', () => { const { ir, code } = compileWithSlots(`
`) expect(code).toMatchSnapshot() - expect(ir.template).toEqual(['
']) + expect([...ir.template.keys()]).toEqual(['
']) expect(ir.block.dynamic.children[0].operation).toMatchObject({ type: IRNodeTypes.CREATE_COMPONENT_NODE, id: 1, @@ -163,7 +163,7 @@ describe('compiler: transform slot', () => { ) expect(code).toMatchSnapshot() - expect(ir.template).toEqual(['foo', 'bar', '']) + expect([...ir.template.keys()]).toEqual(['foo', 'bar', '']) expect(ir.block.dynamic.children[0].operation).toMatchObject({ type: IRNodeTypes.CREATE_COMPONENT_NODE, id: 4, diff --git a/packages/compiler-vapor/__tests__/transforms/vText.spec.ts b/packages/compiler-vapor/__tests__/transforms/vText.spec.ts index 4f074fee87..7ebac8554a 100644 --- a/packages/compiler-vapor/__tests__/transforms/vText.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vText.spec.ts @@ -68,7 +68,7 @@ describe('v-text', () => { ]) // children should have been removed - expect(ir.template).toEqual(['
']) + expect([...ir.template.keys()]).toEqual(['
']) expect(ir.block.effect).toMatchObject([ { diff --git a/packages/compiler-vapor/src/generators/template.ts b/packages/compiler-vapor/src/generators/template.ts index 3b8886d8b9..e3cca1b48b 100644 --- a/packages/compiler-vapor/src/generators/template.ts +++ b/packages/compiler-vapor/src/generators/template.ts @@ -5,18 +5,21 @@ import { genOperationWithInsertionState } from './operation' import { type CodeFragment, NEWLINE, buildCodeFragment, genCall } from './utils' export function genTemplates( - templates: string[], + templates: Map, rootIndex: number | undefined, - { helper, ir: { templateNS } }: CodegenContext, + { helper }: CodegenContext, ): string { - return templates - .map((template, i) => { - const ns = templateNS.get(template) - return `const t${i} = ${helper('template')}(${JSON.stringify( + const result: string[] = [] + let i = 0 + templates.forEach((ns, template) => { + result.push( + `const t${i} = ${helper('template')}(${JSON.stringify( template, - )}${i === rootIndex ? ', true' : ns ? ', false' : ''}${ns ? `, ${ns}` : ''})\n` - }) - .join('') + )}${i === rootIndex ? ', true' : ns ? ', false' : ''}${ns ? `, ${ns}` : ''})\n`, + ) + i++ + }) + return result.join('') } export function genSelf( diff --git a/packages/compiler-vapor/src/ir/index.ts b/packages/compiler-vapor/src/ir/index.ts index b53c2ae6ad..b7c67eac92 100644 --- a/packages/compiler-vapor/src/ir/index.ts +++ b/packages/compiler-vapor/src/ir/index.ts @@ -59,8 +59,8 @@ export interface RootIRNode { type: IRNodeTypes.ROOT node: RootNode source: string - template: string[] - templateNS: Map + template: Map + templateIndexMap: Map rootTemplateIndex?: number component: Set directive: Set diff --git a/packages/compiler-vapor/src/transform.ts b/packages/compiler-vapor/src/transform.ts index bce40a46c3..e993daf4b0 100644 --- a/packages/compiler-vapor/src/transform.ts +++ b/packages/compiler-vapor/src/transform.ts @@ -74,7 +74,6 @@ export class TransformContext { > template: string = '' - templateNS: Map = new Map() childrenTemplate: (string | null)[] = [] dynamic: IRDynamicInfo = this.ir.block.dynamic @@ -100,12 +99,10 @@ export class TransformContext { } enterBlock(ir: BlockIRNode, isVFor: boolean = false): () => void { - const { block, template, templateNS, dynamic, childrenTemplate, slots } = - this + const { block, template, dynamic, childrenTemplate, slots } = this this.block = ir this.dynamic = ir.dynamic this.template = '' - this.templateNS = new Map() this.childrenTemplate = [] this.slots = [] isVFor && this.inVFor++ @@ -114,7 +111,6 @@ export class TransformContext { this.registerTemplate() this.block = block this.template = template - this.templateNS = templateNS this.dynamic = dynamic this.childrenTemplate = childrenTemplate this.slots = slots @@ -130,13 +126,15 @@ export class TransformContext { } pushTemplate(content: string): number { - const existing = this.ir.template.findIndex( - template => template === content, - ) - if (existing !== -1) return existing - this.ir.template.push(content) - this.ir.templateNS.set(content, (this.node as PlainElementNode).ns) - return this.ir.template.length - 1 + const existingIndex = this.ir.templateIndexMap.get(content) + if (existingIndex !== undefined) { + return existingIndex + } + + const newIndex = this.ir.template.size + this.ir.template.set(content, (this.node as PlainElementNode).ns) + this.ir.templateIndexMap.set(content, newIndex) + return newIndex } registerTemplate(): number { if (!this.template) return -1 @@ -220,8 +218,8 @@ export function transform( type: IRNodeTypes.ROOT, node, source: node.source, - template: [], - templateNS: new Map(), + template: new Map(), + templateIndexMap: new Map(), component: new Set(), directive: new Set(), block: newBlock(node), diff --git a/packages/compiler-vapor/src/transforms/transformElement.ts b/packages/compiler-vapor/src/transforms/transformElement.ts index 05153e729a..1919ccd2a2 100644 --- a/packages/compiler-vapor/src/transforms/transformElement.ts +++ b/packages/compiler-vapor/src/transforms/transformElement.ts @@ -250,7 +250,7 @@ function transformNativeElement( } if (singleRoot) { - context.ir.rootTemplateIndex = context.ir.template.length + context.ir.rootTemplateIndex = context.ir.template.size } if ( -- 2.47.2