vue3-core/packages/runtime-core/src/apiCreateApp.ts

236 lines
6.7 KiB
TypeScript
Raw Normal View History

import { Component, Data, validateComponentName } from './component'
import { ComponentOptions } from './apiOptions'
import { ComponentPublicInstance } from './componentProxy'
import { Directive, validateDirectiveName } from './directives'
2019-11-03 00:18:35 +08:00
import { RootRenderFunction } from './renderer'
2019-09-03 04:09:34 +08:00
import { InjectionKey } from './apiInject'
import { isFunction, NO, isObject } from '@vue/shared'
2019-09-03 04:09:34 +08:00
import { warn } from './warning'
2019-12-23 01:25:04 +08:00
import { createVNode, cloneVNode } from './vnode'
2019-09-03 04:09:34 +08:00
2019-09-07 04:58:32 +08:00
export interface App<HostElement = any> {
2019-09-03 04:09:34 +08:00
config: AppConfig
use(plugin: Plugin, ...options: any[]): this
2019-09-03 04:09:34 +08:00
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(rootContainer: HostElement | string): ComponentPublicInstance
unmount(rootContainer: HostElement | string): void
provide<T>(key: InjectionKey<T> | string, value: T): this
// internal. We need to expose these for the server-renderer
_component: Component
_props: Data | null
_container: HostElement | null
_context: AppContext
2019-09-03 04:09:34 +08:00
}
export interface AppConfig {
devtools: boolean
performance: boolean
readonly isNativeTag?: (tag: string) => boolean
isCustomElement?: (tag: string) => boolean
2019-09-03 04:09:34 +08:00
errorHandler?: (
err: Error,
2019-09-07 00:58:31 +08:00
instance: ComponentPublicInstance | null,
2019-09-03 04:09:34 +08:00
info: string
) => void
warnHandler?: (
msg: string,
2019-09-07 00:58:31 +08:00
instance: ComponentPublicInstance | null,
2019-09-03 04:09:34 +08:00
trace: string
) => void
}
export interface AppContext {
config: AppConfig
mixins: ComponentOptions[]
components: Record<string, Component>
directives: Record<string, Directive>
provides: Record<string | symbol, any>
2019-12-23 01:25:04 +08:00
reload?: () => void // HMR only
2019-09-03 04:09:34 +08:00
}
type PluginInstallFunction = (app: App, ...options: any[]) => any
2019-09-03 04:09:34 +08:00
2019-09-04 06:11:04 +08:00
export type Plugin =
| PluginInstallFunction & { install?: PluginInstallFunction }
2019-09-03 04:09:34 +08:00
| {
install: PluginInstallFunction
}
export function createAppContext(): AppContext {
return {
config: {
devtools: true,
performance: false,
isNativeTag: NO,
isCustomElement: NO,
2019-09-03 04:09:34 +08:00
errorHandler: undefined,
2019-09-03 04:43:26 +08:00
warnHandler: undefined
2019-09-03 04:09:34 +08:00
},
mixins: [],
components: {},
directives: {},
provides: {}
}
}
export type CreateAppFunction<HostElement> = (
rootComponent:
| Component
// for compatibility with defineComponent() return types
| { new (): ComponentPublicInstance<any, any, any, any, any> },
rootProps?: Data | null
) => App<HostElement>
2019-09-07 04:58:32 +08:00
export function createAppAPI<HostNode, HostElement>(
render: RootRenderFunction<HostNode, HostElement>
): CreateAppFunction<HostElement> {
return function createApp(rootComponent: Component, rootProps = null) {
if (rootProps != null && !isObject(rootProps)) {
__DEV__ && warn(`root props passed to app.mount() must be an object.`)
rootProps = null
}
2019-09-03 04:09:34 +08:00
const context = createAppContext()
const installedPlugins = new Set()
2019-09-03 04:09:34 +08:00
2019-09-04 06:11:04 +08:00
let isMounted = false
2019-09-03 04:09:34 +08:00
const app: App = {
_component: rootComponent,
_props: rootProps,
_container: null,
_context: context,
2019-09-03 04:09:34 +08:00
get config() {
return context.config
},
set config(v) {
2019-09-03 04:16:08 +08:00
if (__DEV__) {
warn(
`app.config cannot be replaced. Modify individual options instead.`
)
}
2019-09-03 04:09:34 +08:00
},
use(plugin: Plugin, ...options: any[]) {
if (installedPlugins.has(plugin)) {
__DEV__ && warn(`Plugin has already been applied to target app.`)
} else if (plugin && isFunction(plugin.install)) {
installedPlugins.add(plugin)
plugin.install(app, ...options)
} else if (isFunction(plugin)) {
installedPlugins.add(plugin)
plugin(app, ...options)
2019-09-03 04:09:34 +08:00
} else if (__DEV__) {
warn(
`A plugin must either be a function or an object with an "install" ` +
`function.`
)
}
return app
},
mixin(mixin: ComponentOptions) {
2019-10-29 04:22:03 +08:00
if (__DEV__ && !__FEATURE_OPTIONS__) {
warn('Mixins are only available in builds supporting Options API')
}
if (!context.mixins.includes(mixin)) {
context.mixins.push(mixin)
} else if (__DEV__) {
warn(
'Mixin has already been applied to target app' +
(mixin.name ? `: ${mixin.name}` : '')
)
}
2019-09-03 04:09:34 +08:00
return app
},
component(name: string, component?: Component): any {
if (__DEV__) {
validateComponentName(name, context.config)
}
2019-09-03 04:09:34 +08:00
if (!component) {
return context.components[name]
2019-09-03 04:09:34 +08:00
}
if (__DEV__ && context.components[name]) {
warn(`Component "${name}" has already been registered in target app.`)
}
context.components[name] = component
return app
2019-09-03 04:09:34 +08:00
},
directive(name: string, directive?: Directive) {
if (__DEV__) {
validateDirectiveName(name)
}
2019-09-03 04:09:34 +08:00
if (!directive) {
return context.directives[name] as any
}
if (__DEV__ && context.directives[name]) {
warn(`Directive "${name}" has already been registered in target app.`)
}
context.directives[name] = directive
return app
2019-09-03 04:09:34 +08:00
},
mount(rootContainer: HostElement): any {
2019-09-04 06:11:04 +08: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
2019-12-23 01:25:04 +08:00
// HMR root reload
if (__BUNDLER__ && __DEV__) {
context.reload = () => {
render(cloneVNode(vnode), rootContainer)
}
}
2019-09-04 06:11:04 +08:00
render(vnode, rootContainer)
isMounted = true
app._container = rootContainer
return vnode.component!.proxy
2019-09-04 06:11:04 +08:00
} else if (__DEV__) {
warn(
`App has already been mounted. Create a new app instance instead.`
)
}
2019-09-03 04:09:34 +08:00
},
unmount() {
if (isMounted) {
render(null, app._container!)
} else if (__DEV__) {
warn(`Cannot unmount an app that is not mounted.`)
}
},
2019-09-03 04:09:34 +08: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.`
)
}
2019-10-09 00:43:13 +08:00
// TypeScript doesn't allow symbols as index type
// https://github.com/Microsoft/TypeScript/issues/24587
context.provides[key as string] = value
return app
2019-09-03 04:09:34 +08:00
}
}
return app
}
}