feat(ssr): renderToStream (#1197)
This commit is contained in:
committed by
GitHub
parent
e0d19a6953
commit
6bc0e0a31a
46
packages/server-renderer/src/helpers/ssrCompile.ts
Normal file
46
packages/server-renderer/src/helpers/ssrCompile.ts
Normal 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))
|
||||
}
|
||||
15
packages/server-renderer/src/helpers/ssrRenderComponent.ts
Normal file
15
packages/server-renderer/src/helpers/ssrRenderComponent.ts
Normal 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
|
||||
)
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PushFn } from '../renderToString'
|
||||
import { PushFn } from '../render'
|
||||
|
||||
export async function ssrRenderSuspense(
|
||||
push: PushFn,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user