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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

511 lines
14 KiB
TypeScript
Raw Normal View History

import {
type Component,
type ConcreteComponent,
2024-12-04 13:50:54 +08:00
type Data,
2024-12-04 11:54:26 +08:00
type GenericComponent,
type GenericComponentInstance,
validateComponentName,
} from './component'
import type {
ComponentOptions,
MergedComponentOptions,
RuntimeCompilerOptions,
} from './componentOptions'
import type {
ComponentCustomProperties,
ComponentPublicInstance,
} from './componentPublicInstance'
import { type Directive, validateDirectiveName } from './directives'
2025-02-04 21:38:09 +08:00
import type {
ElementNamespace,
RootRenderFunction,
UnmountComponentFn,
} from './renderer'
2019-09-03 04:09:34 +08:00
import type { InjectionKey } from './apiInject'
import { warn } from './warning'
2024-12-04 11:54:26 +08:00
import type { VNode } from './vnode'
import { devtoolsInitApp, devtoolsUnmountApp } from './devtools'
2024-05-11 23:14:26 +08:00
import { NO, extend, isFunction, isObject } from '@vue/shared'
import { version } from '.'
import { installAppCompatProperties } from './compat/global'
import type { NormalizedPropsOptions } from './componentProps'
import type { ObjectEmitsOptions } from './componentEmits'
import { ErrorCodes, callWithAsyncErrorHandling } from './errorHandling'
import type { DefineComponent } from './apiDefineComponent'
2025-02-04 21:38:09 +08:00
import type { VaporInteropInterface } from './vaporInterop'
2019-09-03 04:09:34 +08:00
2019-09-07 04:58:32 +08:00
export interface App<HostElement = any> {
version: string
2019-09-03 04:09:34 +08:00
config: AppConfig
use<Options extends unknown[]>(
plugin: Plugin<Options>,
...options: Options
): this
use<Options>(plugin: Plugin<Options>, options: Options): this
2019-09-03 04:09:34 +08:00
mixin(mixin: ComponentOptions): this
component(name: string): Component | undefined
component<T extends Component | DefineComponent>(
name: string,
component: T,
): this
directive<
HostElement = any,
Value = any,
Modifiers extends string = string,
Arg extends string = string,
>(
name: string,
): Directive<HostElement, Value, Modifiers, Arg> | undefined
directive<
HostElement = any,
Value = any,
Modifiers extends string = string,
Arg extends string = string,
>(
name: string,
directive: Directive<HostElement, Value, Modifiers, Arg>,
): this
2020-02-14 12:31:03 +08:00
mount(
rootContainer: HostElement | string,
/**
* @internal
*/
isHydrate?: boolean,
/**
* @internal
*/
namespace?: boolean | ElementNamespace,
/**
* @internal
*/
vnode?: VNode,
2020-02-14 12:31:03 +08:00
): ComponentPublicInstance
unmount(): void
onUnmount(cb: () => void): void
provide<T, K = InjectionKey<T> | string | number>(
key: K,
value: K extends InjectionKey<infer V> ? V : T,
): this
/**
* Runs a function with the app as active instance. This allows using of `inject()` within the function to get access
* to variables provided via `app.provide()`.
*
* @param fn - function to run with the app as active instance
*/
runWithContext<T>(fn: () => T): T
// internal, but we need to expose these for the server-renderer and devtools
_uid: number
2024-12-04 11:54:26 +08:00
_component: GenericComponent
_props: Data | null
_container: HostElement | null
_context: AppContext
2024-12-04 11:54:26 +08:00
_instance: GenericComponentInstance | null
2021-04-05 23:54:35 +08:00
/**
* @internal custom element vnode
*/
_ceVNode?: VNode
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
}
export type OptionMergeFunction = (to: unknown, from: unknown) => any
2024-12-04 10:53:29 +08:00
/**
* Shared app config between vdom and vapor
*/
export interface GenericAppConfig {
performance?: boolean
2019-09-03 04:09:34 +08:00
errorHandler?: (
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
/**
* TODO document for 3.5
* Enable warnings for computed getters that recursively trigger itself.
*/
warnRecursiveComputed?: boolean
/**
* Whether to throw unhandled errors in production.
* Default is `false` to avoid crashing on any error (and only logs it)
* But in some cases, e.g. SSR, throwing might be more desirable.
*/
throwUnhandledErrorInProduction?: boolean
2024-07-19 18:06:02 +08:00
/**
* Prefix for all useId() calls within this app
*/
idPrefix?: string
2019-09-03 04:09:34 +08:00
}
2024-12-04 10:53:29 +08:00
export interface AppConfig extends GenericAppConfig {
// @private
readonly isNativeTag: (tag: string) => boolean
optionMergeStrategies: Record<string, OptionMergeFunction>
globalProperties: ComponentCustomProperties & Record<string, any>
/**
* Options to pass to `@vue/compiler-dom`.
* Only supported in runtime compiler build.
*/
compilerOptions: RuntimeCompilerOptions
/**
* @deprecated use config.compilerOptions.isCustomElement
*/
isCustomElement?: (tag: string) => boolean
}
/**
* Minimal app context shared between vdom and vapor
*/
export interface GenericAppContext {
app: App // for devtools
2024-12-04 10:53:29 +08:00
config: GenericAppConfig
provides: Record<string | symbol, any>
components?: Record<string, Component>
directives?: Record<string, Directive>
/**
* HMR only
* @internal
*/
reload?: () => void
2025-02-04 21:38:09 +08:00
/**
* @internal vapor interop only, for creating vapor components in vdom
*/
vapor?: VaporInteropInterface
/**
* @internal vapor interop only, for creating vdom components in vapor
*/
vdomMount?: (component: ConcreteComponent, props?: any, slots?: any) => any
/**
* @internal
*/
vdomUnmount?: UnmountComponentFn
2024-12-04 10:53:29 +08:00
}
export interface AppContext extends GenericAppContext {
2019-09-03 04:09:34 +08:00
config: AppConfig
components: Record<string, Component>
2019-09-03 04:09:34 +08:00
directives: Record<string, Directive>
2024-12-04 10:53:29 +08:00
mixins: ComponentOptions[]
/**
* Cache for merged/normalized component options
* Each app instance has its own cache because app-level global mixins and
* optionMergeStrategies can affect merge behavior.
* @internal
*/
optionsCache: WeakMap<ComponentOptions, MergedComponentOptions>
/**
* Cache for normalized props options
* @internal
*/
propsCache: WeakMap<ConcreteComponent, NormalizedPropsOptions>
/**
* Cache for normalized emits options
* @internal
*/
emitsCache: WeakMap<ConcreteComponent, ObjectEmitsOptions | null>
2021-04-20 00:08:26 +08:00
/**
* v2 compat only
* @internal
*/
filters?: Record<string, Function>
2019-09-03 04:09:34 +08:00
}
type PluginInstallFunction<Options = any[]> = Options extends unknown[]
? (app: App, ...options: Options) => any
: (app: App, options: Options) => any
2019-09-03 04:09:34 +08:00
export type ObjectPlugin<Options = any[]> = {
install: PluginInstallFunction<Options>
}
export type FunctionPlugin<Options = any[]> = PluginInstallFunction<Options> &
Partial<ObjectPlugin<Options>>
export type Plugin<Options = any[]> =
| FunctionPlugin<Options>
| ObjectPlugin<Options>
2019-09-03 04:09:34 +08:00
export function createAppContext(): AppContext {
return {
app: null as any,
2019-09-03 04:09:34 +08:00
config: {
isNativeTag: NO,
2019-09-03 04:09:34 +08:00
performance: false,
globalProperties: {},
optionMergeStrategies: {},
2019-09-03 04:09:34 +08:00
errorHandler: undefined,
warnHandler: undefined,
2021-04-29 00:36:08 +08:00
compilerOptions: {},
2019-09-03 04:09:34 +08:00
},
mixins: [],
components: {},
directives: {},
provides: Object.create(null),
optionsCache: new WeakMap(),
propsCache: new WeakMap(),
emitsCache: new WeakMap(),
2019-09-03 04:09:34 +08:00
}
}
2024-12-04 13:50:54 +08:00
export type CreateAppFunction<HostElement, Comp = Component> = (
rootComponent: Comp,
rootProps?: Data | null,
) => App<HostElement>
let uid = 0
2024-12-04 11:54:26 +08:00
export type AppMountFn<HostElement> = (
app: App,
rootContainer: HostElement,
isHydrate?: boolean,
namespace?: boolean | ElementNamespace,
) => GenericComponentInstance
export type AppUnmountFn = (app: App) => void
/**
* @internal
*/
2024-12-04 13:50:54 +08:00
export function createAppAPI<HostElement, Comp = Component>(
2024-12-04 11:54:26 +08:00
// render: RootRenderFunction<HostElement>,
// hydrate?: RootHydrateFunction,
mount: AppMountFn<HostElement>,
unmount: AppUnmountFn,
getPublicInstance: (instance: GenericComponentInstance) => any,
2024-12-04 11:54:26 +08:00
render?: RootRenderFunction,
2024-12-04 13:50:54 +08:00
): CreateAppFunction<HostElement, Comp> {
return function createApp(rootComponent, rootProps = null) {
if (!isFunction(rootComponent)) {
2023-02-03 09:54:15 +08:00
rootComponent = extend({}, rootComponent)
}
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 WeakSet()
const pluginCleanupFns: Array<() => any> = []
2019-09-03 04:09:34 +08:00
2019-09-04 06:11:04 +08:00
let isMounted = false
const app: App = (context.app = {
_uid: uid++,
_component: rootComponent as ConcreteComponent,
_props: rootProps,
_container: null,
_context: context,
_instance: null,
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
},
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) {
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
},
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(
2024-12-04 11:54:26 +08:00
rootContainer: HostElement & { __vue_app__?: App },
isHydrate?: boolean,
namespace?: boolean | ElementNamespace,
): any {
2019-09-04 06:11:04 +08:00
if (!isMounted) {
// #5571
2024-12-04 11:54:26 +08:00
if (__DEV__ && rootContainer.__vue_app__) {
warn(
`There is already an app instance mounted on the host container.\n` +
` If you want to mount another app on the same host container,` +
` you need to unmount the previous app by calling \`app.unmount()\` first.`,
)
}
2024-12-04 11:54:26 +08:00
const instance = mount(app, rootContainer, isHydrate, namespace)
2024-12-04 11:54:26 +08:00
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
app._instance = instance
devtoolsInitApp(app, version)
2019-12-23 01:25:04 +08:00
}
2019-09-04 06:11:04 +08:00
isMounted = true
app._container = rootContainer
// for devtools and telemetry
2024-12-04 11:54:26 +08:00
rootContainer.__vue_app__ = app
2020-07-17 06:18:52 +08:00
return getPublicInstance(instance)
2019-09-04 06:11:04 +08:00
} else if (__DEV__) {
warn(
`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
},
onUnmount(cleanupFn: () => void) {
if (__DEV__ && typeof cleanupFn !== 'function') {
warn(
`Expected function as first argument to app.onUnmount(), ` +
`but got ${typeof cleanupFn}`,
)
}
pluginCleanupFns.push(cleanupFn)
},
unmount() {
if (isMounted) {
callWithAsyncErrorHandling(
pluginCleanupFns,
app._instance,
ErrorCodes.APP_UNMOUNT_CLEANUP,
)
2024-12-04 11:54:26 +08:00
unmount(app)
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
app._instance = null
devtoolsUnmountApp(app)
}
delete app._container.__vue_app__
} else if (__DEV__) {
warn(`Cannot unmount an app that is not mounted.`)
}
},
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(
`App already provides property with key "${String(key)}". ` +
2019-09-03 04:09:34 +08:00
`It will be overwritten with the new value.`,
)
}
context.provides[key as string | symbol] = value
return app
},
runWithContext(fn) {
const lastApp = currentApp
currentApp = app
try {
return fn()
} finally {
currentApp = lastApp
}
2019-09-03 04:09:34 +08:00
},
})
2020-07-17 06:18:52 +08:00
2021-04-05 23:54:35 +08:00
if (__COMPAT__) {
2024-12-04 11:54:26 +08:00
installAppCompatProperties(
app,
context,
// vapor doesn't have compat mode so this is always passed
render!,
)
2021-04-05 23:54:35 +08:00
}
2019-09-03 04:09:34 +08:00
return app
}
}
/**
* @internal Used to identify the current app when using `inject()` within
* `app.runWithContext()`.
*/
export let currentApp: App<unknown> | null = null