Skip to content

Svelte 5: $host() assignment statement will be removed after compilation #13121

Closed
@CLDXiang

Description

@CLDXiang

Describe the bug

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

Reproduction

https://github.com/CLDXiang/svelte-custom-element-host-bug

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

Compilation output:

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

var element = $$props.$$host is missing.

Logs

No response

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i5-13600KF
    Memory: 16.67 GB / 31.85 GB
  Binaries:
    Node: 20.10.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.2.3 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.9.0 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    svelte: 5.0.0-next.243 => 5.0.0-next.243

Severity

annoyance

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions