Evan You 9 лет назад
Родитель
Сommit
5213ec7bbf

+ 9 - 4
src/platforms/web/server/modules/attrs.js

@@ -2,6 +2,11 @@
 
 import { escape } from 'he'
 
+import {
+  isDef,
+  isUndef
+} from 'shared/util'
+
 import {
   isBooleanAttr,
   isEnumeratedAttr,
@@ -12,15 +17,15 @@ export default function renderAttrs (node: VNodeWithData): string {
   let attrs = node.data.attrs
   let res = ''
 
-  let parent = node.parent
-  while (parent) {
-    if (parent.data && parent.data.attrs) {
+  let parent: any = node.parent
+  while (isDef(parent)) {
+    if (isDef(parent.data) && isDef(parent.data.attrs)) {
       attrs = Object.assign({}, attrs, parent.data.attrs)
     }
     parent = parent.parent
   }
 
-  if (!attrs) {
+  if (isUndef(attrs)) {
     return res
   }
 

+ 1 - 1
src/platforms/web/server/modules/class.js

@@ -5,7 +5,7 @@ import { genClassForVnode } from 'web/util/index'
 
 export default function renderClass (node: VNodeWithData): ?string {
   const classList = genClassForVnode(node)
-  if (classList) {
+  if (classList !== '') {
     return ` class="${escape(classList)}"`
   }
 }

+ 6 - 5
src/platforms/web/server/modules/dom-props.js

@@ -2,25 +2,26 @@
 
 import VNode from 'core/vdom/vnode'
 import { renderAttr } from './attrs'
+import { isDef, isUndef } from 'shared/util'
 import { propsToAttrMap, isRenderableAttr } from '../util'
 
 export default function renderDOMProps (node: VNodeWithData): string {
   let props = node.data.domProps
   let res = ''
 
-  let parent = node.parent
-  while (parent) {
+  let parent: any = node.parent
+  while (isDef(parent)) {
     if (parent.data && parent.data.domProps) {
       props = Object.assign({}, props, parent.data.domProps)
     }
     parent = parent.parent
   }
 
-  if (!props) {
+  if (isUndef(props)) {
     return res
   }
 
-  const attrs = node.data.attrs
+  const attrs: any = node.data.attrs
   for (const key in props) {
     if (key === 'innerHTML') {
       setText(node, props[key], true)
@@ -30,7 +31,7 @@ export default function renderDOMProps (node: VNodeWithData): string {
       const attr = propsToAttrMap[key] || key.toLowerCase()
       if (isRenderableAttr(attr) &&
           // avoid rendering double-bound props/attrs twice
-          !(attrs && attrs[attr])) {
+          !(isDef(attrs) && isDef(attrs[attr]))) {
         res += renderAttr(attr, props[key])
       }
     }

+ 1 - 1
src/platforms/web/server/modules/style.js

@@ -15,7 +15,7 @@ function genStyleText (vnode: VNode): string {
 
 export default function renderStyle (vnode: VNodeWithData): ?string {
   const styleText = genStyleText(vnode)
-  if (styleText) {
+  if (styleText !== '') {
     return ` style=${JSON.stringify(escape(styleText))}`
   }
 }

+ 10 - 10
src/platforms/web/util/class.js

@@ -1,18 +1,18 @@
 /* @flow */
 
-import { isObject } from 'shared/util'
+import { isDef, isUndef, isObject } from 'shared/util'
 
 export function genClassForVnode (vnode: VNode): string {
   let data = vnode.data
   let parentNode = vnode
   let childNode = vnode
-  while (childNode.componentInstance) {
+  while (isDef(childNode.componentInstance)) {
     childNode = childNode.componentInstance._vnode
     if (childNode.data) {
       data = mergeClassData(childNode.data, data)
     }
   }
-  while ((parentNode = parentNode.parent)) {
+  while (isDef(parentNode = parentNode.parent)) {
     if (parentNode.data) {
       data = mergeClassData(data, parentNode.data)
     }
@@ -26,7 +26,7 @@ function mergeClassData (child: VNodeData, parent: VNodeData): {
 } {
   return {
     staticClass: concat(child.staticClass, parent.staticClass),
-    class: child.class
+    class: isDef(child.class)
       ? [child.class, parent.class]
       : parent.class
   }
@@ -35,7 +35,7 @@ function mergeClassData (child: VNodeData, parent: VNodeData): {
 function genClassFromData (data: Object): string {
   const dynamicClass = data.class
   const staticClass = data.staticClass
-  if (staticClass || dynamicClass) {
+  if (isDef(staticClass) || isDef(dynamicClass)) {
     return concat(staticClass, stringifyClass(dynamicClass))
   }
   /* istanbul ignore next */
@@ -47,18 +47,18 @@ export function concat (a: ?string, b: ?string): string {
 }
 
 export function stringifyClass (value: any): string {
-  let res = ''
-  if (!value) {
-    return res
+  if (isUndef(value)) {
+    return ''
   }
   if (typeof value === 'string') {
     return value
   }
+  let res = ''
   if (Array.isArray(value)) {
     let stringified
     for (let i = 0, l = value.length; i < l; i++) {
-      if (value[i]) {
-        if ((stringified = stringifyClass(value[i]))) {
+      if (isDef(value[i])) {
+        if (isDef(stringified = stringifyClass(value[i])) && stringified !== '') {
           res += stringified + ' '
         }
       }

+ 1 - 0
src/server/create-renderer.js

@@ -59,6 +59,7 @@ export function createRenderer ({
       let result = ''
       const write = createWriteFunction(text => {
         result += text
+        return false
       }, done)
       try {
         render(component, write, () => {

+ 4 - 2
src/server/render-context.js

@@ -1,5 +1,7 @@
 /* @flow */
 
+import { isUndef } from 'shared/util'
+
 type RenderState = {
   type: 'Element';
   rendered: number;
@@ -57,7 +59,7 @@ export class RenderContext {
 
   next () {
     const lastState = this.renderStates[this.renderStates.length - 1]
-    if (!lastState) {
+    if (isUndef(lastState)) {
       return this.done()
     }
     switch (lastState.type) {
@@ -99,7 +101,7 @@ export class RenderContext {
 
 function normalizeAsync (cache, method) {
   const fn = cache[method]
-  if (!fn) {
+  if (isUndef(fn)) {
     return
   } else if (fn.length > 1) {
     return (key, cb) => fn.call(cache, key, cb)

+ 4 - 2
src/server/render-stream.js

@@ -10,6 +10,7 @@
 
 const stream = require('stream')
 
+import { isTrue, isUndef } from 'shared/util'
 import { createWriteFunction } from './write'
 
 export default class RenderStream extends stream.Readable {
@@ -35,6 +36,7 @@ export default class RenderStream extends stream.Readable {
         this.pushBySize(n)
         return true // we will decide when to call next
       }
+      return false
     }, err => {
       this.emit('error', err)
     })
@@ -73,7 +75,7 @@ export default class RenderStream extends stream.Readable {
     // it's possible that the last chunk added bumped the buffer up to > 2 * n,
     // which means we will need to go through multiple read calls to drain it
     // down to < n.
-    if (this.done) {
+    if (isTrue(this.done)) {
       this.push(null)
       return
     }
@@ -81,7 +83,7 @@ export default class RenderStream extends stream.Readable {
       this.pushBySize(n)
       return
     }
-    if (!this.next) {
+    if (isUndef(this.next)) {
       // start the rendering chain.
       this.tryRender()
     } else {

+ 26 - 24
src/server/render.js

@@ -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

+ 2 - 2
src/server/write.js

@@ -3,7 +3,7 @@
 const MAX_STACK_DEPTH = 1000
 
 export function createWriteFunction (
-  write: (text: string, next: Function) => ?boolean,
+  write: (text: string, next: Function) => boolean,
   onError: Function
 ): Function {
   let stackDepth = 0
@@ -12,7 +12,7 @@ export function createWriteFunction (
       cachedWrite.cacheBuffer[cachedWrite.cacheBuffer.length - 1] += text
     }
     const waitForNext = write(text, next)
-    if (!waitForNext) {
+    if (waitForNext !== true) {
       if (stackDepth >= MAX_STACK_DEPTH) {
         process.nextTick(() => {
           try { next() } catch (e) {