Skip to content

Commit 9b3429e

Browse files
committed
perf(runtime-vapor): tree-shake unused suspense paths
1 parent 31477e7 commit 9b3429e

4 files changed

Lines changed: 66 additions & 25 deletions

File tree

packages/runtime-vapor/src/component.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ import type {
122122
} from './apiDefineComponent'
123123
import { DynamicFragment, isFragment } from './fragment'
124124
import type { VaporElement } from './apiDefineCustomElement'
125-
import { parentSuspense, setParentSuspense } from './components/Suspense'
125+
import {
126+
isSuspenseEnabled,
127+
parentSuspense,
128+
setParentSuspense,
129+
} from './suspense'
126130
import { isInteropEnabled } from './vdomInteropState'
127131
import { setComponentScopeId, setScopeId } from './scopeId'
128132
import { isTransitionEnabled } from './transition'
@@ -276,7 +280,12 @@ export function createComponent(
276280

277281
try {
278282
let prevSuspense: SuspenseBoundary | null = null
279-
if (__FEATURE_SUSPENSE__ && currentInstance && currentInstance.suspense) {
283+
if (
284+
__FEATURE_SUSPENSE__ &&
285+
isSuspenseEnabled &&
286+
currentInstance &&
287+
currentInstance.suspense
288+
) {
280289
prevSuspense = setParentSuspense(currentInstance.suspense)
281290
}
282291

@@ -409,7 +418,12 @@ export function createComponent(
409418
endMeasure(instance, 'init')
410419
}
411420

412-
if (__FEATURE_SUSPENSE__ && currentInstance && currentInstance.suspense) {
421+
if (
422+
__FEATURE_SUSPENSE__ &&
423+
isSuspenseEnabled &&
424+
currentInstance &&
425+
currentInstance.suspense
426+
) {
413427
setParentSuspense(prevSuspense)
414428
}
415429

@@ -426,6 +440,8 @@ export function createComponent(
426440
}
427441

428442
if (
443+
__FEATURE_SUSPENSE__ &&
444+
isSuspenseEnabled &&
429445
isHydrating &&
430446
hydrationClose &&
431447
instance.suspense &&
@@ -741,8 +757,12 @@ export class VaporComponentInstance<
741757
this.emitted = this.exposed = this.exposeProxy = this.propsDefaults = null
742758

743759
// suspense related
744-
this.suspense = parentSuspense
745-
this.suspenseId = parentSuspense ? parentSuspense.pendingId : 0
760+
this.suspense = null
761+
this.suspenseId = 0
762+
if (__FEATURE_SUSPENSE__ && isSuspenseEnabled) {
763+
this.suspense = parentSuspense
764+
this.suspenseId = parentSuspense ? parentSuspense.pendingId : 0
765+
}
746766
this.asyncDep = null
747767
this.asyncResolved = false
748768

@@ -957,6 +977,7 @@ export function mountComponent(
957977
): void {
958978
if (
959979
__FEATURE_SUSPENSE__ &&
980+
isSuspenseEnabled &&
960981
instance.suspense &&
961982
instance.asyncDep &&
962983
!instance.asyncResolved
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
import type { SuspenseBoundary } from '@vue/runtime-dom'
2-
3-
export let parentSuspense: SuspenseBoundary | null = null
4-
5-
export function setParentSuspense(
6-
suspense: SuspenseBoundary | null,
7-
): SuspenseBoundary | null {
8-
try {
9-
return parentSuspense
10-
} finally {
11-
parentSuspense = suspense
12-
}
13-
}
1+
import { withSuspenseEnabled } from '../suspense'
142

153
// TODO: implement this
16-
export const VaporSuspenseImpl = {
4+
export const VaporSuspenseImpl: {
5+
name: string
6+
__isSuspense: true
7+
process(): void
8+
} = /*@__PURE__*/ withSuspenseEnabled({
179
name: 'VaporSuspense',
1810
__isSuspense: true,
1911
process(): void {},
20-
}
12+
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { SuspenseBoundary } from '@vue/runtime-dom'
2+
3+
export let isSuspenseEnabled = false
4+
export let parentSuspense: SuspenseBoundary | null = null
5+
6+
export function enableSuspense(): void {
7+
isSuspenseEnabled = true
8+
}
9+
10+
export function withSuspenseEnabled<T>(value: T): T {
11+
enableSuspense()
12+
return value
13+
}
14+
15+
export function setParentSuspense(
16+
suspense: SuspenseBoundary | null,
17+
): SuspenseBoundary | null {
18+
try {
19+
return parentSuspense
20+
} finally {
21+
parentSuspense = suspense
22+
}
23+
}

packages/runtime-vapor/src/vdomInterop.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ import {
125125
} from './components/KeepAlive'
126126
import {
127127
parentSuspense as currentParentSuspense,
128+
enableSuspense,
129+
isSuspenseEnabled,
128130
setParentSuspense,
129-
} from './components/Suspense'
131+
} from './suspense'
130132

131133
export const interopKey: unique symbol = Symbol(`interop`)
132134

@@ -169,7 +171,7 @@ const vaporInteropImpl: Omit<
169171
const slotsRef = shallowRef(vnode.children)
170172

171173
let prevSuspense: SuspenseBoundary | null = null
172-
if (__FEATURE_SUSPENSE__ && parentSuspense) {
174+
if (__FEATURE_SUSPENSE__ && isSuspenseEnabled && parentSuspense) {
173175
prevSuspense = setParentSuspense(parentSuspense)
174176
}
175177

@@ -209,7 +211,7 @@ const vaporInteropImpl: Omit<
209211
)
210212
}
211213

212-
if (__FEATURE_SUSPENSE__ && parentSuspense) {
214+
if (__FEATURE_SUSPENSE__ && isSuspenseEnabled && parentSuspense) {
213215
setParentSuspense(prevSuspense)
214216
}
215217

@@ -1426,6 +1428,9 @@ function shouldUseCurrentParent(block: Block): boolean {
14261428
}
14271429

14281430
export const vaporInteropPlugin: Plugin = app => {
1431+
if (__FEATURE_SUSPENSE__) {
1432+
enableSuspense()
1433+
}
14291434
setInteropEnabled()
14301435
const internals = ensureRenderer().internals
14311436
app._context.vapor = extend(vaporInteropImpl, {
@@ -1587,7 +1592,7 @@ function renderVaporSlot(
15871592
const prev = currentInstance
15881593
let prevSuspense: SuspenseBoundary | null = null
15891594
simpleSetCurrentInstance(parentComponent)
1590-
if (__FEATURE_SUSPENSE__ && parentSuspense) {
1595+
if (__FEATURE_SUSPENSE__ && isSuspenseEnabled && parentSuspense) {
15911596
prevSuspense = setParentSuspense(parentSuspense)
15921597
}
15931598
try {
@@ -1761,7 +1766,7 @@ function renderVaporSlot(
17611766
throw e
17621767
}
17631768
} finally {
1764-
if (__FEATURE_SUSPENSE__ && parentSuspense) {
1769+
if (__FEATURE_SUSPENSE__ && isSuspenseEnabled && parentSuspense) {
17651770
setParentSuspense(prevSuspense)
17661771
}
17671772
simpleSetCurrentInstance(prev)

0 commit comments

Comments
 (0)