component.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { isFunction, isObject } from '@vue/shared'
  2. import type { Component, ComponentInternalInstance } from '../component'
  3. import {
  4. DeprecationTypes,
  5. checkCompatEnabled,
  6. softAssertCompatEnabled,
  7. } from './compatConfig'
  8. import { convertLegacyAsyncComponent } from './componentAsync'
  9. import { convertLegacyFunctionalComponent } from './componentFunctional'
  10. export function convertLegacyComponent(
  11. comp: any,
  12. instance: ComponentInternalInstance | null,
  13. ): Component {
  14. if (comp.__isBuiltIn) {
  15. return comp
  16. }
  17. // 2.x constructor
  18. if (isFunction(comp) && comp.cid) {
  19. // #7766
  20. if (comp.render) {
  21. // only necessary when compiled from SFC
  22. comp.options.render = comp.render
  23. }
  24. // copy over internal properties set by the SFC compiler
  25. comp.options.__file = comp.__file
  26. comp.options.__hmrId = comp.__hmrId
  27. comp.options.__scopeId = comp.__scopeId
  28. comp = comp.options
  29. }
  30. // 2.x async component
  31. if (
  32. isFunction(comp) &&
  33. checkCompatEnabled(DeprecationTypes.COMPONENT_ASYNC, instance, comp)
  34. ) {
  35. // since after disabling this, plain functions are still valid usage, do not
  36. // use softAssert here.
  37. return convertLegacyAsyncComponent(comp)
  38. }
  39. // 2.x functional component
  40. if (
  41. isObject(comp) &&
  42. comp.functional &&
  43. softAssertCompatEnabled(
  44. DeprecationTypes.COMPONENT_FUNCTIONAL,
  45. instance,
  46. comp,
  47. )
  48. ) {
  49. return convertLegacyFunctionalComponent(comp)
  50. }
  51. return comp
  52. }