vue3-core/packages/runtime-core/src/compat/globalConfig.ts

120 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-04-12 04:53:43 +08:00
import { extend, isArray, isString } from '@vue/shared'
2021-04-06 22:02:46 +08:00
import { AppConfig } from '../apiCreateApp'
import { isRuntimeOnly } from '../component'
2021-04-12 04:53:43 +08:00
import { deepMergeData } from './data'
import {
DeprecationTypes,
warnDeprecation,
isCompatEnabled
} from './compatConfig'
2021-04-06 22:02:46 +08:00
import { isCopyingConfig } from './global'
// legacy config warnings
export type LegacyConfig = {
/**
* @deprecated `config.silent` option has been removed
*/
silent?: boolean
/**
* @deprecated use __VUE_PROD_DEVTOOLS__ compile-time feature flag instead
* https://github.com/vuejs/vue-next/tree/master/packages/vue#bundler-build-feature-flags
*/
devtools?: boolean
/**
* @deprecated use `config.isCustomElement` instead
* https://v3.vuejs.org/guide/migration/global-api.html#config-ignoredelements-is-now-config-iscustomelement
*/
ignoredElements?: (string | RegExp)[]
/**
* @deprecated
* https://v3.vuejs.org/guide/migration/keycode-modifiers.html
*/
keyCodes?: Record<string, number | number[]>
/**
* @deprecated
* https://v3.vuejs.org/guide/migration/global-api.html#config-productiontip-removed
*/
productionTip?: boolean
}
// dev only
2021-04-12 04:53:43 +08:00
export function installLegacyConfigProperties(config: AppConfig) {
2021-04-06 22:02:46 +08:00
const legacyConfigOptions: Record<string, DeprecationTypes> = {
silent: DeprecationTypes.CONFIG_SILENT,
devtools: DeprecationTypes.CONFIG_DEVTOOLS,
ignoredElements: DeprecationTypes.CONFIG_IGNORED_ELEMENTS,
keyCodes: DeprecationTypes.CONFIG_KEY_CODES,
productionTip: DeprecationTypes.CONFIG_PRODUCTION_TIP
}
Object.keys(legacyConfigOptions).forEach(key => {
let val = (config as any)[key]
Object.defineProperty(config, key, {
enumerable: true,
get() {
return val
},
set(newVal) {
if (!isCopyingConfig) {
2021-04-10 06:52:14 +08:00
warnDeprecation(legacyConfigOptions[key], null)
2021-04-06 22:02:46 +08:00
}
val = newVal
// compat for runtime ignoredElements -> isCustomElement
if (
key === 'ignoredElements' &&
2021-04-10 06:52:14 +08:00
isCompatEnabled(DeprecationTypes.CONFIG_IGNORED_ELEMENTS, null) &&
!isRuntimeOnly() &&
isArray(newVal)
) {
2021-04-06 22:02:46 +08:00
config.isCustomElement = tag => {
return newVal.some(
val => (isString(val) ? val === tag : val.test(tag))
)
}
}
}
})
})
2021-04-12 04:53:43 +08:00
// Internal merge strats which are no longer needed in v3, but we need to
// expose them because some v2 plugins will reuse these internal strats to
// merge their custom options.
const strats = config.optionMergeStrategies as any
strats.data = deepMergeData
// lifecycle hooks
strats.beforeCreate = mergeHook
strats.created = mergeHook
strats.beforeMount = mergeHook
strats.mounted = mergeHook
strats.beforeUpdate = mergeHook
strats.updated = mergeHook
strats.beforeDestroy = mergeHook
strats.destroyed = mergeHook
strats.activated = mergeHook
strats.deactivated = mergeHook
strats.errorCaptured = mergeHook
strats.serverPrefetch = mergeHook
// assets
strats.components = mergeObjectOptions
strats.directives = mergeObjectOptions
strats.filters = mergeObjectOptions
// objects
strats.props = mergeObjectOptions
strats.methods = mergeObjectOptions
strats.inject = mergeObjectOptions
strats.computed = mergeObjectOptions
// watch has special merge behavior in v2, but isn't actually needed in v3.
// since we are only exposing these for compat and nobody should be relying
// on the watch-specific behavior, just expose the object merge strat.
strats.watch = mergeObjectOptions
}
function mergeHook(to: Function[] | undefined, from: Function | Function[]) {
return Array.from(new Set([...(to || []), from]))
}
function mergeObjectOptions(to: Object | undefined, from: Object | undefined) {
return to ? extend(extend(Object.create(null), to), from) : from
2021-04-06 22:02:46 +08:00
}