From 568e8242004a66a49db667ff07566c68caa8175e Mon Sep 17 00:00:00 2001 From: edison Date: Wed, 29 Jan 2025 12:30:00 +0800 Subject: [PATCH] fix(runtime-vapor): properly mount component only with template in production mode (#12727) Co-authored-by: Evan You --- .../runtime-vapor/__tests__/component.spec.ts | 18 ++++++++++++++++++ packages/runtime-vapor/src/component.ts | 14 ++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/runtime-vapor/__tests__/component.spec.ts b/packages/runtime-vapor/__tests__/component.spec.ts index a4af5be7ab..c9f3a5ffd6 100644 --- a/packages/runtime-vapor/__tests__/component.spec.ts +++ b/packages/runtime-vapor/__tests__/component.spec.ts @@ -282,6 +282,24 @@ describe('component', () => { expect(i.scope.effects.length).toBe(0) }) + test('should mount component only with template in production mode', () => { + __DEV__ = false + const { component: Child } = define({ + render() { + return template('
HI
', true)() + }, + }) + + const { host } = define({ + setup() { + return createComponent(Child, null, null, true) + }, + }).render() + + expect(host.innerHTML).toBe('
HI
') + __DEV__ = true + }) + it('warn if functional vapor component not return a block', () => { define(() => { return () => {} diff --git a/packages/runtime-vapor/src/component.ts b/packages/runtime-vapor/src/component.ts index abe72e79cf..3e5d4fc2d2 100644 --- a/packages/runtime-vapor/src/component.ts +++ b/packages/runtime-vapor/src/component.ts @@ -212,8 +212,18 @@ export function createComponent( } } } else { - // in prod result can only be block - instance.block = setupResult as Block + // component has a render function but no setup function + // (typically components with only a template and no state) + if (!setupFn && component.render) { + instance.block = callWithErrorHandling( + component.render, + instance, + ErrorCodes.RENDER_FUNCTION, + ) + } else { + // in prod result can only be block + instance.block = setupResult as Block + } } // single root, inherit attrs -- 2.47.2