summaryrefslogtreecommitdiff
path: root/src/frontend/render/vue.ts
blob: f83c1665a247b8500bb8ef68cc10977deb4a0525 (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import type { Component } from 'vue';

import { renderToString } from '@vue/server-renderer';
import { createSSRApp, h as createElement } from 'vue';

export function __vue_static(VueComponent: Component) {
  return async (attrs: Record<string, any>, ...children: any): Promise<string> => {
    const app = createSSRApp({
      components: {
        VueComponent
      },
      render() {
        return createElement(VueComponent as any, attrs);
      }
    });

    const html = await renderToString(app);

    return html;
  };
}

export function __vue_dynamic(VueComponent: Component, importUrl: string, vueUrl: 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}';
          import {createApp, h as createElement} from '${vueUrl}';

          const App = {
            render() {
              return createElement(Component, ${JSON.stringify(attrs)});
            }
          };

          createApp(App).mount(document.getElementById('${placeholderId}'));
      </script>`;
  };
}