From 7fa4403692bae983317e92194dc7b56d9b409a07 Mon Sep 17 00:00:00 2001 From: zhiyuanzmj <260480378@qq.com> Date: Tue, 3 Jun 2025 11:56:30 +0800 Subject: [PATCH] feat(runtime-vapor): support Fragment --- packages/runtime-core/src/errorHandling.ts | 2 ++ packages/runtime-vapor/__tests__/component.spec.ts | 13 +++++++++++++ packages/runtime-vapor/src/component.ts | 14 +++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/errorHandling.ts b/packages/runtime-core/src/errorHandling.ts index f8048c5c0e7..89a704c7784 100644 --- a/packages/runtime-core/src/errorHandling.ts +++ b/packages/runtime-core/src/errorHandling.ts @@ -28,6 +28,7 @@ export enum ErrorCodes { SCHEDULER, COMPONENT_UPDATE, APP_UNMOUNT_CLEANUP, + RENDER_SLOTS, } export const ErrorTypeStrings: Record = { @@ -62,6 +63,7 @@ export const ErrorTypeStrings: Record = { [ErrorCodes.SCHEDULER]: 'scheduler flush', [ErrorCodes.COMPONENT_UPDATE]: 'component update', [ErrorCodes.APP_UNMOUNT_CLEANUP]: 'app unmount cleanup function', + [ErrorCodes.RENDER_SLOTS]: 'render slots', } export type ErrorTypes = LifecycleHooks | ErrorCodes | WatchErrorCodes diff --git a/packages/runtime-vapor/__tests__/component.spec.ts b/packages/runtime-vapor/__tests__/component.spec.ts index 5fdff8eafe4..fa2bfadf61c 100644 --- a/packages/runtime-vapor/__tests__/component.spec.ts +++ b/packages/runtime-vapor/__tests__/component.spec.ts @@ -1,4 +1,5 @@ import { + Fragment, type Ref, inject, nextTick, @@ -327,4 +328,16 @@ describe('component', () => { 'Vapor component setup() returned non-block value, and has no render function', ).toHaveBeenWarned() }) + + it('Fragment slots should be rendered', () => { + const { host } = define({ + setup() { + return createComponent(Fragment as any, null, { + default: () => template('HI', true)(), + }) + }, + }).render() + + expect(host.innerHTML).toBe('HI') + }) }) diff --git a/packages/runtime-vapor/src/component.ts b/packages/runtime-vapor/src/component.ts index 548babebf8b..16b90b7fea8 100644 --- a/packages/runtime-vapor/src/component.ts +++ b/packages/runtime-vapor/src/component.ts @@ -5,6 +5,7 @@ import { type EmitFn, type EmitsOptions, ErrorCodes, + Fragment, type GenericAppContext, type GenericComponentInstance, type LifecycleHook, @@ -207,7 +208,18 @@ export function createComponent( ]) || EMPTY_OBJ : EMPTY_OBJ - if (__DEV__ && !isBlock(setupResult)) { + if (component === Fragment && rawSlots) { + const defaultSlot = getSlot(rawSlots as RawSlots, 'default') + if (defaultSlot) { + instance.block = callWithErrorHandling( + defaultSlot, + instance, + ErrorCodes.RENDER_SLOTS, + ) + } else { + instance.block = [] + } + } else if (__DEV__ && !isBlock(setupResult)) { if (isFunction(component)) { warn(`Functional vapor component must return a block directly.`) instance.block = []