]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: root Vue compat
authorEvan You <yyx990803@gmail.com>
Thu, 20 Sep 2018 03:19:25 +0000 (23:19 -0400)
committerEvan You <yyx990803@gmail.com>
Thu, 20 Sep 2018 03:19:25 +0000 (23:19 -0400)
packages/core/src/componentUtils.ts
packages/core/src/createRenderer.ts
packages/core/src/index.ts
packages/vue/src/index.ts

index fe0e43af063dbf8ffe0a0c17f114f37147f6cc22..6ef7c27cf0853e524c8cdd2a8a1cd10da63a0bd4 100644 (file)
@@ -1,5 +1,6 @@
 import { VNodeFlags } from './flags'
 import { EMPTY_OBJ } from './utils'
+import { h } from './h'
 import { VNode, createFragment } from './vdom'
 import { Component, MountedComponent, ComponentClass } from './component'
 import { createTextVNode, cloneVNode } from './vdom'
@@ -153,6 +154,7 @@ export function shouldUpdateFunctionalComponent(
   return shouldUpdate
 }
 
+// compat only
 export function createComponentClassFromOptions(
   options: ComponentOptions
 ): ComponentClass {
@@ -165,7 +167,13 @@ export function createComponentClassFromOptions(
   for (const key in options) {
     const value = options[key]
     if (typeof value === 'function') {
-      ;(ObjectComponent.prototype as any)[key] = value
+      ;(ObjectComponent.prototype as any)[key] =
+        key === 'render'
+          ? // normalize render for legacy signature
+            function render() {
+              return value.call(this, h)
+            }
+          : value
     }
     if (key === 'computed') {
       const isGet = typeof value === 'function'
index 7d5ccd63396b049e8a60f50954a15d88c83f667d..0541d1fb5fd1f651aa904fb0e0e79c3e3899de44 100644 (file)
@@ -1174,11 +1174,11 @@ export function createRenderer(options: RendererOptions) {
     isSVG: boolean,
     endNode: RenderNode | RenderFragment | null
   ): RenderNode {
-    const instance = createComponentInstance(
-      parentVNode,
-      Component,
-      parentComponent
-    )
+    // a vnode may already have an instance if this is a compat call
+    // with new Vue()
+    const instance =
+      (__COMPAT__ && (parentVNode.children as MountedComponent)) ||
+      createComponentInstance(parentVNode, Component, parentComponent)
 
     const queueUpdate = (instance.$forceUpdate = () => {
       queueJob(instance._updateHandle, flushHooks)
index 14fa9679e52c60205c8008948dd22b03b505d743..6cd5decc6af4b14d12359e6075875499e2aac27d 100644 (file)
@@ -10,8 +10,11 @@ export const Component = InternalComponent as ComponentClass
 // observer api
 export * from '@vue/observer'
 
+// internal api
+export { createComponentInstance } from './componentUtils'
+
 // flags & types
-export { FunctionalComponent } from './component'
+export { ComponentClass, FunctionalComponent } from './component'
 export { ComponentOptions, PropType } from './componentOptions'
 export { VNodeFlags, ChildrenFlags } from './flags'
 export { VNode, VNodeData, VNodeChildren, Key, Ref, Slots, Slot } from './vdom'
index d98bad2115b8629baf30c706615fc64c3c5c69b5..a29912cbdda1d39bf97d2a073dd108d0b22e6158 100644 (file)
@@ -1,25 +1,36 @@
-import { h, render, ComponentOptions } from '@vue/renderer-dom'
+import {
+  h,
+  render,
+  Component,
+  ComponentOptions,
+  createComponentInstance
+} from '@vue/renderer-dom'
 
-function Vue(options: ComponentOptions & { el: any }) {
-  const { el, render: r } = options
+class Vue extends Component {
+  static h = h
+  static render = render
 
-  if (r) {
-    options.render = function(props, slots) {
-      return r.call(this, h, props, slots)
+  constructor(options: ComponentOptions & { el: any }) {
+    super()
+    if (!options) {
+      return
     }
-  }
 
-  function mount(el: any) {
-    const dom = document.querySelector(el)
-    render(h(options), dom)
-    return (dom as any).vnode.children.$proxy
-  }
+    const vnode = h(options)
+    const instance = createComponentInstance(vnode, options._normalized, null)
+    vnode.children = instance
+
+    function mount(el: any) {
+      const dom = document.querySelector(el)
+      render(vnode, dom)
+      return instance.$proxy
+    }
 
-  if (el) {
-    return mount(el)
-  } else {
-    return {
-      $mount: mount
+    if (options.el) {
+      return mount(options.el)
+    } else {
+      ;(instance as any).$mount = mount
+      return instance.$proxy
     }
   }
 }