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

245 lines
6.9 KiB
TypeScript
Raw Normal View History

import {
Component,
Data,
validateComponentName,
PublicAPIComponent
} from './component'
import { ComponentOptions } from './apiOptions'
import { ComponentPublicInstance } from './componentProxy'
import { Directive, validateDirectiveName } from './directives'
2019-11-02 16:18:35 +00:00
import { RootRenderFunction } from './renderer'
2019-09-02 20:09:34 +00:00
import { InjectionKey } from './apiInject'
import { isFunction, NO, isObject } from '@vue/shared'
2019-09-02 20:09:34 +00:00
import { warn } from './warning'
2020-02-14 04:31:03 +00:00
import { createVNode, cloneVNode, VNode } from './vnode'
2019-09-02 20:09:34 +00:00
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
2019-09-02 20:09:34 +00: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
2020-02-14 04:31:03 +00:00
mount(
rootContainer: HostElement | string,
isHydrate?: boolean
): 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-02 20:09:34 +00:00
}
export interface AppConfig {
devtools: boolean
performance: boolean
readonly isNativeTag?: (tag: string) => boolean
isCustomElement?: (tag: string) => boolean
2019-09-02 20:09:34 +00:00
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>
2019-12-22 17:25:04 +00:00
reload?: () => void // HMR only
2019-09-02 20:09:34 +00:00
}
type PluginInstallFunction = (app: App, ...options: any[]) => any
2019-09-02 20:09:34 +00:00
2019-09-03 22:11:04 +00:00
export type Plugin =
| PluginInstallFunction & { install?: PluginInstallFunction }
2019-09-02 20:09:34 +00:00
| {
install: PluginInstallFunction
}
export function createAppContext(): AppContext {
return {
config: {
devtools: true,
performance: false,
isNativeTag: NO,
isCustomElement: NO,
2019-09-02 20:09:34 +00:00
errorHandler: undefined,
2019-09-02 20:43:26 +00:00
warnHandler: undefined
2019-09-02 20:09:34 +00:00
},
mixins: [],
components: {},
directives: {},
provides: {}
}
}
export type CreateAppFunction<HostElement> = (
rootComponent: PublicAPIComponent,
rootProps?: Data | null
) => App<HostElement>
2019-09-06 20:58:32 +00:00
export function createAppAPI<HostNode, HostElement>(
2020-02-14 04:31:03 +00:00
render: RootRenderFunction<HostNode, HostElement>,
hydrate?: (vnode: VNode, container: Element) => void
): 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-02 20:09:34 +00:00
const context = createAppContext()
const installedPlugins = new Set()
2019-09-02 20:09:34 +00:00
2019-09-03 22:11:04 +00:00
let isMounted = false
2019-09-02 20:09:34 +00:00
const app: App = {
_component: rootComponent,
_props: rootProps,
_container: null,
_context: context,
2019-09-02 20:09:34 +00:00
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, ...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-02 20:09:34 +00:00
} else if (__DEV__) {
warn(
`A plugin must either be a function or an object with an "install" ` +
`function.`
)
}
return app
},
mixin(mixin: ComponentOptions) {
2020-02-15 16:40:09 +00:00
if (__FEATURE_OPTIONS__) {
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-10-28 20:22:03 +00:00
} else if (__DEV__) {
2020-02-15 16:40:09 +00:00
warn('Mixins are only available in builds supporting Options API')
2019-10-28 20:22:03 +00:00
}
2019-09-02 20:09:34 +00:00
return app
},
component(name: string, component?: PublicAPIComponent): any {
if (__DEV__) {
validateComponentName(name, context.config)
}
2019-09-02 20:09:34 +00:00
if (!component) {
return context.components[name]
2019-09-02 20:09:34 +00:00
}
if (__DEV__ && context.components[name]) {
warn(`Component "${name}" has already been registered in target app.`)
}
context.components[name] = component as Component
return app
2019-09-02 20:09:34 +00:00
},
directive(name: string, directive?: Directive) {
if (__DEV__) {
validateDirectiveName(name)
}
2019-09-02 20:09:34 +00: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-02 20:09:34 +00:00
},
2020-02-14 04:31:03 +00:00
mount(rootContainer: HostElement, isHydrate?: boolean): 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
2019-12-22 17:25:04 +00:00
// HMR root reload
if (__BUNDLER__ && __DEV__) {
context.reload = () => {
render(cloneVNode(vnode), rootContainer)
}
}
if (isHydrate && hydrate) {
2020-02-14 04:31:03 +00:00
hydrate(vnode, rootContainer as any)
} else {
render(vnode, rootContainer)
}
2019-09-03 22:11:04 +00:00
isMounted = true
app._container = rootContainer
return vnode.component!.proxy
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
},
unmount() {
if (isMounted) {
render(null, app._container)
} else if (__DEV__) {
warn(`Cannot unmount an app that is not mounted.`)
}
},
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.`
)
}
2019-10-08 16:43:13 +00: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-02 20:09:34 +00:00
}
}
return app
}
}