2019-09-06 16:58:31 +00:00
|
|
|
import { Component, Data, ComponentInternalInstance } from './component'
|
2019-10-02 14:03:43 +00:00
|
|
|
import { ComponentOptions } from './apiOptions'
|
|
|
|
import { ComponentPublicInstance } from './componentProxy'
|
2019-09-02 20:09:34 +00:00
|
|
|
import { Directive } from './directives'
|
2019-09-06 20:58:32 +00:00
|
|
|
import { RootRenderFunction } from './createRenderer'
|
2019-09-02 20:09:34 +00:00
|
|
|
import { InjectionKey } from './apiInject'
|
2019-09-04 15:36:27 +00:00
|
|
|
import { isFunction } from '@vue/shared'
|
2019-09-02 20:09:34 +00:00
|
|
|
import { warn } from './warning'
|
|
|
|
import { createVNode } from './vnode'
|
|
|
|
|
2019-09-06 20:58:32 +00:00
|
|
|
export interface App<HostElement = any> {
|
2019-09-02 20:09:34 +00:00
|
|
|
config: AppConfig
|
|
|
|
use(plugin: Plugin, options?: any): this
|
|
|
|
mixin(mixin: ComponentOptions): this
|
|
|
|
component(name: string): Component | undefined
|
|
|
|
component(name: string, component: Component): this
|
|
|
|
directive(name: string): Directive | undefined
|
|
|
|
directive(name: string, directive: Directive): this
|
|
|
|
mount(
|
|
|
|
rootComponent: Component,
|
2019-09-06 20:58:32 +00:00
|
|
|
rootContainer: HostElement,
|
2019-09-02 20:09:34 +00:00
|
|
|
rootProps?: Data
|
2019-09-06 16:58:31 +00:00
|
|
|
): ComponentPublicInstance
|
2019-09-02 20:09:34 +00:00
|
|
|
provide<T>(key: InjectionKey<T> | string, value: T): void
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AppConfig {
|
|
|
|
devtools: boolean
|
|
|
|
performance: boolean
|
|
|
|
errorHandler?: (
|
|
|
|
err: Error,
|
2019-09-06 16:58:31 +00:00
|
|
|
instance: ComponentPublicInstance | null,
|
2019-09-02 20:09:34 +00:00
|
|
|
info: string
|
|
|
|
) => void
|
|
|
|
warnHandler?: (
|
|
|
|
msg: string,
|
2019-09-06 16:58:31 +00:00
|
|
|
instance: ComponentPublicInstance | null,
|
2019-09-02 20:09:34 +00:00
|
|
|
trace: string
|
|
|
|
) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AppContext {
|
|
|
|
config: AppConfig
|
|
|
|
mixins: ComponentOptions[]
|
|
|
|
components: Record<string, Component>
|
|
|
|
directives: Record<string, Directive>
|
|
|
|
provides: Record<string | symbol, any>
|
|
|
|
}
|
|
|
|
|
|
|
|
type PluginInstallFunction = (app: App) => any
|
|
|
|
|
2019-09-03 22:11:04 +00:00
|
|
|
export type Plugin =
|
2019-09-02 20:09:34 +00:00
|
|
|
| PluginInstallFunction
|
|
|
|
| {
|
|
|
|
install: PluginInstallFunction
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createAppContext(): AppContext {
|
|
|
|
return {
|
|
|
|
config: {
|
|
|
|
devtools: true,
|
|
|
|
performance: false,
|
|
|
|
errorHandler: undefined,
|
2019-09-02 20:43:26 +00:00
|
|
|
warnHandler: undefined
|
2019-09-02 20:09:34 +00:00
|
|
|
},
|
|
|
|
mixins: [],
|
|
|
|
components: {},
|
|
|
|
directives: {},
|
|
|
|
provides: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 20:58:32 +00:00
|
|
|
export function createAppAPI<HostNode, HostElement>(
|
|
|
|
render: RootRenderFunction<HostNode, HostElement>
|
|
|
|
): () => App<HostElement> {
|
2019-09-02 20:09:34 +00:00
|
|
|
return function createApp(): App {
|
|
|
|
const context = createAppContext()
|
|
|
|
|
2019-09-03 22:11:04 +00:00
|
|
|
let isMounted = false
|
|
|
|
|
2019-09-02 20:09:34 +00:00
|
|
|
const app: App = {
|
|
|
|
get config() {
|
|
|
|
return context.config
|
|
|
|
},
|
|
|
|
|
|
|
|
set config(v) {
|
2019-09-02 20:16:08 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
warn(
|
|
|
|
`app.config cannot be replaced. Modify individual options instead.`
|
|
|
|
)
|
|
|
|
}
|
2019-09-02 20:09:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
use(plugin: Plugin) {
|
|
|
|
if (isFunction(plugin)) {
|
|
|
|
plugin(app)
|
|
|
|
} else if (isFunction(plugin.install)) {
|
|
|
|
plugin.install(app)
|
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(
|
|
|
|
`A plugin must either be a function or an object with an "install" ` +
|
|
|
|
`function.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return app
|
|
|
|
},
|
|
|
|
|
|
|
|
mixin(mixin: ComponentOptions) {
|
|
|
|
context.mixins.push(mixin)
|
|
|
|
return app
|
|
|
|
},
|
|
|
|
|
|
|
|
component(name: string, component?: Component) {
|
|
|
|
// TODO component name validation
|
|
|
|
if (!component) {
|
|
|
|
return context.components[name] as any
|
|
|
|
} else {
|
|
|
|
context.components[name] = component
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
directive(name: string, directive?: Directive) {
|
|
|
|
// TODO directive name validation
|
|
|
|
if (!directive) {
|
|
|
|
return context.directives[name] as any
|
|
|
|
} else {
|
|
|
|
context.directives[name] = directive
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-09-06 20:58:32 +00:00
|
|
|
mount(
|
|
|
|
rootComponent: Component,
|
|
|
|
rootContainer: string | HostElement,
|
|
|
|
rootProps?: Data
|
|
|
|
): any {
|
2019-09-03 22:11:04 +00:00
|
|
|
if (!isMounted) {
|
|
|
|
const vnode = createVNode(rootComponent, rootProps)
|
|
|
|
// store app context on the root VNode.
|
|
|
|
// this will be set on the root instance on initial mount.
|
|
|
|
vnode.appContext = context
|
|
|
|
render(vnode, rootContainer)
|
|
|
|
isMounted = true
|
2019-09-06 16:58:31 +00:00
|
|
|
return (vnode.component as ComponentInternalInstance).renderProxy
|
2019-09-03 22:11:04 +00:00
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(
|
|
|
|
`App has already been mounted. Create a new app instance instead.`
|
|
|
|
)
|
|
|
|
}
|
2019-09-02 20:09:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
provide(key, value) {
|
|
|
|
if (__DEV__ && key in context.provides) {
|
|
|
|
warn(
|
|
|
|
`App already provides property with key "${key}". ` +
|
|
|
|
`It will be overwritten with the new value.`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
context.provides[key as any] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
}
|