From 6bf77a645166720e9050db699a29943faef6c632 Mon Sep 17 00:00:00 2001 From: Yuichiro Yamashita Date: Sat, 21 May 2022 12:43:23 +0900 Subject: [PATCH] improve error message if this attribute of is not SvelteComponent --- .../compile/render_dom/wrappers/InlineComponent/index.ts | 2 ++ src/runtime/internal/dev.ts | 6 ++++++ src/runtime/internal/ssr.ts | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts b/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts index e6eab33fbd..7523d79e41 100644 --- a/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts +++ b/src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts @@ -416,6 +416,7 @@ export default class InlineComponentWrapper extends Wrapper { } if (${switch_value}) { + ${component.compile_options.dev && b`@validate_svelte_component(${switch_value});`} ${name} = new ${switch_value}(${switch_props}(#ctx)); ${munged_bindings} @@ -460,6 +461,7 @@ export default class InlineComponentWrapper extends Wrapper { } if (${switch_value}) { + ${component.compile_options.dev && b`@validate_svelte_component(${switch_value});`} ${name} = new ${switch_value}(${switch_props}(#ctx)); ${munged_bindings} diff --git a/src/runtime/internal/dev.ts b/src/runtime/internal/dev.ts index 3144911672..502afd88d6 100644 --- a/src/runtime/internal/dev.ts +++ b/src/runtime/internal/dev.ts @@ -121,6 +121,12 @@ export function validate_void_dynamic_element(tag: undefined | string) { } } +export function validate_svelte_component(component: any) { + if (!(component.prototype instanceof SvelteComponent)) { + throw new Error('this={...} of should specify a Svelte component.'); + } +} + type Props = Record; export interface SvelteComponentDev { $set(props?: Props): void; diff --git a/src/runtime/internal/ssr.ts b/src/runtime/internal/ssr.ts index 2c7d24a2cd..c51c2827df 100644 --- a/src/runtime/internal/ssr.ts +++ b/src/runtime/internal/ssr.ts @@ -122,7 +122,7 @@ export const missing_component = { export function validate_component(component, name) { if (!component || !component.$$render) { if (name === 'svelte:component') name += ' this={...}'; - throw new Error(`<${name}> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules`); + throw new Error(`<${name}> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules. Otherwise you may need to fix a <${name}>.`); } return component;