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

125 lines
3.7 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-28 05:34:19 +08:00
import { mergeDataOption } 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.
2021-04-28 05:34:19 +08:00
extend(config.optionMergeStrategies, legacyOptionMergeStrats)
}
export const legacyOptionMergeStrats = {
data: mergeDataOption,
beforeCreate: mergeHook,
created: mergeHook,
beforeMount: mergeHook,
mounted: mergeHook,
beforeUpdate: mergeHook,
updated: mergeHook,
beforeDestroy: mergeHook,
destroyed: mergeHook,
activated: mergeHook,
deactivated: mergeHook,
errorCaptured: mergeHook,
serverPrefetch: mergeHook,
2021-04-12 04:53:43 +08:00
// assets
2021-04-28 05:34:19 +08:00
components: mergeObjectOptions,
directives: mergeObjectOptions,
filters: mergeObjectOptions,
2021-04-12 04:53:43 +08:00
// objects
2021-04-28 05:34:19 +08:00
props: mergeObjectOptions,
methods: mergeObjectOptions,
inject: mergeObjectOptions,
computed: mergeObjectOptions,
2021-04-12 04:53:43 +08:00
// 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.
2021-04-28 05:34:19 +08:00
watch: mergeObjectOptions
2021-04-12 04:53:43 +08:00
}
2021-04-23 02:59:54 +08:00
function mergeHook(
to: Function[] | Function | undefined,
from: Function | Function[]
) {
return Array.from(new Set([...(isArray(to) ? to : to ? [to] : []), from]))
2021-04-12 04:53:43 +08:00
}
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
}