feat(ssr): renderToStream (#1197)

This commit is contained in:
Stanislav Lashmanov
2020-06-26 18:09:47 +03:00
committed by GitHub
parent e0d19a6953
commit 6bc0e0a31a
13 changed files with 1054 additions and 389 deletions

View File

@@ -0,0 +1,46 @@
import { ComponentInternalInstance, warn } from 'vue'
import { compile } from '@vue/compiler-ssr'
import { generateCodeFrame, NO } from '@vue/shared'
import { CompilerError } from '@vue/compiler-core'
import { PushFn } from '../render'
type SSRRenderFunction = (
context: any,
push: PushFn,
parentInstance: ComponentInternalInstance
) => void
const compileCache: Record<string, SSRRenderFunction> = Object.create(null)
export function ssrCompile(
template: string,
instance: ComponentInternalInstance
): SSRRenderFunction {
const cached = compileCache[template]
if (cached) {
return cached
}
const { code } = compile(template, {
isCustomElement: instance.appContext.config.isCustomElement || NO,
isNativeTag: instance.appContext.config.isNativeTag || NO,
onError(err: CompilerError) {
if (__DEV__) {
const message = `[@vue/server-renderer] Template compilation error: ${
err.message
}`
const codeFrame =
err.loc &&
generateCodeFrame(
template as string,
err.loc.start.offset,
err.loc.end.offset
)
warn(codeFrame ? `${message}\n${codeFrame}` : message)
} else {
throw err
}
}
})
return (compileCache[template] = Function('require', code)(require))
}

View File

@@ -0,0 +1,15 @@
import { Component, ComponentInternalInstance, createVNode, Slots } from 'vue'
import { Props, renderComponentVNode, SSRBuffer } from '../render'
import { SSRSlots } from './ssrRenderSlot'
export function ssrRenderComponent(
comp: Component,
props: Props | null = null,
children: Slots | SSRSlots | null = null,
parentComponent: ComponentInternalInstance | null = null
): SSRBuffer | Promise<SSRBuffer> {
return renderComponentVNode(
createVNode(comp, props, children),
parentComponent
)
}

View File

@@ -1,8 +1,7 @@
import { Props, PushFn, renderVNodeChildren } from '../renderToString'
import { ComponentInternalInstance, Slot, Slots } from 'vue'
import { Props, PushFn, renderVNodeChildren } from '../render'
export type SSRSlots = Record<string, SSRSlot>
export type SSRSlot = (
props: Props,
push: PushFn,

View File

@@ -1,4 +1,4 @@
import { PushFn } from '../renderToString'
import { PushFn } from '../render'
export async function ssrRenderSuspense(
push: PushFn,

View File

@@ -1,10 +1,5 @@
import { ComponentInternalInstance, ssrContextKey } from 'vue'
import {
SSRContext,
createBuffer,
PushFn,
SSRBufferItem
} from '../renderToString'
import { createBuffer, PushFn, SSRBufferItem, SSRContext } from '../render'
export function ssrRenderTeleport(
parentPush: PushFn,