2019-09-12 11:44:37 +08:00
|
|
|
import { VNode, VNodeChild, isVNode } from './vnode'
|
2019-12-03 03:11:12 +08:00
|
|
|
import { ReactiveEffect, reactive, shallowReadonly } from '@vue/reactivity'
|
2019-09-07 00:58:31 +08:00
|
|
|
import {
|
|
|
|
PublicInstanceProxyHandlers,
|
2019-12-11 00:14:29 +08:00
|
|
|
ComponentPublicInstance,
|
|
|
|
runtimeCompiledRenderProxyHandlers
|
2019-10-02 22:03:43 +08:00
|
|
|
} from './componentProxy'
|
2019-09-06 23:19:22 +08:00
|
|
|
import { ComponentPropsOptions } from './componentProps'
|
2019-05-31 18:07:43 +08:00
|
|
|
import { Slots } from './componentSlots'
|
2019-08-31 03:05:39 +08:00
|
|
|
import { warn } from './warning'
|
|
|
|
import {
|
2019-09-07 00:58:31 +08:00
|
|
|
ErrorCodes,
|
2019-08-31 03:05:39 +08:00
|
|
|
callWithErrorHandling,
|
|
|
|
callWithAsyncErrorHandling
|
|
|
|
} from './errorHandling'
|
2019-10-15 03:36:30 +08:00
|
|
|
import { AppContext, createAppContext, AppConfig } from './apiApp'
|
2019-10-19 00:34:45 +08:00
|
|
|
import { Directive, validateDirectiveName } from './directives'
|
2019-10-02 22:03:43 +08:00
|
|
|
import { applyOptions, ComponentOptions } from './apiOptions'
|
2019-09-06 23:19:22 +08:00
|
|
|
import {
|
|
|
|
EMPTY_OBJ,
|
|
|
|
isFunction,
|
|
|
|
capitalize,
|
|
|
|
NOOP,
|
2019-10-15 03:36:30 +08:00
|
|
|
isObject,
|
2019-10-16 10:18:55 +08:00
|
|
|
NO,
|
2019-10-18 03:47:26 +08:00
|
|
|
makeMap,
|
2019-12-11 23:25:34 +08:00
|
|
|
isPromise
|
2019-09-06 23:19:22 +08:00
|
|
|
} from '@vue/shared'
|
2019-11-05 07:38:55 +08:00
|
|
|
import { SuspenseBoundary } from './components/Suspense'
|
2019-12-11 23:25:34 +08:00
|
|
|
import { CompilerOptions } from '@vue/compiler-core'
|
2019-11-23 12:32:53 +08:00
|
|
|
import { currentRenderingInstance } from './componentRenderUtils'
|
2019-05-28 13:27:31 +08:00
|
|
|
|
2019-08-13 23:18:23 +08:00
|
|
|
export type Data = { [key: string]: unknown }
|
2019-05-29 10:43:27 +08:00
|
|
|
|
2019-12-16 23:36:48 +08:00
|
|
|
export interface SFCInternalOptions {
|
|
|
|
__scopeId?: string
|
|
|
|
__hmrId?: string
|
|
|
|
__hmrUpdated?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FunctionalComponent<P = {}> extends SFCInternalOptions {
|
2019-06-19 16:43:34 +08:00
|
|
|
(props: P, ctx: SetupContext): VNodeChild
|
2019-05-28 18:06:00 +08:00
|
|
|
props?: ComponentPropsOptions<P>
|
2019-10-26 00:12:17 +08:00
|
|
|
inheritAttrs?: boolean
|
2019-05-28 18:06:00 +08:00
|
|
|
displayName?: string
|
|
|
|
}
|
|
|
|
|
2019-09-03 04:09:34 +08:00
|
|
|
export type Component = ComponentOptions | FunctionalComponent
|
2019-10-30 10:28:38 +08:00
|
|
|
export { ComponentOptions }
|
2019-09-03 04:09:34 +08:00
|
|
|
|
2019-05-28 19:36:15 +08:00
|
|
|
type LifecycleHook = Function[] | null
|
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
export const enum LifecycleHooks {
|
|
|
|
BEFORE_CREATE = 'bc',
|
|
|
|
CREATED = 'c',
|
|
|
|
BEFORE_MOUNT = 'bm',
|
|
|
|
MOUNTED = 'm',
|
|
|
|
BEFORE_UPDATE = 'bu',
|
|
|
|
UPDATED = 'u',
|
|
|
|
BEFORE_UNMOUNT = 'bum',
|
|
|
|
UNMOUNTED = 'um',
|
|
|
|
DEACTIVATED = 'da',
|
|
|
|
ACTIVATED = 'a',
|
|
|
|
RENDER_TRIGGERED = 'rtg',
|
|
|
|
RENDER_TRACKED = 'rtc',
|
|
|
|
ERROR_CAPTURED = 'ec'
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
2019-10-22 23:26:48 +08:00
|
|
|
export type Emit = (event: string, ...args: unknown[]) => void
|
2019-09-06 08:36:35 +08:00
|
|
|
|
2019-09-06 23:19:22 +08:00
|
|
|
export interface SetupContext {
|
2019-06-19 16:43:34 +08:00
|
|
|
attrs: Data
|
|
|
|
slots: Slots
|
2019-09-06 08:36:35 +08:00
|
|
|
emit: Emit
|
2019-06-19 16:43:34 +08:00
|
|
|
}
|
|
|
|
|
2019-12-11 00:14:29 +08:00
|
|
|
export type RenderFunction = {
|
|
|
|
(): VNodeChild
|
|
|
|
isRuntimeCompiled?: boolean
|
|
|
|
}
|
2019-09-06 06:48:49 +08:00
|
|
|
|
2019-09-07 00:58:31 +08:00
|
|
|
export interface ComponentInternalInstance {
|
2019-05-28 18:06:00 +08:00
|
|
|
type: FunctionalComponent | ComponentOptions
|
2019-09-07 00:58:31 +08:00
|
|
|
parent: ComponentInternalInstance | null
|
2019-09-03 04:09:34 +08:00
|
|
|
appContext: AppContext
|
2019-09-07 00:58:31 +08:00
|
|
|
root: ComponentInternalInstance
|
2019-05-29 10:43:27 +08:00
|
|
|
vnode: VNode
|
2019-05-28 17:19:47 +08:00
|
|
|
next: VNode | null
|
2019-05-29 10:43:27 +08:00
|
|
|
subTree: VNode
|
2019-05-28 17:19:47 +08:00
|
|
|
update: ReactiveEffect
|
2019-09-06 06:48:49 +08:00
|
|
|
render: RenderFunction | null
|
2019-06-19 17:31:49 +08:00
|
|
|
effects: ReactiveEffect[] | null
|
2019-06-19 22:48:22 +08:00
|
|
|
provides: Data
|
2019-12-11 00:14:29 +08:00
|
|
|
// cache for proxy access type to avoid hasOwnProperty calls
|
2019-10-21 05:00:11 +08:00
|
|
|
accessCache: Data | null
|
|
|
|
// cache for render function values that rely on _ctx but won't need updates
|
|
|
|
// after initialized (e.g. inline handlers)
|
2019-10-24 05:57:40 +08:00
|
|
|
renderCache: (Function | VNode)[] | null
|
2019-06-19 16:43:34 +08:00
|
|
|
|
2019-10-30 10:28:38 +08:00
|
|
|
// assets for fast resolution
|
2019-09-04 23:36:27 +08:00
|
|
|
components: Record<string, Component>
|
|
|
|
directives: Record<string, Directive>
|
|
|
|
|
2019-05-29 10:43:27 +08:00
|
|
|
// the rest are only for stateful components
|
2019-09-06 08:36:35 +08:00
|
|
|
renderContext: Data
|
|
|
|
data: Data
|
|
|
|
props: Data
|
|
|
|
attrs: Data
|
|
|
|
slots: Slots
|
2019-12-11 00:14:29 +08:00
|
|
|
proxy: ComponentPublicInstance | null
|
|
|
|
// alternative proxy used only for runtime-compiled render functions using
|
|
|
|
// `with` block
|
|
|
|
withProxy: ComponentPublicInstance | null
|
2019-09-06 08:36:35 +08:00
|
|
|
propsProxy: Data | null
|
2019-06-19 16:43:34 +08:00
|
|
|
setupContext: SetupContext | null
|
2019-09-04 03:27:59 +08:00
|
|
|
refs: Data
|
2019-09-06 08:36:35 +08:00
|
|
|
emit: Emit
|
2019-08-21 21:50:20 +08:00
|
|
|
|
2019-10-30 10:28:38 +08:00
|
|
|
// suspense related
|
|
|
|
asyncDep: Promise<any> | null
|
|
|
|
asyncResult: unknown
|
|
|
|
asyncResolved: boolean
|
|
|
|
|
|
|
|
// storage for any extra properties
|
|
|
|
sink: { [key: string]: any }
|
2019-08-31 00:16:09 +08:00
|
|
|
|
|
|
|
// lifecycle
|
2019-11-23 12:32:53 +08:00
|
|
|
isMounted: boolean
|
2019-09-11 21:07:29 +08:00
|
|
|
isUnmounted: boolean
|
2019-10-30 10:28:38 +08:00
|
|
|
isDeactivated: boolean
|
2019-08-31 00:16:09 +08:00
|
|
|
[LifecycleHooks.BEFORE_CREATE]: LifecycleHook
|
|
|
|
[LifecycleHooks.CREATED]: LifecycleHook
|
|
|
|
[LifecycleHooks.BEFORE_MOUNT]: LifecycleHook
|
|
|
|
[LifecycleHooks.MOUNTED]: LifecycleHook
|
|
|
|
[LifecycleHooks.BEFORE_UPDATE]: LifecycleHook
|
|
|
|
[LifecycleHooks.UPDATED]: LifecycleHook
|
|
|
|
[LifecycleHooks.BEFORE_UNMOUNT]: LifecycleHook
|
|
|
|
[LifecycleHooks.UNMOUNTED]: LifecycleHook
|
|
|
|
[LifecycleHooks.RENDER_TRACKED]: LifecycleHook
|
|
|
|
[LifecycleHooks.RENDER_TRIGGERED]: LifecycleHook
|
|
|
|
[LifecycleHooks.ACTIVATED]: LifecycleHook
|
|
|
|
[LifecycleHooks.DEACTIVATED]: LifecycleHook
|
|
|
|
[LifecycleHooks.ERROR_CAPTURED]: LifecycleHook
|
2019-12-13 07:13:59 +08:00
|
|
|
|
|
|
|
// hmr marker (dev only)
|
|
|
|
renderUpdated?: boolean
|
2019-09-06 08:36:35 +08:00
|
|
|
}
|
2019-05-28 19:36:15 +08:00
|
|
|
|
2019-09-03 04:09:34 +08:00
|
|
|
const emptyAppContext = createAppContext()
|
|
|
|
|
2019-06-03 09:43:28 +08:00
|
|
|
export function createComponentInstance(
|
2019-08-29 00:13:36 +08:00
|
|
|
vnode: VNode,
|
2019-09-07 00:58:31 +08:00
|
|
|
parent: ComponentInternalInstance | null
|
2019-10-10 00:17:42 +08:00
|
|
|
) {
|
2019-09-04 06:11:04 +08:00
|
|
|
// inherit parent app context - or - if root, adopt from root vnode
|
|
|
|
const appContext =
|
|
|
|
(parent ? parent.appContext : vnode.appContext) || emptyAppContext
|
2019-10-10 00:17:42 +08:00
|
|
|
const instance: ComponentInternalInstance = {
|
2019-08-29 00:13:36 +08:00
|
|
|
vnode,
|
2019-06-03 09:43:28 +08:00
|
|
|
parent,
|
2019-09-04 06:11:04 +08:00
|
|
|
appContext,
|
2019-11-02 11:04:28 +08:00
|
|
|
type: vnode.type as Component,
|
2019-10-10 00:17:42 +08:00
|
|
|
root: null!, // set later so it can point to itself
|
2019-05-28 19:36:15 +08:00
|
|
|
next: null,
|
2019-10-10 00:17:42 +08:00
|
|
|
subTree: null!, // will be set synchronously right after creation
|
|
|
|
update: null!, // will be set synchronously right after creation
|
2019-06-12 15:43:19 +08:00
|
|
|
render: null,
|
2019-12-11 00:14:29 +08:00
|
|
|
proxy: null,
|
|
|
|
withProxy: null,
|
2019-05-30 23:16:15 +08:00
|
|
|
propsProxy: null,
|
2019-06-19 16:43:34 +08:00
|
|
|
setupContext: null,
|
2019-08-31 00:16:09 +08:00
|
|
|
effects: null,
|
2019-09-04 06:11:04 +08:00
|
|
|
provides: parent ? parent.provides : Object.create(appContext.provides),
|
2019-10-17 10:13:52 +08:00
|
|
|
accessCache: null!,
|
2019-10-21 05:00:11 +08:00
|
|
|
renderCache: null,
|
2019-05-28 19:36:15 +08:00
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
// setup context properties
|
2019-09-06 08:36:35 +08:00
|
|
|
renderContext: EMPTY_OBJ,
|
2019-08-31 00:16:09 +08:00
|
|
|
data: EMPTY_OBJ,
|
|
|
|
props: EMPTY_OBJ,
|
|
|
|
attrs: EMPTY_OBJ,
|
|
|
|
slots: EMPTY_OBJ,
|
|
|
|
refs: EMPTY_OBJ,
|
|
|
|
|
2019-09-04 23:36:27 +08:00
|
|
|
// per-instance asset storage (mutable during options resolution)
|
|
|
|
components: Object.create(appContext.components),
|
|
|
|
directives: Object.create(appContext.directives),
|
|
|
|
|
2019-09-10 04:00:50 +08:00
|
|
|
// async dependency management
|
|
|
|
asyncDep: null,
|
|
|
|
asyncResult: null,
|
|
|
|
asyncResolved: false,
|
|
|
|
|
2019-08-31 00:16:09 +08:00
|
|
|
// user namespace for storing whatever the user assigns to `this`
|
2019-10-30 10:28:38 +08:00
|
|
|
// can also be used as a wildcard storage for ad-hoc injections internally
|
|
|
|
sink: {},
|
2019-08-31 00:16:09 +08:00
|
|
|
|
|
|
|
// lifecycle hooks
|
|
|
|
// not using enums here because it results in computed properties
|
2019-11-23 12:32:53 +08:00
|
|
|
isMounted: false,
|
2019-09-11 21:07:29 +08:00
|
|
|
isUnmounted: false,
|
2019-10-30 10:28:38 +08:00
|
|
|
isDeactivated: false,
|
2019-08-31 00:16:09 +08:00
|
|
|
bc: null,
|
|
|
|
c: null,
|
2019-05-28 19:36:15 +08:00
|
|
|
bm: null,
|
|
|
|
m: null,
|
|
|
|
bu: null,
|
|
|
|
u: null,
|
|
|
|
um: null,
|
|
|
|
bum: null,
|
|
|
|
da: null,
|
|
|
|
a: null,
|
|
|
|
rtg: null,
|
|
|
|
rtc: null,
|
|
|
|
ec: null,
|
2019-08-21 21:50:20 +08:00
|
|
|
|
2019-10-10 00:17:42 +08:00
|
|
|
emit: (event, ...args) => {
|
2019-06-19 16:43:34 +08:00
|
|
|
const props = instance.vnode.props || EMPTY_OBJ
|
|
|
|
const handler = props[`on${event}`] || props[`on${capitalize(event)}`]
|
|
|
|
if (handler) {
|
2019-10-22 01:59:10 +08:00
|
|
|
callWithAsyncErrorHandling(
|
|
|
|
handler,
|
|
|
|
instance,
|
|
|
|
ErrorCodes.COMPONENT_EVENT_HANDLER,
|
|
|
|
args
|
|
|
|
)
|
2019-06-19 16:43:34 +08:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
2019-06-03 09:43:28 +08:00
|
|
|
|
|
|
|
instance.root = parent ? parent.root : instance
|
|
|
|
return instance
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
2019-09-07 00:58:31 +08:00
|
|
|
export let currentInstance: ComponentInternalInstance | null = null
|
2019-09-11 22:09:00 +08:00
|
|
|
export let currentSuspense: SuspenseBoundary | null = null
|
2019-05-28 19:36:15 +08:00
|
|
|
|
2019-09-07 00:58:31 +08:00
|
|
|
export const getCurrentInstance: () => ComponentInternalInstance | null = () =>
|
2019-11-23 12:32:53 +08:00
|
|
|
currentInstance || currentRenderingInstance
|
2019-06-20 15:25:10 +08:00
|
|
|
|
2019-09-07 00:58:31 +08:00
|
|
|
export const setCurrentInstance = (
|
|
|
|
instance: ComponentInternalInstance | null
|
|
|
|
) => {
|
2019-08-31 00:16:09 +08:00
|
|
|
currentInstance = instance
|
|
|
|
}
|
|
|
|
|
2019-10-16 10:18:55 +08:00
|
|
|
const isBuiltInTag = /*#__PURE__*/ makeMap('slot,component')
|
2019-10-15 03:36:30 +08:00
|
|
|
|
|
|
|
export function validateComponentName(name: string, config: AppConfig) {
|
|
|
|
const appIsNativeTag = config.isNativeTag || NO
|
|
|
|
if (isBuiltInTag(name) || appIsNativeTag(name)) {
|
|
|
|
warn(
|
|
|
|
'Do not use built-in or reserved HTML elements as component id: ' + name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 22:09:00 +08:00
|
|
|
export function setupStatefulComponent(
|
|
|
|
instance: ComponentInternalInstance,
|
|
|
|
parentSuspense: SuspenseBoundary | null
|
|
|
|
) {
|
2019-05-29 10:43:27 +08:00
|
|
|
const Component = instance.type as ComponentOptions
|
2019-10-15 03:36:30 +08:00
|
|
|
|
|
|
|
if (__DEV__) {
|
|
|
|
if (Component.name) {
|
|
|
|
validateComponentName(Component.name, instance.appContext.config)
|
|
|
|
}
|
|
|
|
if (Component.components) {
|
|
|
|
const names = Object.keys(Component.components)
|
|
|
|
for (let i = 0; i < names.length; i++) {
|
2019-10-28 22:13:26 +08:00
|
|
|
validateComponentName(names[i], instance.appContext.config)
|
2019-10-15 03:36:30 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-19 00:34:45 +08:00
|
|
|
if (Component.directives) {
|
|
|
|
const names = Object.keys(Component.directives)
|
|
|
|
for (let i = 0; i < names.length; i++) {
|
|
|
|
validateDirectiveName(names[i])
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 03:36:30 +08:00
|
|
|
}
|
2019-10-17 10:13:52 +08:00
|
|
|
// 0. create render proxy property access cache
|
2019-10-18 10:29:51 +08:00
|
|
|
instance.accessCache = {}
|
2019-12-11 00:14:29 +08:00
|
|
|
// 1. create public instance / render proxy
|
|
|
|
instance.proxy = new Proxy(instance, PublicInstanceProxyHandlers)
|
2019-09-04 03:27:59 +08:00
|
|
|
// 2. create props proxy
|
|
|
|
// the propsProxy is a reactive AND readonly proxy to the actual props.
|
|
|
|
// it will be updated in resolveProps() on updates before render
|
2019-12-03 03:11:12 +08:00
|
|
|
const propsProxy = (instance.propsProxy = shallowReadonly(instance.props))
|
2019-09-04 03:27:59 +08:00
|
|
|
// 3. call setup()
|
2019-05-30 23:16:15 +08:00
|
|
|
const { setup } = Component
|
|
|
|
if (setup) {
|
2019-06-19 16:43:34 +08:00
|
|
|
const setupContext = (instance.setupContext =
|
|
|
|
setup.length > 1 ? createSetupContext(instance) : null)
|
2019-09-10 04:00:50 +08:00
|
|
|
|
|
|
|
currentInstance = instance
|
2019-09-11 22:09:00 +08:00
|
|
|
currentSuspense = parentSuspense
|
2019-08-31 03:05:39 +08:00
|
|
|
const setupResult = callWithErrorHandling(
|
|
|
|
setup,
|
|
|
|
instance,
|
2019-09-07 00:58:31 +08:00
|
|
|
ErrorCodes.SETUP_FUNCTION,
|
2019-08-31 03:05:39 +08:00
|
|
|
[propsProxy, setupContext]
|
|
|
|
)
|
2019-09-10 04:00:50 +08:00
|
|
|
currentInstance = null
|
2019-09-11 22:09:00 +08:00
|
|
|
currentSuspense = null
|
2019-06-19 22:48:22 +08:00
|
|
|
|
2019-10-18 03:47:26 +08:00
|
|
|
if (isPromise(setupResult)) {
|
2019-09-10 04:28:32 +08:00
|
|
|
if (__FEATURE_SUSPENSE__) {
|
|
|
|
// async setup returned Promise.
|
|
|
|
// bail here and wait for re-entry.
|
2019-10-05 22:09:34 +08:00
|
|
|
instance.asyncDep = setupResult
|
2019-09-10 04:28:32 +08:00
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(
|
|
|
|
`setup() returned a Promise, but the version of Vue you are using ` +
|
|
|
|
`does not support it yet.`
|
|
|
|
)
|
|
|
|
}
|
2019-06-12 15:43:19 +08:00
|
|
|
} else {
|
2019-09-11 22:09:00 +08:00
|
|
|
handleSetupResult(instance, setupResult, parentSuspense)
|
2019-06-12 15:43:19 +08:00
|
|
|
}
|
2019-08-22 05:05:14 +08:00
|
|
|
} else {
|
2019-09-11 22:09:00 +08:00
|
|
|
finishComponentSetup(instance, parentSuspense)
|
2019-09-10 04:00:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleSetupResult(
|
|
|
|
instance: ComponentInternalInstance,
|
2019-09-11 22:09:00 +08:00
|
|
|
setupResult: unknown,
|
|
|
|
parentSuspense: SuspenseBoundary | null
|
2019-09-10 04:00:50 +08:00
|
|
|
) {
|
|
|
|
if (isFunction(setupResult)) {
|
|
|
|
// setup returned an inline render function
|
|
|
|
instance.render = setupResult as RenderFunction
|
|
|
|
} else if (isObject(setupResult)) {
|
2019-09-12 11:44:37 +08:00
|
|
|
if (__DEV__ && isVNode(setupResult)) {
|
|
|
|
warn(
|
|
|
|
`setup() should not return VNodes directly - ` +
|
|
|
|
`return a render function instead.`
|
|
|
|
)
|
|
|
|
}
|
2019-09-10 04:00:50 +08:00
|
|
|
// setup returned bindings.
|
|
|
|
// assuming a render function compiled from template is present.
|
|
|
|
instance.renderContext = reactive(setupResult)
|
|
|
|
} else if (__DEV__ && setupResult !== undefined) {
|
|
|
|
warn(
|
|
|
|
`setup() should return an object. Received: ${
|
|
|
|
setupResult === null ? 'null' : typeof setupResult
|
|
|
|
}`
|
|
|
|
)
|
|
|
|
}
|
2019-09-11 22:09:00 +08:00
|
|
|
finishComponentSetup(instance, parentSuspense)
|
2019-09-10 04:00:50 +08:00
|
|
|
}
|
|
|
|
|
2019-09-21 00:16:19 +08:00
|
|
|
type CompileFunction = (
|
2019-12-11 23:25:34 +08:00
|
|
|
template: string | object,
|
2019-09-21 00:16:19 +08:00
|
|
|
options?: CompilerOptions
|
|
|
|
) => RenderFunction
|
|
|
|
|
|
|
|
let compile: CompileFunction | undefined
|
|
|
|
|
2019-11-02 10:54:01 +08:00
|
|
|
// exported method uses any to avoid d.ts relying on the compiler types.
|
|
|
|
export function registerRuntimeCompiler(_compile: any) {
|
2019-09-20 12:24:16 +08:00
|
|
|
compile = _compile
|
|
|
|
}
|
|
|
|
|
2019-09-11 22:09:00 +08:00
|
|
|
function finishComponentSetup(
|
|
|
|
instance: ComponentInternalInstance,
|
|
|
|
parentSuspense: SuspenseBoundary | null
|
|
|
|
) {
|
2019-09-10 04:00:50 +08:00
|
|
|
const Component = instance.type as ComponentOptions
|
|
|
|
if (!instance.render) {
|
2019-10-14 13:08:00 +08:00
|
|
|
if (__RUNTIME_COMPILE__ && Component.template && !Component.render) {
|
2019-10-16 05:50:38 +08:00
|
|
|
// __RUNTIME_COMPILE__ ensures `compile` is provided
|
|
|
|
Component.render = compile!(Component.template, {
|
2019-12-11 23:25:34 +08:00
|
|
|
isCustomElement: instance.appContext.config.isCustomElement || NO
|
2019-10-16 05:50:38 +08:00
|
|
|
})
|
2019-12-15 11:15:38 +08:00
|
|
|
// mark the function as runtime compiled
|
|
|
|
;(Component.render as RenderFunction).isRuntimeCompiled = true
|
2019-10-16 05:50:38 +08:00
|
|
|
}
|
2019-12-11 00:14:29 +08:00
|
|
|
|
2019-10-16 05:50:38 +08:00
|
|
|
if (__DEV__ && !Component.render) {
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (!__RUNTIME_COMPILE__ && Component.template) {
|
2019-09-20 12:24:16 +08:00
|
|
|
warn(
|
|
|
|
`Component provides template but the build of Vue you are running ` +
|
2019-12-11 23:25:34 +08:00
|
|
|
`does not support runtime template compilation. Either use the ` +
|
2019-09-20 12:24:16 +08:00
|
|
|
`full build or pre-compile the template using Vue CLI.`
|
|
|
|
)
|
2019-10-16 05:50:38 +08:00
|
|
|
} else {
|
|
|
|
warn(
|
|
|
|
`Component is missing${
|
|
|
|
__RUNTIME_COMPILE__ ? ` template or` : ``
|
|
|
|
} render function.`
|
|
|
|
)
|
2019-09-20 12:24:16 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 00:14:29 +08:00
|
|
|
|
2019-09-10 04:00:50 +08:00
|
|
|
instance.render = (Component.render || NOOP) as RenderFunction
|
2019-12-11 00:14:29 +08:00
|
|
|
|
|
|
|
// for runtime-compiled render functions using `with` blocks, the render
|
|
|
|
// proxy used needs a different `has` handler which is more performant and
|
|
|
|
// also only allows a whitelist of globals to fallthrough.
|
|
|
|
if (__RUNTIME_COMPILE__ && instance.render.isRuntimeCompiled) {
|
|
|
|
instance.withProxy = new Proxy(
|
|
|
|
instance,
|
|
|
|
runtimeCompiledRenderProxyHandlers
|
|
|
|
)
|
|
|
|
}
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
2019-09-10 04:00:50 +08:00
|
|
|
|
2019-09-04 10:25:38 +08:00
|
|
|
// support for 2.x options
|
|
|
|
if (__FEATURE_OPTIONS__) {
|
2019-09-10 04:00:50 +08:00
|
|
|
currentInstance = instance
|
2019-09-11 22:09:00 +08:00
|
|
|
currentSuspense = parentSuspense
|
2019-09-04 23:36:27 +08:00
|
|
|
applyOptions(instance, Component)
|
2019-09-10 04:00:50 +08:00
|
|
|
currentInstance = null
|
2019-09-11 22:09:00 +08:00
|
|
|
currentSuspense = null
|
2019-09-04 10:25:38 +08:00
|
|
|
}
|
2019-09-10 04:00:50 +08:00
|
|
|
|
2019-09-06 08:36:35 +08:00
|
|
|
if (instance.renderContext === EMPTY_OBJ) {
|
|
|
|
instance.renderContext = reactive({})
|
2019-09-05 06:16:11 +08:00
|
|
|
}
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
2019-05-28 17:19:47 +08:00
|
|
|
|
2019-08-23 10:07:51 +08:00
|
|
|
// used to identify a setup context proxy
|
|
|
|
export const SetupProxySymbol = Symbol()
|
|
|
|
|
2019-06-19 16:43:34 +08:00
|
|
|
const SetupProxyHandlers: { [key: string]: ProxyHandler<any> } = {}
|
2019-10-21 22:36:38 +08:00
|
|
|
;['attrs', 'slots'].forEach((type: string) => {
|
2019-06-19 16:43:34 +08:00
|
|
|
SetupProxyHandlers[type] = {
|
2019-10-05 22:09:34 +08:00
|
|
|
get: (instance, key) => instance[type][key],
|
|
|
|
has: (instance, key) => key === SetupProxySymbol || key in instance[type],
|
|
|
|
ownKeys: instance => Reflect.ownKeys(instance[type]),
|
2019-08-23 10:07:51 +08:00
|
|
|
// this is necessary for ownKeys to work properly
|
|
|
|
getOwnPropertyDescriptor: (instance, key) =>
|
|
|
|
Reflect.getOwnPropertyDescriptor(instance[type], key),
|
2019-06-19 16:43:34 +08:00
|
|
|
set: () => false,
|
|
|
|
deleteProperty: () => false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-09-07 00:58:31 +08:00
|
|
|
function createSetupContext(instance: ComponentInternalInstance): SetupContext {
|
2019-06-19 16:43:34 +08:00
|
|
|
const context = {
|
2019-10-21 22:36:38 +08:00
|
|
|
// attrs & slots are non-reactive, but they need to always expose
|
2019-06-19 16:43:34 +08:00
|
|
|
// the latest values (instance.xxx may get replaced during updates) so we
|
|
|
|
// need to expose them through a proxy
|
|
|
|
attrs: new Proxy(instance, SetupProxyHandlers.attrs),
|
|
|
|
slots: new Proxy(instance, SetupProxyHandlers.slots),
|
2019-08-27 06:08:56 +08:00
|
|
|
emit: instance.emit
|
2019-10-05 22:09:34 +08:00
|
|
|
}
|
2019-06-19 16:43:34 +08:00
|
|
|
return __DEV__ ? Object.freeze(context) : context
|
|
|
|
}
|