summaryrefslogtreecommitdiff
path: root/src/frontend/render/svelte.ts
blob: ffdf70254ef8f5882e2ee73cb96869a292bc6818 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { SvelteComponent as Component } from 'svelte';

export function __svelte_static(SvelteComponent: Component) {
  return (attrs: Record<string, any>, ...children: any): string => {
    // TODO include head and css stuff too...
    const { html } = SvelteComponent.render(attrs);
  
    return html;
  };
}

export function __svelte_dynamic(SvelteComponent: Component, importUrl: string) {
  const placeholderId = `placeholder_${String(Math.random())}`;
  return (attrs: Record<string, string>, ...children: any) => {
      return `<div id="${placeholderId}"></div><script type="module">
          import Component from '${importUrl}';

          new Component({
            target: document.getElementById('${placeholderId}'),
            props: ${JSON.stringify(attrs)}
          });
      </script>`;
  };
}