2020-02-16 10:04:29 +08:00
|
|
|
import {
|
2020-08-20 04:11:29 +08:00
|
|
|
ConcreteComponent,
|
2020-02-16 10:04:29 +08:00
|
|
|
Data,
|
|
|
|
validateComponentName,
|
2021-06-08 05:23:45 +08:00
|
|
|
Component,
|
2021-09-22 00:55:08 +08:00
|
|
|
ComponentInternalInstance,
|
|
|
|
getExposeProxy
|
2020-02-16 10:04:29 +08:00
|
|
|
} from './component'
|
2021-06-03 02:37:27 +08:00
|
|
|
import {
|
|
|
|
ComponentOptions,
|
|
|
|
MergedComponentOptions,
|
|
|
|
RuntimeCompilerOptions
|
|
|
|
} from './componentOptions'
|
2020-08-20 04:11:29 +08:00
|
|
|
import { ComponentPublicInstance } from './componentPublicInstance'
|
2019-10-19 00:34:45 +08:00
|
|
|
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 { warn } from './warning'
|
2020-02-14 12:31:03 +08:00
|
|
|
import { createVNode, cloneVNode, VNode } from './vnode'
|
2020-03-23 23:08:22 +08:00
|
|
|
import { RootHydrateFunction } from './hydration'
|
2020-07-21 09:51:30 +08:00
|
|
|
import { devtoolsInitApp, devtoolsUnmountApp } from './devtools'
|
2021-04-06 05:09:22 +08:00
|
|
|
import { isFunction, NO, isObject } from '@vue/shared'
|
2020-06-26 21:03:55 +08:00
|
|
|
import { version } from '.'
|
2021-05-07 05:15:11 +08:00
|
|
|
import { installAppCompatProperties } from './compat/global'
|
2021-06-03 03:22:52 +08:00
|
|
|
import { NormalizedPropsOptions } from './componentProps'
|
|
|
|
import { ObjectEmitsOptions } from './componentEmits'
|
2019-09-03 04:09:34 +08:00
|
|
|
|
2019-09-07 04:58:32 +08:00
|
|
|
export interface App<HostElement = any> {
|
2020-06-26 21:03:55 +08:00
|
|
|
version: string
|
2019-09-03 04:09:34 +08:00
|
|
|
config: AppConfig
|
2019-12-24 23:33:47 +08:00
|
|
|
use(plugin: Plugin, ...options: any[]): this
|
2019-09-03 04:09:34 +08:00
|
|
|
mixin(mixin: ComponentOptions): this
|
2020-08-20 04:11:29 +08:00
|
|
|
component(name: string): Component | undefined
|
|
|
|
component(name: string, component: Component): this
|
2019-09-03 04:09:34 +08:00
|
|
|
directive(name: string): Directive | undefined
|
|
|
|
directive(name: string, directive: Directive): this
|
2020-02-14 12:31:03 +08:00
|
|
|
mount(
|
|
|
|
rootContainer: HostElement | string,
|
2021-03-02 00:51:32 +08:00
|
|
|
isHydrate?: boolean,
|
|
|
|
isSVG?: boolean
|
2020-02-14 12:31:03 +08:00
|
|
|
): ComponentPublicInstance
|
2021-02-04 02:09:59 +08:00
|
|
|
unmount(): void
|
2019-10-28 08:54:33 +08:00
|
|
|
provide<T>(key: InjectionKey<T> | string, value: T): this
|
2020-01-24 10:01:56 +08:00
|
|
|
|
2020-07-21 09:51:30 +08:00
|
|
|
// internal, but we need to expose these for the server-renderer and devtools
|
2020-09-01 06:32:07 +08:00
|
|
|
_uid: number
|
2020-08-20 04:11:29 +08:00
|
|
|
_component: ConcreteComponent
|
2020-01-24 10:01:56 +08:00
|
|
|
_props: Data | null
|
|
|
|
_container: HostElement | null
|
2020-01-31 01:20:23 +08:00
|
|
|
_context: AppContext
|
2021-06-08 05:23:45 +08:00
|
|
|
_instance: ComponentInternalInstance | null
|
2021-04-05 23:54:35 +08:00
|
|
|
|
|
|
|
/**
|
2021-04-20 00:08:26 +08:00
|
|
|
* v2 compat only
|
|
|
|
*/
|
|
|
|
filter?(name: string): Function | undefined
|
|
|
|
filter?(name: string, filter: Function): this
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal v3 compat only
|
2021-04-05 23:54:35 +08:00
|
|
|
*/
|
|
|
|
_createRoot?(options: ComponentOptions): ComponentPublicInstance
|
2019-09-03 04:09:34 +08:00
|
|
|
}
|
|
|
|
|
2021-06-02 22:37:50 +08:00
|
|
|
export type OptionMergeFunction = (to: unknown, from: unknown) => any
|
2020-03-24 23:59:00 +08:00
|
|
|
|
2019-09-03 04:09:34 +08:00
|
|
|
export interface AppConfig {
|
2020-03-24 23:59:00 +08:00
|
|
|
// @private
|
|
|
|
readonly isNativeTag?: (tag: string) => boolean
|
|
|
|
|
2019-09-03 04:09:34 +08:00
|
|
|
performance: boolean
|
2020-03-24 23:59:00 +08:00
|
|
|
optionMergeStrategies: Record<string, OptionMergeFunction>
|
2020-03-25 21:28:37 +08:00
|
|
|
globalProperties: Record<string, any>
|
2019-09-03 04:09:34 +08:00
|
|
|
errorHandler?: (
|
2020-03-10 03:58:52 +08:00
|
|
|
err: unknown,
|
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
|
2021-04-26 23:46:29 +08:00
|
|
|
|
2021-07-28 05:52:31 +08:00
|
|
|
/**
|
|
|
|
* Options to pass to @vue/compiler-dom.
|
|
|
|
* Only supported in runtime compiler build.
|
|
|
|
*/
|
|
|
|
compilerOptions: RuntimeCompilerOptions
|
|
|
|
|
2021-04-26 23:46:29 +08:00
|
|
|
/**
|
|
|
|
* @deprecated use config.compilerOptions.isCustomElement
|
|
|
|
*/
|
|
|
|
isCustomElement?: (tag: string) => boolean
|
|
|
|
|
|
|
|
/**
|
2021-07-28 05:52:31 +08:00
|
|
|
* Temporary config for opt-in to unwrap injected refs.
|
|
|
|
* TODO deprecate in 3.3
|
2021-04-26 23:46:29 +08:00
|
|
|
*/
|
2021-07-28 05:52:31 +08:00
|
|
|
unwrapInjectedRef?: boolean
|
2019-09-03 04:09:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface AppContext {
|
2020-07-21 09:51:30 +08:00
|
|
|
app: App // for devtools
|
2019-09-03 04:09:34 +08:00
|
|
|
config: AppConfig
|
|
|
|
mixins: ComponentOptions[]
|
2020-08-20 04:11:29 +08:00
|
|
|
components: Record<string, Component>
|
2019-09-03 04:09:34 +08:00
|
|
|
directives: Record<string, Directive>
|
|
|
|
provides: Record<string | symbol, any>
|
2021-06-02 22:37:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache for merged/normalized component options
|
|
|
|
* Each app instance has its own cache because app-level global mixins and
|
|
|
|
* optionMergeStrategies can affect merge behavior.
|
2021-06-03 03:22:52 +08:00
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
optionsCache: WeakMap<ComponentOptions, MergedComponentOptions>
|
|
|
|
/**
|
|
|
|
* Cache for normalized props options
|
|
|
|
* @internal
|
2021-06-02 22:37:50 +08:00
|
|
|
*/
|
2021-06-03 03:22:52 +08:00
|
|
|
propsCache: WeakMap<ConcreteComponent, NormalizedPropsOptions>
|
2020-10-07 03:31:29 +08:00
|
|
|
/**
|
2021-06-03 03:22:52 +08:00
|
|
|
* Cache for normalized emits options
|
2020-10-07 03:31:29 +08:00
|
|
|
* @internal
|
|
|
|
*/
|
2021-06-03 03:22:52 +08:00
|
|
|
emitsCache: WeakMap<ConcreteComponent, ObjectEmitsOptions | null>
|
2020-10-07 03:31:29 +08:00
|
|
|
/**
|
|
|
|
* HMR only
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
reload?: () => void
|
2021-04-20 00:08:26 +08:00
|
|
|
/**
|
|
|
|
* v2 compat only
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
filters?: Record<string, Function>
|
2019-09-03 04:09:34 +08:00
|
|
|
}
|
|
|
|
|
2019-12-24 23:33:47 +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 =
|
2021-07-20 06:24:18 +08:00
|
|
|
| (PluginInstallFunction & { install?: PluginInstallFunction })
|
2019-09-03 04:09:34 +08:00
|
|
|
| {
|
|
|
|
install: PluginInstallFunction
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createAppContext(): AppContext {
|
|
|
|
return {
|
2020-07-21 09:51:30 +08:00
|
|
|
app: null as any,
|
2019-09-03 04:09:34 +08:00
|
|
|
config: {
|
2020-03-24 23:59:00 +08:00
|
|
|
isNativeTag: NO,
|
2019-09-03 04:09:34 +08:00
|
|
|
performance: false,
|
2020-03-25 21:28:37 +08:00
|
|
|
globalProperties: {},
|
2020-03-24 23:59:00 +08:00
|
|
|
optionMergeStrategies: {},
|
2019-09-03 04:09:34 +08:00
|
|
|
errorHandler: undefined,
|
2021-04-26 23:46:29 +08:00
|
|
|
warnHandler: undefined,
|
2021-04-29 00:36:08 +08:00
|
|
|
compilerOptions: {}
|
2019-09-03 04:09:34 +08:00
|
|
|
},
|
|
|
|
mixins: [],
|
|
|
|
components: {},
|
|
|
|
directives: {},
|
2021-06-02 22:37:50 +08:00
|
|
|
provides: Object.create(null),
|
2021-06-03 03:22:52 +08:00
|
|
|
optionsCache: new WeakMap(),
|
|
|
|
propsCache: new WeakMap(),
|
|
|
|
emitsCache: new WeakMap()
|
2019-09-03 04:09:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
export type CreateAppFunction<HostElement> = (
|
2020-08-20 04:11:29 +08:00
|
|
|
rootComponent: Component,
|
2020-01-24 04:05:38 +08:00
|
|
|
rootProps?: Data | null
|
|
|
|
) => App<HostElement>
|
|
|
|
|
2020-09-01 06:32:07 +08:00
|
|
|
let uid = 0
|
|
|
|
|
2020-03-23 23:08:22 +08:00
|
|
|
export function createAppAPI<HostElement>(
|
|
|
|
render: RootRenderFunction,
|
|
|
|
hydrate?: RootHydrateFunction
|
2020-01-24 04:05:38 +08:00
|
|
|
): CreateAppFunction<HostElement> {
|
2020-03-23 23:08:22 +08:00
|
|
|
return function createApp(rootComponent, rootProps = null) {
|
2020-01-24 10:01:56 +08:00
|
|
|
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()
|
2019-11-05 23:49:00 +08:00
|
|
|
const installedPlugins = new Set()
|
2019-09-03 04:09:34 +08:00
|
|
|
|
2019-09-04 06:11:04 +08:00
|
|
|
let isMounted = false
|
|
|
|
|
2020-07-21 09:51:30 +08:00
|
|
|
const app: App = (context.app = {
|
2020-09-01 06:32:07 +08:00
|
|
|
_uid: uid++,
|
2020-08-20 04:11:29 +08:00
|
|
|
_component: rootComponent as ConcreteComponent,
|
2020-01-24 10:01:56 +08:00
|
|
|
_props: rootProps,
|
|
|
|
_container: null,
|
2020-01-31 01:20:23 +08:00
|
|
|
_context: context,
|
2021-06-08 05:23:45 +08:00
|
|
|
_instance: null,
|
2020-01-24 04:05:38 +08:00
|
|
|
|
2020-06-26 21:03:55 +08:00
|
|
|
version,
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2019-12-24 23:33:47 +08:00
|
|
|
use(plugin: Plugin, ...options: any[]) {
|
2019-11-05 23:49:00 +08:00
|
|
|
if (installedPlugins.has(plugin)) {
|
|
|
|
__DEV__ && warn(`Plugin has already been applied to target app.`)
|
2019-11-27 22:18:03 +08:00
|
|
|
} else if (plugin && isFunction(plugin.install)) {
|
2019-11-05 23:49:00 +08:00
|
|
|
installedPlugins.add(plugin)
|
2019-12-24 23:33:47 +08:00
|
|
|
plugin.install(app, ...options)
|
2020-01-09 01:40:24 +08:00
|
|
|
} 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) {
|
2020-07-21 09:51:30 +08:00
|
|
|
if (__FEATURE_OPTIONS_API__) {
|
2020-02-16 00:40:09 +08:00
|
|
|
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-29 04:22:03 +08:00
|
|
|
} else if (__DEV__) {
|
2020-02-16 00:40:09 +08:00
|
|
|
warn('Mixins are only available in builds supporting Options API')
|
2019-10-29 04:22:03 +08:00
|
|
|
}
|
2019-09-03 04:09:34 +08:00
|
|
|
return app
|
|
|
|
},
|
|
|
|
|
2020-08-20 04:11:29 +08:00
|
|
|
component(name: string, component?: Component): any {
|
2019-10-15 03:36:30 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
validateComponentName(name, context.config)
|
|
|
|
}
|
2019-09-03 04:09:34 +08:00
|
|
|
if (!component) {
|
2019-10-11 02:54:06 +08:00
|
|
|
return context.components[name]
|
2019-09-03 04:09:34 +08:00
|
|
|
}
|
2019-11-27 22:18:03 +08:00
|
|
|
if (__DEV__ && context.components[name]) {
|
|
|
|
warn(`Component "${name}" has already been registered in target app.`)
|
|
|
|
}
|
2020-03-12 22:19:30 +08:00
|
|
|
context.components[name] = component
|
2019-11-27 22:18:03 +08:00
|
|
|
return app
|
2019-09-03 04:09:34 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
directive(name: string, directive?: Directive) {
|
2019-10-19 00:34:45 +08:00
|
|
|
if (__DEV__) {
|
|
|
|
validateDirectiveName(name)
|
|
|
|
}
|
|
|
|
|
2019-09-03 04:09:34 +08:00
|
|
|
if (!directive) {
|
|
|
|
return context.directives[name] as any
|
|
|
|
}
|
2019-11-27 22:18:03 +08:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2021-03-02 00:51:32 +08:00
|
|
|
mount(
|
|
|
|
rootContainer: HostElement,
|
|
|
|
isHydrate?: boolean,
|
|
|
|
isSVG?: boolean
|
|
|
|
): any {
|
2019-09-04 06:11:04 +08:00
|
|
|
if (!isMounted) {
|
2020-08-20 04:11:29 +08:00
|
|
|
const vnode = createVNode(
|
|
|
|
rootComponent as ConcreteComponent,
|
|
|
|
rootProps
|
|
|
|
)
|
2019-09-04 06:11:04 +08:00
|
|
|
// 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
|
2020-04-20 12:34:53 +08:00
|
|
|
if (__DEV__) {
|
2019-12-23 01:25:04 +08:00
|
|
|
context.reload = () => {
|
2021-03-02 00:51:32 +08:00
|
|
|
render(cloneVNode(vnode), rootContainer, isSVG)
|
2019-12-23 01:25:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 14:30:08 +08:00
|
|
|
if (isHydrate && hydrate) {
|
2020-03-23 23:08:22 +08:00
|
|
|
hydrate(vnode as VNode<Node, Element>, rootContainer as any)
|
2020-02-14 12:31:03 +08:00
|
|
|
} else {
|
2021-03-02 00:51:32 +08:00
|
|
|
render(vnode, rootContainer, isSVG)
|
2020-02-14 12:31:03 +08:00
|
|
|
}
|
2019-09-04 06:11:04 +08:00
|
|
|
isMounted = true
|
2020-01-24 10:01:56 +08:00
|
|
|
app._container = rootContainer
|
2020-07-21 09:51:30 +08:00
|
|
|
// for devtools and telemetry
|
|
|
|
;(rootContainer as any).__vue_app__ = app
|
2020-07-17 06:18:52 +08:00
|
|
|
|
2020-07-21 09:51:30 +08:00
|
|
|
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
|
2021-06-08 05:23:45 +08:00
|
|
|
app._instance = vnode.component
|
2020-07-21 09:51:30 +08:00
|
|
|
devtoolsInitApp(app, version)
|
|
|
|
}
|
2020-07-17 06:18:52 +08:00
|
|
|
|
2021-09-22 00:55:08 +08:00
|
|
|
return getExposeProxy(vnode.component!) || vnode.component!.proxy
|
2019-09-04 06:11:04 +08:00
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(
|
2020-06-12 05:20:38 +08:00
|
|
|
`App has already been mounted.\n` +
|
|
|
|
`If you want to remount the same app, move your app creation logic ` +
|
|
|
|
`into a factory function and create fresh app instances for each ` +
|
|
|
|
`mount - e.g. \`const createMyApp = () => createApp(App)\``
|
2019-09-04 06:11:04 +08:00
|
|
|
)
|
|
|
|
}
|
2019-09-03 04:09:34 +08:00
|
|
|
},
|
|
|
|
|
2020-01-24 04:05:38 +08:00
|
|
|
unmount() {
|
|
|
|
if (isMounted) {
|
2020-02-16 00:33:22 +08:00
|
|
|
render(null, app._container)
|
2020-08-22 22:34:18 +08:00
|
|
|
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
|
2021-06-08 05:23:45 +08:00
|
|
|
app._instance = null
|
2020-08-22 22:34:18 +08:00
|
|
|
devtoolsUnmountApp(app)
|
|
|
|
}
|
2021-02-04 02:09:20 +08:00
|
|
|
delete app._container.__vue_app__
|
2020-01-24 04:05:38 +08:00
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(`Cannot unmount an app that is not mounted.`)
|
|
|
|
}
|
2020-01-17 01:23:47 +08:00
|
|
|
},
|
|
|
|
|
2019-09-03 04:09:34 +08:00
|
|
|
provide(key, value) {
|
2020-08-21 05:48:28 +08:00
|
|
|
if (__DEV__ && (key as string | symbol) in context.provides) {
|
2019-09-03 04:09:34 +08:00
|
|
|
warn(
|
2020-05-02 22:26:32 +08:00
|
|
|
`App already provides property with key "${String(key)}". ` +
|
2019-09-03 04:09:34 +08:00
|
|
|
`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
|
2019-10-28 08:54:33 +08:00
|
|
|
|
|
|
|
return app
|
2019-09-03 04:09:34 +08:00
|
|
|
}
|
2020-07-21 09:51:30 +08:00
|
|
|
})
|
2020-07-17 06:18:52 +08:00
|
|
|
|
2021-04-05 23:54:35 +08:00
|
|
|
if (__COMPAT__) {
|
2021-05-07 05:15:11 +08:00
|
|
|
installAppCompatProperties(app, context, render)
|
2021-04-05 23:54:35 +08:00
|
|
|
}
|
|
|
|
|
2019-09-03 04:09:34 +08:00
|
|
|
return app
|
|
|
|
}
|
|
|
|
}
|