vue3-yuanma/packages/runtime-core/src/devtools.ts
Evan You 54727f9874 feat: provide ability to overwrite feature flags in esm-bundler builds
e.g. by replacing `__VUE_OPTIONS_API__` to `false` using webpack's
`DefinePlugin`, the final bundle will drop all code supporting the
options API.

This does not break existing usage, but requires the user to explicitly
configure the feature flags via bundlers to properly tree-shake the
disabled branches. As a result, users will see a console warning if
the flags have not been properly configured.
2020-07-20 21:51:30 -04:00

73 lines
1.8 KiB
TypeScript

import { App } from './apiCreateApp'
import { Fragment, Text, Comment, Static } from './vnode'
import { ComponentInternalInstance } from './component'
export interface AppRecord {
id: number
app: App
version: string
types: Record<string, string | Symbol>
}
const enum DevtoolsHooks {
APP_INIT = 'app:init',
APP_UNMOUNT = 'app:unmount',
COMPONENT_UPDATED = 'component:updated',
COMPONENT_ADDED = 'component:added',
COMPONENT_REMOVED = 'component:removed'
}
export interface DevtoolsHook {
emit: (event: string, ...payload: any[]) => void
on: (event: string, handler: Function) => void
once: (event: string, handler: Function) => void
off: (event: string, handler: Function) => void
appRecords: AppRecord[]
}
export let devtools: DevtoolsHook
export function setDevtoolsHook(hook: DevtoolsHook) {
devtools = hook
}
export function devtoolsInitApp(app: App, version: string) {
// TODO queue if devtools is undefined
if (!devtools) return
devtools.emit(DevtoolsHooks.APP_INIT, app, version, {
Fragment,
Text,
Comment,
Static
})
}
export function devtoolsUnmountApp(app: App) {
if (!devtools) return
devtools.emit(DevtoolsHooks.APP_UNMOUNT, app)
}
export const devtoolsComponentAdded = /*#__PURE__*/ createDevtoolsHook(
DevtoolsHooks.COMPONENT_ADDED
)
export const devtoolsComponentUpdated = /*#__PURE__*/ createDevtoolsHook(
DevtoolsHooks.COMPONENT_UPDATED
)
export const devtoolsComponentRemoved = /*#__PURE__*/ createDevtoolsHook(
DevtoolsHooks.COMPONENT_REMOVED
)
function createDevtoolsHook(hook: DevtoolsHooks) {
return (component: ComponentInternalInstance) => {
if (!devtools) return
devtools.emit(
hook,
component.appContext.app,
component.uid,
component.parent ? component.parent.uid : undefined
)
}
}