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

Commit 30f6eec

Browse files
committed
refactor(runtime-vapor): re-organize block
1 parent 59975ed commit 30f6eec

File tree

9 files changed

+61
-63
lines changed

9 files changed

+61
-63
lines changed

packages/runtime-vapor/src/apiCreateComponent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import { type RawSlots, isDynamicSlotFn } from './componentSlots'
1515
import { withAttrs } from './componentAttrs'
1616
import { isString } from '@vue/shared'
1717
import { renderEffect } from './renderEffect'
18-
import { normalizeBlock } from './dom/element'
1918
import { setClass, setDynamicProp } from './dom/prop'
2019
import { setStyle } from './dom/style'
20+
import { normalizeBlock } from './block'
2121

2222
export function createComponent(
2323
comp: Component | string,

packages/runtime-vapor/src/apiCreateFor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
insert,
1212
remove as removeBlock,
1313
} from './dom/element'
14-
import { type Block, type Fragment, fragmentKey } from './apiRender'
14+
import { type Block, type Fragment, fragmentKey } from './block'
1515
import { warn } from './warning'
1616
import { currentInstance } from './component'
1717
import { componentKey } from './component'

packages/runtime-vapor/src/apiCreateIf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { renderEffect } from './renderEffect'
2-
import { type Block, type Fragment, fragmentKey } from './apiRender'
2+
import { type Block, type Fragment, fragmentKey } from './block'
33
import { type EffectScope, effectScope, shallowReactive } from '@vue/reactivity'
44
import { createComment, createTextNode, insert, remove } from './dom/element'
55

packages/runtime-vapor/src/apiRender.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,7 @@ import { VaporErrorCodes, callWithErrorHandling } from './errorHandling'
2020
import { endMeasure, startMeasure } from './profiling'
2121
import { devtoolsComponentAdded } from './devtools'
2222
import { fallThroughAttrs } from './componentAttrs'
23-
24-
export const fragmentKey: unique symbol = Symbol(__DEV__ ? `fragmentKey` : ``)
25-
26-
export type Block = Node | Fragment | ComponentInternalInstance | Block[]
27-
export type Fragment = {
28-
nodes: Block
29-
anchor?: Node
30-
[fragmentKey]: true
31-
}
23+
import { type Block, findFirstRootElement, fragmentKey } from './block'
3224

3325
export function setupComponent(instance: ComponentInternalInstance): void {
3426
if (__DEV__) {
@@ -176,20 +168,3 @@ export function unmountComponent(instance: ComponentInternalInstance): void {
176168
)
177169
flushPostFlushCbs()
178170
}
179-
180-
function findFirstRootElement(instance: ComponentInternalInstance) {
181-
const element = getFirstNode(instance.block)
182-
return element instanceof Element ? element : undefined
183-
}
184-
185-
function getFirstNode(block: Block | null): Node | undefined {
186-
if (!block || componentKey in block) return
187-
if (block instanceof Node) return block
188-
if (isArray(block)) {
189-
if (block.length === 1) {
190-
return getFirstNode(block[0])
191-
}
192-
} else {
193-
return getFirstNode(block.nodes)
194-
}
195-
}

packages/runtime-vapor/src/block.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { isArray } from '@vue/shared'
2+
import { type ComponentInternalInstance, componentKey } from './component'
3+
4+
export const fragmentKey: unique symbol = Symbol(__DEV__ ? `fragmentKey` : ``)
5+
6+
export type Block = Node | Fragment | ComponentInternalInstance | Block[]
7+
export type Fragment = {
8+
nodes: Block
9+
anchor?: Node
10+
[fragmentKey]: true
11+
}
12+
13+
/*! #__NO_SIDE_EFFECTS__ */
14+
export function normalizeBlock(block: Block): Node[] {
15+
const nodes: Node[] = []
16+
if (block instanceof Node) {
17+
nodes.push(block)
18+
} else if (isArray(block)) {
19+
block.forEach(child => nodes.push(...normalizeBlock(child)))
20+
} else if (componentKey in block) {
21+
nodes.push(...normalizeBlock(block.block!))
22+
} else if (block) {
23+
nodes.push(...normalizeBlock(block.nodes))
24+
block.anchor && nodes.push(block.anchor)
25+
}
26+
return nodes
27+
}
28+
29+
export function findFirstRootElement(
30+
instance: ComponentInternalInstance,
31+
): Element | undefined {
32+
const element = getFirstNode(instance.block)
33+
return element instanceof Element ? element : undefined
34+
}
35+
36+
export function getFirstNode(block: Block | null): Node | undefined {
37+
if (!block || componentKey in block) return
38+
if (block instanceof Node) return block
39+
if (isArray(block)) {
40+
if (block.length === 1) {
41+
return getFirstNode(block[0])
42+
}
43+
} else {
44+
return getFirstNode(block.nodes)
45+
}
46+
}
47+
48+
export function isValidBlock(block: Block): boolean {
49+
return (
50+
normalizeBlock(block).filter(node => !(node instanceof Comment)).length > 0
51+
)
52+
}

packages/runtime-vapor/src/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
isBuiltInTag,
77
isFunction,
88
} from '@vue/shared'
9-
import type { Block } from './apiRender'
9+
import type { Block } from './block'
1010
import {
1111
type ComponentPropsOptions,
1212
type NormalizedPropsOptions,

packages/runtime-vapor/src/componentSlots.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@ import {
1111
currentInstance,
1212
setCurrentInstance,
1313
} from './component'
14-
import { type Block, type Fragment, fragmentKey } from './apiRender'
14+
import { type Block, type Fragment, fragmentKey, isValidBlock } from './block'
1515
import { firstEffect, renderEffect } from './renderEffect'
16-
import {
17-
createComment,
18-
createTextNode,
19-
insert,
20-
normalizeBlock,
21-
remove,
22-
} from './dom/element'
16+
import { createComment, createTextNode, insert, remove } from './dom/element'
2317
import type { NormalizedRawProps } from './componentProps'
2418
import type { Data } from '@vue/runtime-shared'
2519
import { mergeProps } from './dom/prop'
@@ -271,9 +265,3 @@ function normalizeSlotProps(rawPropsList: NormalizedRawProps) {
271265
}
272266
}
273267
}
274-
275-
function isValidBlock(block: Block) {
276-
return (
277-
normalizeBlock(block).filter(node => !(node instanceof Comment)).length > 0
278-
)
279-
}

packages/runtime-vapor/src/directives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
isVaporComponent,
66
} from './component'
77
import { warn } from './warning'
8-
import { normalizeBlock } from './dom/element'
8+
import { normalizeBlock } from './block'
99
import { getCurrentScope } from '@vue/reactivity'
1010
import { VaporErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
1111

packages/runtime-vapor/src/dom/element.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
import { isArray } from '@vue/shared'
2-
import type { Block } from '../apiRender'
3-
import { componentKey } from '../component'
42
import { renderEffect } from '../renderEffect'
53
import { setText } from './prop'
6-
7-
/*! #__NO_SIDE_EFFECTS__ */
8-
export function normalizeBlock(block: Block): Node[] {
9-
const nodes: Node[] = []
10-
if (block instanceof Node) {
11-
nodes.push(block)
12-
} else if (isArray(block)) {
13-
block.forEach(child => nodes.push(...normalizeBlock(child)))
14-
} else if (componentKey in block) {
15-
nodes.push(...normalizeBlock(block.block!))
16-
} else if (block) {
17-
nodes.push(...normalizeBlock(block.nodes))
18-
block.anchor && nodes.push(block.anchor)
19-
}
20-
return nodes
21-
}
4+
import { type Block, normalizeBlock } from '../block'
225

236
export function insert(
247
block: Block,

0 commit comments

Comments
 (0)