compatConfig.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { extend } from '@vue/shared'
  2. import { DeprecationTypes, warnDeprecation } from './deprecations'
  3. export type CompatConfig = Partial<
  4. Record<DeprecationTypes, DeprecationConfigItem>
  5. >
  6. export interface DeprecationConfigItem {
  7. warning?: boolean // default: true
  8. enabled?: boolean // default: true
  9. }
  10. const globalCompatConfig: CompatConfig = {}
  11. export function configureCompat(config: CompatConfig) {
  12. extend(globalCompatConfig, config)
  13. }
  14. export function getCompatConfig(
  15. key: DeprecationTypes
  16. ): DeprecationConfigItem | undefined {
  17. return globalCompatConfig[key]
  18. }
  19. export function isCompatEnabled(key: DeprecationTypes): boolean {
  20. const config = getCompatConfig(key)
  21. return !config || config.enabled !== false
  22. }
  23. export function assertCompatEnabled(key: DeprecationTypes, ...args: any[]) {
  24. if (!isCompatEnabled(key)) {
  25. throw new Error(`${key} compat has been disabled.`)
  26. } else if (__DEV__) {
  27. warnDeprecation(key, ...args)
  28. }
  29. }
  30. export function softAssertCompatEnabled(key: DeprecationTypes, ...args: any[]) {
  31. if (__DEV__) {
  32. warnDeprecation(key, ...args)
  33. }
  34. return isCompatEnabled(key)
  35. }
  36. // disable features that conflict with v3 behavior
  37. if (__TEST__) {
  38. configureCompat({
  39. COMPONENT_ASYNC: { enabled: false },
  40. COMPONENT_FUNCTIONAL: { enabled: false },
  41. WATCH_ARRAY: { enabled: false },
  42. INSTANCE_ATTRS_CLASS_STYLE: { enabled: false }
  43. })
  44. }