|
1 |
| -import { |
2 |
| - App, |
3 |
| - Component, |
4 |
| - ComponentInternalInstance, |
5 |
| - createComponentInstance, |
6 |
| - setupComponent, |
7 |
| - VNode, |
8 |
| - createVNode |
9 |
| -} from 'vue' |
10 |
| -import { isString, isPromise, isArray } from '@vue/shared' |
| 1 | +import { toDisplayString } from 'vue' |
11 | 2 |
|
12 |
| -export * from './helpers' |
| 3 | +export { renderToString, renderComponent } from './renderToString' |
13 | 4 |
|
14 |
| -type SSRBuffer = SSRBufferItem[] |
15 |
| -type SSRBufferItem = string | ResolvedSSRBuffer | Promise<SSRBuffer> |
16 |
| -type ResolvedSSRBuffer = (string | ResolvedSSRBuffer)[] |
| 5 | +export { |
| 6 | + renderVNode, |
| 7 | + renderClass, |
| 8 | + renderStyle, |
| 9 | + renderProps |
| 10 | +} from './renderVnode' |
17 | 11 |
|
18 |
| -function createBuffer() { |
19 |
| - let appendable = false |
20 |
| - let hasAsync = false |
21 |
| - const buffer: SSRBuffer = [] |
22 |
| - return { |
23 |
| - buffer, |
24 |
| - hasAsync() { |
25 |
| - return hasAsync |
26 |
| - }, |
27 |
| - push(item: SSRBufferItem) { |
28 |
| - const isStringItem = isString(item) |
29 |
| - if (appendable && isStringItem) { |
30 |
| - buffer[buffer.length - 1] += item as string |
31 |
| - } else { |
32 |
| - buffer.push(item) |
33 |
| - } |
34 |
| - appendable = isStringItem |
35 |
| - if (!isStringItem && !isArray(item)) { |
36 |
| - // promise |
37 |
| - hasAsync = true |
38 |
| - } |
39 |
| - } |
40 |
| - } |
41 |
| -} |
42 |
| - |
43 |
| -function unrollBuffer(buffer: ResolvedSSRBuffer): string { |
44 |
| - let ret = '' |
45 |
| - for (let i = 0; i < buffer.length; i++) { |
46 |
| - const item = buffer[i] |
47 |
| - if (isString(item)) { |
48 |
| - ret += item |
49 |
| - } else { |
50 |
| - ret += unrollBuffer(item) |
51 |
| - } |
52 |
| - } |
53 |
| - return ret |
54 |
| -} |
55 |
| - |
56 |
| -export async function renderToString(app: App): Promise<string> { |
57 |
| - const resolvedBuffer = (await renderComponent( |
58 |
| - app._component, |
59 |
| - app._props |
60 |
| - )) as ResolvedSSRBuffer |
61 |
| - return unrollBuffer(resolvedBuffer) |
62 |
| -} |
63 |
| - |
64 |
| -export function renderComponent( |
65 |
| - comp: Component, |
66 |
| - props: Record<string, any> | null = null, |
67 |
| - children: VNode['children'] = null, |
68 |
| - parentComponent: ComponentInternalInstance | null = null |
69 |
| -): ResolvedSSRBuffer | Promise<SSRBuffer> { |
70 |
| - const vnode = createVNode(comp, props, children) |
71 |
| - const instance = createComponentInstance(vnode, parentComponent) |
72 |
| - const res = setupComponent(instance, null) |
73 |
| - if (isPromise(res)) { |
74 |
| - return res.then(() => innerRenderComponent(comp, instance)) |
75 |
| - } else { |
76 |
| - return innerRenderComponent(comp, instance) |
77 |
| - } |
78 |
| -} |
| 12 | +export { escape } from './escape' |
79 | 13 |
|
80 |
| -function innerRenderComponent( |
81 |
| - comp: Component, |
82 |
| - instance: ComponentInternalInstance |
83 |
| -): ResolvedSSRBuffer | Promise<SSRBuffer> { |
84 |
| - const { buffer, push, hasAsync } = createBuffer() |
85 |
| - if (typeof comp === 'function') { |
86 |
| - // TODO FunctionalComponent |
87 |
| - } else { |
88 |
| - if (comp.ssrRender) { |
89 |
| - // optimized |
90 |
| - comp.ssrRender(push, instance.proxy) |
91 |
| - } else if (comp.render) { |
92 |
| - // TODO fallback to vdom serialization |
93 |
| - } else { |
94 |
| - // TODO warn component missing render function |
95 |
| - } |
96 |
| - } |
97 |
| - // If the current component's buffer contains any Promise from async children, |
98 |
| - // then it must return a Promise too. Otherwise this is a component that |
99 |
| - // contains only sync children so we can avoid the async book-keeping overhead. |
100 |
| - return hasAsync() |
101 |
| - ? // TS can't figure out the typing due to recursive appearance of Promise |
102 |
| - Promise.all(buffer as any) |
103 |
| - : (buffer as ResolvedSSRBuffer) |
| 14 | +export function interpolate(value: unknown) { |
| 15 | + return escape(toDisplayString(value)) |
104 | 16 | }
|
0 commit comments