|
|
@@ -6,6 +6,8 @@ import { RenderContext } from './render-context'
|
|
|
import { compileToFunctions } from 'web/compiler/index'
|
|
|
import { createComponentInstanceForVnode } from 'core/vdom/create-component'
|
|
|
|
|
|
+import { isDef, isUndef, isTrue } from 'shared/util'
|
|
|
+
|
|
|
let warned = Object.create(null)
|
|
|
const warnOnce = msg => {
|
|
|
if (!warned[msg]) {
|
|
|
@@ -17,7 +19,7 @@ const warnOnce = msg => {
|
|
|
const compilationCache = Object.create(null)
|
|
|
const normalizeRender = vm => {
|
|
|
const { render, template } = vm.$options
|
|
|
- if (!render) {
|
|
|
+ if (isUndef(render)) {
|
|
|
if (template) {
|
|
|
const renderFns = (
|
|
|
compilationCache[template] ||
|
|
|
@@ -36,26 +38,26 @@ const normalizeRender = vm => {
|
|
|
|
|
|
function renderNode (node, isRoot, context) {
|
|
|
const { write, next } = context
|
|
|
- if (node.componentOptions) {
|
|
|
+ if (isDef(node.componentOptions)) {
|
|
|
// check cache hit
|
|
|
const Ctor = node.componentOptions.Ctor
|
|
|
const getKey = Ctor.options.serverCacheKey
|
|
|
const name = Ctor.options.name
|
|
|
const cache = context.cache
|
|
|
- if (getKey && cache && name) {
|
|
|
+ if (isDef(getKey) && isDef(cache) && isDef(name)) {
|
|
|
const key = name + '::' + getKey(node.componentOptions.propsData)
|
|
|
const { has, get } = context
|
|
|
- if (has) {
|
|
|
- has(key, hit => {
|
|
|
- if (hit && get) {
|
|
|
- get(key, res => write(res, next))
|
|
|
+ if (isDef(has)) {
|
|
|
+ (has: any)(key, hit => {
|
|
|
+ if (hit === true && isDef(get)) {
|
|
|
+ (get: any)(key, res => write(res, next))
|
|
|
} else {
|
|
|
renderComponentWithCache(node, isRoot, key, context)
|
|
|
}
|
|
|
})
|
|
|
- } else if (get) {
|
|
|
- get(key, res => {
|
|
|
- if (res) {
|
|
|
+ } else if (isDef(get)) {
|
|
|
+ (get: any)(key, res => {
|
|
|
+ if (isDef(res)) {
|
|
|
write(res, next)
|
|
|
} else {
|
|
|
renderComponentWithCache(node, isRoot, key, context)
|
|
|
@@ -63,7 +65,7 @@ function renderNode (node, isRoot, context) {
|
|
|
})
|
|
|
}
|
|
|
} else {
|
|
|
- if (getKey && !cache) {
|
|
|
+ if (isDef(getKey) && isUndef(cache)) {
|
|
|
warnOnce(
|
|
|
`[vue-server-renderer] Component ${
|
|
|
Ctor.options.name || '(anonymous)'
|
|
|
@@ -71,7 +73,7 @@ function renderNode (node, isRoot, context) {
|
|
|
'but no cache was provided to the renderer.'
|
|
|
)
|
|
|
}
|
|
|
- if (getKey && !name) {
|
|
|
+ if (isDef(getKey) && isUndef(name)) {
|
|
|
warnOnce(
|
|
|
`[vue-server-renderer] Components that implement "serverCacheKey" ` +
|
|
|
`must also define a unique "name" option.`
|
|
|
@@ -80,9 +82,9 @@ function renderNode (node, isRoot, context) {
|
|
|
renderComponent(node, isRoot, context)
|
|
|
}
|
|
|
} else {
|
|
|
- if (node.tag) {
|
|
|
+ if (isDef(node.tag)) {
|
|
|
renderElement(node, isRoot, context)
|
|
|
- } else if (node.isComment) {
|
|
|
+ } else if (isTrue(node.isComment)) {
|
|
|
write(`<!--${node.text}-->`, next)
|
|
|
} else {
|
|
|
write(node.raw ? node.text : escape(String(node.text)), next)
|
|
|
@@ -116,7 +118,7 @@ function renderComponentWithCache (node, isRoot, key, context) {
|
|
|
}
|
|
|
|
|
|
function renderElement (el, isRoot, context) {
|
|
|
- if (isRoot) {
|
|
|
+ if (isTrue(isRoot)) {
|
|
|
if (!el.data) el.data = {}
|
|
|
if (!el.data.attrs) el.data.attrs = {}
|
|
|
el.data.attrs[SSR_ATTR] = 'true'
|
|
|
@@ -126,7 +128,7 @@ function renderElement (el, isRoot, context) {
|
|
|
const { write, next } = context
|
|
|
if (context.isUnaryTag(el.tag)) {
|
|
|
write(startTag, next)
|
|
|
- } else if (!el.children || !el.children.length) {
|
|
|
+ } else if (isUndef(el.children) || el.children.length === 0) {
|
|
|
write(startTag + endTag, next)
|
|
|
} else {
|
|
|
const children: Array<VNode> = el.children
|
|
|
@@ -149,7 +151,7 @@ function getVShowDirectiveInfo (node: VNode): ?VNodeDirective {
|
|
|
let dir: VNodeDirective
|
|
|
let tmp
|
|
|
|
|
|
- while (node) {
|
|
|
+ while (isDef(node)) {
|
|
|
if (node.data && node.data.directives) {
|
|
|
tmp = node.data.directives.find(dir => dir.name === 'show')
|
|
|
if (tmp) {
|
|
|
@@ -167,10 +169,10 @@ function renderStartingTag (node: VNode, context) {
|
|
|
|
|
|
// construct synthetic data for module processing
|
|
|
// because modules like style also produce code by parent VNode data
|
|
|
- if (!node.data && hasAncestorData(node)) {
|
|
|
+ if (isUndef(node.data) && hasAncestorData(node)) {
|
|
|
node.data = {}
|
|
|
}
|
|
|
- if (node.data) {
|
|
|
+ if (isDef(node.data)) {
|
|
|
// check directives
|
|
|
const dirs = node.data.directives
|
|
|
if (dirs) {
|
|
|
@@ -202,13 +204,13 @@ function renderStartingTag (node: VNode, context) {
|
|
|
// attach scoped CSS ID
|
|
|
let scopeId
|
|
|
const activeInstance = context.activeInstance
|
|
|
- if (activeInstance &&
|
|
|
+ if (isDef(activeInstance) &&
|
|
|
activeInstance !== node.context &&
|
|
|
- (scopeId = activeInstance.$options._scopeId)) {
|
|
|
- markup += ` ${scopeId}`
|
|
|
+ isDef(scopeId = activeInstance.$options._scopeId)) {
|
|
|
+ markup += ` ${(scopeId: any)}`
|
|
|
}
|
|
|
- while (node) {
|
|
|
- if ((scopeId = node.context.$options._scopeId)) {
|
|
|
+ while (isDef(node)) {
|
|
|
+ if (isDef(scopeId = node.context.$options._scopeId)) {
|
|
|
markup += ` ${scopeId}`
|
|
|
}
|
|
|
node = node.parent
|