|
1 |
| -import { resolveComponent as resolveComponentCore } from '@vue/runtime-core'; |
2 | 1 | import type { CreateAppFunction } from '@vue/runtime-core';
|
| 2 | +import { |
| 3 | + createBlock as createBlockCore, |
| 4 | + createElementBlock as createElementBlockCore, |
| 5 | + createElementVNode as createElementVNodeCore, |
| 6 | + createVNode as createVNodeCore, |
| 7 | + resolveComponent as resolveComponentCore, |
| 8 | +} from '@vue/runtime-core'; |
3 | 9 |
|
4 | 10 | import { BUILT_IN_COMPONENTS } from './components';
|
5 | 11 |
|
@@ -106,3 +112,41 @@ export function resolveComponent(name: string, maybeSelReference: boolean) {
|
106 | 112 | const component = resolveComponentCore(name, maybeSelReference);
|
107 | 113 | return component;
|
108 | 114 | }
|
| 115 | + |
| 116 | +/** |
| 117 | + * Checks if the type has a constructor.name that matches a known view or built-in component |
| 118 | + * If so, returns the name of the view or component. This allows {N} element imports to be |
| 119 | + * used inside script setup context without requiring aliasing. |
| 120 | + */ |
| 121 | +function maybeConvertToKnownComponentOrViewName(type: any) { |
| 122 | + const name = type?.prototype?.constructor?.name; |
| 123 | + if (name) { |
| 124 | + if (BUILT_IN_COMPONENTS[name]) { |
| 125 | + return BUILT_IN_COMPONENTS[name]; |
| 126 | + } |
| 127 | + |
| 128 | + if (isKnownView(name)) { |
| 129 | + return name; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return type; |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Wraps the original function and replaces the first argument if it matches |
| 138 | + * a known view or built-in component. |
| 139 | + */ |
| 140 | +function wrapCreate<T>(originalFunction: T): T { |
| 141 | + return ((type: any, ...args: any) => { |
| 142 | + return (originalFunction as any)( |
| 143 | + maybeConvertToKnownComponentOrViewName(type), |
| 144 | + ...args |
| 145 | + ); |
| 146 | + }) as T; |
| 147 | +} |
| 148 | + |
| 149 | +export const createBlock = wrapCreate(createBlockCore); |
| 150 | +export const createElementBlock = wrapCreate(createElementBlockCore); |
| 151 | +export const createElementVNode = wrapCreate(createElementVNodeCore); |
| 152 | +export const createVNode: typeof createVNodeCore = wrapCreate(createVNodeCore); |
0 commit comments