vue3-yuanma/packages/runtime-core/src/component.ts

559 lines
15 KiB
TypeScript
Raw Normal View History

import { VNode, VNodeChild, isVNode } from './vnode'
import {
reactive,
ReactiveEffect,
pauseTracking,
resetTracking
} from '@vue/reactivity'
2019-09-07 00:58:31 +08:00
import {
ComponentPublicInstance,
ComponentPublicProxyTarget,
PublicInstanceProxyHandlers,
RuntimeCompiledPublicInstanceProxyHandlers,
createDevProxyTarget,
exposePropsOnDevProxyTarget,
exposeRenderContextOnDevProxyTarget
} from './componentProxy'
import { ComponentPropsOptions, initProps } from './componentProps'
import { Slots, initSlots, InternalSlots } from './componentSlots'
import { warn } from './warning'
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import { AppContext, createAppContext, AppConfig } from './apiCreateApp'
import { Directive, validateDirectiveName } from './directives'
import { applyOptions, ComponentOptions } from './componentOptions'
import {
EmitsOptions,
ObjectEmitsOptions,
EmitFn,
emit
} from './componentEmits'
import {
EMPTY_OBJ,
isFunction,
NOOP,
isObject,
NO,
makeMap,
isPromise,
ShapeFlags
} from '@vue/shared'
import { SuspenseBoundary } from './components/Suspense'
import { CompilerOptions } from '@vue/compiler-core'
import {
currentRenderingInstance,
markAttrsAccessed
} from './componentRenderUtils'
import { startMeasure, endMeasure } from './profiling'
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
export interface SFCInternalOptions {
__scopeId?: string
2019-12-18 10:28:24 +08:00
__cssModules?: Data
__hmrId?: string
__hmrUpdated?: boolean
}
export interface FunctionalComponent<
P = {},
E extends EmitsOptions = Record<string, any>
> extends SFCInternalOptions {
(props: P, ctx: SetupContext<E>): any
2019-05-28 18:06:00 +08:00
props?: ComponentPropsOptions<P>
emits?: E | (keyof E)[]
inheritAttrs?: boolean
2019-05-28 18:06:00 +08:00
displayName?: string
}
export interface ClassComponent {
new (...args: any[]): ComponentPublicInstance<any, any, any, any, any>
__vccOpts: ComponentOptions
}
export type Component = ComponentOptions | FunctionalComponent<any>
// A type used in public APIs where a component type is expected.
// The constructor type is an artificial type returned by defineComponent().
export type PublicAPIComponent =
| Component
| { new (...args: any[]): ComponentPublicInstance<any, any, any, any, any> }
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
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
}
export interface SetupContext<E = ObjectEmitsOptions> {
2019-06-19 16:43:34 +08:00
attrs: Data
slots: Slots
emit: EmitFn<E>
2019-06-19 16:43:34 +08:00
}
export type RenderFunction = {
(
ctx: ComponentPublicInstance,
cache: ComponentInternalInstance['renderCache']
): VNodeChild
_rc?: boolean // isRuntimeCompiled
}
2019-09-07 00:58:31 +08:00
export interface ComponentInternalInstance {
uid: number
type: Component
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
render: RenderFunction | null
2019-06-19 17:31:49 +08:00
effects: ReactiveEffect[] | null
2019-06-19 22:48:22 +08:00
provides: Data
// cache for proxy access type to avoid hasOwnProperty calls
accessCache: Data | null
// cache for render function values that rely on _ctx but won't need updates
// after initialized (e.g. inline handlers)
renderCache: (Function | VNode)[]
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
renderContext: Data
data: Data
props: Data
attrs: Data
slots: InternalSlots
proxy: ComponentPublicInstance | null
proxyTarget: ComponentPublicProxyTarget
// alternative proxy used only for runtime-compiled render functions using
// `with` block
withProxy: ComponentPublicInstance | null
2019-06-19 16:43:34 +08:00
setupContext: SetupContext | null
refs: Data
emit: EmitFn
2019-08-21 21:50:20 +08:00
2019-10-30 10:28:38 +08:00
// suspense related
suspense: SuspenseBoundary | null
2019-10-30 10:28:38 +08:00
asyncDep: Promise<any> | null
asyncResolved: boolean
// storage for any extra properties
sink: { [key: string]: any }
// lifecycle
2019-11-23 12:32:53 +08:00
isMounted: boolean
isUnmounted: boolean
2019-10-30 10:28:38 +08:00
isDeactivated: boolean
[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
// hmr marker (dev only)
renderUpdated?: boolean
}
2019-05-28 19:36:15 +08:00
2019-09-03 04:09:34 +08:00
const emptyAppContext = createAppContext()
let uid = 0
export function createComponentInstance(
2019-08-29 00:13:36 +08:00
vnode: VNode,
parent: ComponentInternalInstance | null,
suspense: SuspenseBoundary | null
) {
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
const instance: ComponentInternalInstance = {
uid: uid++,
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,
root: null!, // to be immediately set
2019-05-28 19:36:15 +08:00
next: null,
subTree: null!, // will be set synchronously right after creation
update: null!, // will be set synchronously right after creation
render: null,
proxy: null,
proxyTarget: null!, // to be immediately set
withProxy: null,
2019-06-19 16:43:34 +08:00
setupContext: null,
effects: null,
2019-09-04 06:11:04 +08:00
provides: parent ? parent.provides : Object.create(appContext.provides),
accessCache: null!,
renderCache: [],
2019-05-28 19:36:15 +08:00
// setup context properties
renderContext: EMPTY_OBJ,
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),
// suspense related
suspense,
2019-09-10 04:00:50 +08:00
asyncDep: null,
asyncResolved: false,
// 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: {},
// lifecycle hooks
// not using enums here because it results in computed properties
2019-11-23 12:32:53 +08:00
isMounted: false,
isUnmounted: false,
2019-10-30 10:28:38 +08:00
isDeactivated: false,
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,
emit: null as any // to be set immediately
2019-05-28 19:36:15 +08:00
}
if (__DEV__) {
instance.proxyTarget = createDevProxyTarget(instance)
} else {
instance.proxyTarget = { _: instance }
}
2019-06-03 09:43:28 +08:00
instance.root = parent ? parent.root : instance
instance.emit = emit.bind(null, instance)
2019-06-03 09:43:28 +08:00
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-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
) => {
currentInstance = instance
}
const isBuiltInTag = /*#__PURE__*/ makeMap('slot,component')
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
)
}
}
export let isInSSRComponentSetup = false
2020-01-24 11:23:10 +08:00
export function setupComponent(
instance: ComponentInternalInstance,
isSSR = false
) {
isInSSRComponentSetup = isSSR
2020-01-24 11:23:10 +08:00
const { props, children, shapeFlag } = instance.vnode
const isStateful = shapeFlag & ShapeFlags.STATEFUL_COMPONENT
initProps(instance, props, isStateful, isSSR)
initSlots(instance, children)
2020-01-24 11:23:10 +08:00
const setupResult = isStateful
? setupStatefulComponent(instance, isSSR)
: undefined
isInSSRComponentSetup = false
return setupResult
2020-01-24 11:23:10 +08:00
}
function setupStatefulComponent(
instance: ComponentInternalInstance,
isSSR: boolean
2020-01-24 11:23:10 +08:00
) {
2019-05-29 10:43:27 +08:00
const Component = instance.type as ComponentOptions
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++) {
validateComponentName(names[i], instance.appContext.config)
}
}
if (Component.directives) {
const names = Object.keys(Component.directives)
for (let i = 0; i < names.length; i++) {
validateDirectiveName(names[i])
}
}
}
// 0. create render proxy property access cache
2019-10-18 10:29:51 +08:00
instance.accessCache = {}
// 1. create public instance / render proxy
instance.proxy = new Proxy(instance.proxyTarget, PublicInstanceProxyHandlers)
if (__DEV__) {
exposePropsOnDevProxyTarget(instance)
}
// 2. 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
pauseTracking()
const setupResult = callWithErrorHandling(
setup,
instance,
2019-09-07 00:58:31 +08:00
ErrorCodes.SETUP_FUNCTION,
[instance.props, setupContext]
)
resetTracking()
2019-09-10 04:00:50 +08:00
currentInstance = null
2019-06-19 22:48:22 +08:00
if (isPromise(setupResult)) {
if (isSSR) {
// return the promise so server-renderer can wait on it
return setupResult.then((resolvedResult: unknown) => {
handleSetupResult(instance, resolvedResult, isSSR)
})
} else if (__FEATURE_SUSPENSE__) {
2019-09-10 04:28:32 +08:00
// 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.`
)
}
} else {
handleSetupResult(instance, setupResult, isSSR)
}
2019-08-22 05:05:14 +08:00
} else {
finishComponentSetup(instance, isSSR)
2019-09-10 04:00:50 +08:00
}
}
export function handleSetupResult(
instance: ComponentInternalInstance,
setupResult: unknown,
isSSR: boolean
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)) {
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)
if (__DEV__) {
exposeRenderContextOnDevProxyTarget(instance)
}
2019-09-10 04:00:50 +08:00
} else if (__DEV__ && setupResult !== undefined) {
warn(
`setup() should return an object. Received: ${
setupResult === null ? 'null' : typeof setupResult
}`
)
}
finishComponentSetup(instance, isSSR)
2019-09-10 04:00:50 +08:00
}
type CompileFunction = (
template: string | object,
options?: CompilerOptions
) => RenderFunction
let compile: CompileFunction | undefined
// 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
}
function finishComponentSetup(
instance: ComponentInternalInstance,
isSSR: boolean
) {
2019-09-10 04:00:50 +08:00
const Component = instance.type as ComponentOptions
// template / render function normalization
if (__NODE_JS__ && isSSR) {
if (Component.render) {
instance.render = Component.render as RenderFunction
}
} else if (!instance.render) {
if (compile && Component.template && !Component.render) {
if (__DEV__) {
startMeasure(instance, `compile`)
}
Component.render = compile(Component.template, {
isCustomElement: instance.appContext.config.isCustomElement || NO
})
if (__DEV__) {
endMeasure(instance, `compile`)
}
// mark the function as runtime compiled
;(Component.render as RenderFunction)._rc = true
}
if (__DEV__ && !Component.render) {
/* istanbul ignore if */
if (!compile && Component.template) {
2019-09-20 12:24:16 +08:00
warn(
`Component provides template but the build of Vue you are running ` +
`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.`
)
} else {
warn(`Component is missing template or render function.`)
2019-09-20 12:24:16 +08:00
}
}
2019-09-10 04:00:50 +08:00
instance.render = (Component.render || NOOP) as RenderFunction
// 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 (instance.render._rc) {
instance.withProxy = new Proxy(
instance.proxyTarget,
RuntimeCompiledPublicInstanceProxyHandlers
)
}
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-04 23:36:27 +08:00
applyOptions(instance, Component)
2019-09-10 04:00:50 +08:00
currentInstance = null
2019-09-04 10:25:38 +08:00
}
2019-05-28 19:36:15 +08:00
}
2019-05-28 17:19:47 +08:00
const slotsHandlers: ProxyHandler<InternalSlots> = {
set: () => {
warn(`setupContext.slots is readonly.`)
return false
},
deleteProperty: () => {
warn(`setupContext.slots is readonly.`)
return false
2019-06-19 16:43:34 +08:00
}
}
2019-06-19 16:43:34 +08:00
const attrHandlers: ProxyHandler<Data> = {
get: (target, key: string) => {
markAttrsAccessed()
return target[key]
},
set: () => {
warn(`setupContext.attrs is readonly.`)
return false
},
deleteProperty: () => {
warn(`setupContext.attrs is readonly.`)
return false
}
}
2019-09-07 00:58:31 +08:00
function createSetupContext(instance: ComponentInternalInstance): SetupContext {
if (__DEV__) {
// We use getters in dev in case libs like test-utils overwrite instance
// properties (overwrites should not be done in prod)
return Object.freeze({
get attrs() {
return new Proxy(instance.attrs, attrHandlers)
},
get slots() {
return new Proxy(instance.slots, slotsHandlers)
},
get emit() {
return (event: string, ...args: any[]) => instance.emit(event, ...args)
}
})
} else {
return {
attrs: instance.attrs,
slots: instance.slots,
emit: instance.emit
}
2019-10-05 22:09:34 +08:00
}
2019-06-19 16:43:34 +08:00
}
// record effects created during a component's setup() so that they can be
// stopped when the component unmounts
export function recordInstanceBoundEffect(effect: ReactiveEffect) {
if (currentInstance) {
;(currentInstance.effects || (currentInstance.effects = [])).push(effect)
}
}
const classifyRE = /(?:^|[-_])(\w)/g
const classify = (str: string): string =>
str.replace(classifyRE, c => c.toUpperCase()).replace(/[-_]/g, '')
export function formatComponentName(
Component: Component,
file?: string
): string {
let name = isFunction(Component)
? Component.displayName || Component.name
: Component.name
if (!name && file) {
const match = file.match(/([^/\\]+)\.vue$/)
if (match) {
name = match[1]
}
}
return name ? classify(name) : 'Anonymous'
}