|
| 1 | +import { isArray, isFunction, isObject, isPromise } from '@vue/shared/src' |
| 2 | +import { defineAsyncComponent } from '../apiAsyncComponent' |
| 3 | +import { Component, ComponentOptions, FunctionalComponent } from '../component' |
| 4 | +import { isVNode } from '../vnode' |
| 5 | +import { softAssertCompatEnabled } from './compatConfig' |
| 6 | +import { DeprecationTypes } from './deprecations' |
| 7 | + |
| 8 | +export function convertLegacyComponent(comp: any): Component { |
| 9 | + // 2.x async component |
| 10 | + if ( |
| 11 | + isFunction(comp) && |
| 12 | + softAssertCompatEnabled(DeprecationTypes.COMPONENT_ASYNC, comp) |
| 13 | + ) { |
| 14 | + return convertLegacyAsyncComponent(comp) |
| 15 | + } |
| 16 | + |
| 17 | + // 2.x functional component |
| 18 | + if ( |
| 19 | + isObject(comp) && |
| 20 | + comp.functional && |
| 21 | + softAssertCompatEnabled(DeprecationTypes.COMPONENT_FUNCTIONAL, comp) |
| 22 | + ) { |
| 23 | + return convertLegacyFunctionalComponent(comp) |
| 24 | + } |
| 25 | + |
| 26 | + return comp |
| 27 | +} |
| 28 | + |
| 29 | +interface LegacyAsyncOptions { |
| 30 | + component: Promise<Component> |
| 31 | + loading?: Component |
| 32 | + error?: Component |
| 33 | + delay?: number |
| 34 | + timeout?: number |
| 35 | +} |
| 36 | + |
| 37 | +type LegacyAsyncReturnValue = Promise<Component> | LegacyAsyncOptions |
| 38 | + |
| 39 | +type LegacyAsyncComponent = ( |
| 40 | + resolve?: (res: LegacyAsyncReturnValue) => void, |
| 41 | + reject?: (reason?: any) => void |
| 42 | +) => LegacyAsyncReturnValue | undefined |
| 43 | + |
| 44 | +const normalizedAsyncComponentMap = new Map<LegacyAsyncComponent, Component>() |
| 45 | + |
| 46 | +function convertLegacyAsyncComponent(comp: LegacyAsyncComponent) { |
| 47 | + if (normalizedAsyncComponentMap.has(comp)) { |
| 48 | + return normalizedAsyncComponentMap.get(comp)! |
| 49 | + } |
| 50 | + |
| 51 | + // we have to call the function here due to how v2's API won't expose the |
| 52 | + // options until we call it |
| 53 | + let resolve: (res: LegacyAsyncReturnValue) => void |
| 54 | + let reject: (reason?: any) => void |
| 55 | + const fallbackPromise = new Promise<Component>((r, rj) => { |
| 56 | + ;(resolve = r), (reject = rj) |
| 57 | + }) |
| 58 | + |
| 59 | + const res = comp(resolve!, reject!) |
| 60 | + |
| 61 | + let converted: Component |
| 62 | + if (isPromise(res)) { |
| 63 | + converted = defineAsyncComponent(() => res) |
| 64 | + } else if (isObject(res) && !isVNode(res) && !isArray(res)) { |
| 65 | + converted = defineAsyncComponent({ |
| 66 | + loader: () => res.component, |
| 67 | + loadingComponent: res.loading, |
| 68 | + errorComponent: res.error, |
| 69 | + delay: res.delay, |
| 70 | + timeout: res.timeout |
| 71 | + }) |
| 72 | + } else if (res == null) { |
| 73 | + converted = defineAsyncComponent(() => fallbackPromise) |
| 74 | + } else { |
| 75 | + converted = comp as any // probably a v3 functional comp |
| 76 | + } |
| 77 | + normalizedAsyncComponentMap.set(comp, converted) |
| 78 | + return converted |
| 79 | +} |
| 80 | + |
| 81 | +function convertLegacyFunctionalComponent(comp: ComponentOptions) { |
| 82 | + return comp.render as FunctionalComponent |
| 83 | +} |
0 commit comments