svelte-custom-element-host-bug Svelte Themes

Svelte Custom Element Host Bug

Reproduce Svelte bug related to $host() rune

Reproduce Svelte $host() rune bug

This is a reproduction of the bug of Svelte's $host() rune:

If you assign $host() to a variable, the assignment statement will be removed after compilation.

source

MyElement.svelte

function fail(greeting: string) {
  const element = $host() as HTMLElement;
  element.dispatchEvent(
  new CustomEvent('greeting', { detail: greeting })
  );
}

after compilation

components.iife.js

function fail(greeting) {
  element.dispatchEvent(new CustomEvent("greeting", { detail: greeting }));
}

Which will throw ReferenceError: element is not defined in runtime.

Top categories

svelte logo

Need a Svelte website built?

Hire a professional Svelte developer today.
Loading Svelte Themes