2021-09-24 19:11:56 +08:00
|
|
|
import { NOOP } from '@vue/shared'
|
2022-02-24 11:24:34 +08:00
|
|
|
|
|
|
|
import type { App } from 'vue'
|
2022-03-25 15:35:56 +08:00
|
|
|
import type { SFCInstallWithContext, SFCWithInstall } from './typescript'
|
2021-07-25 15:26:00 +08:00
|
|
|
|
2021-09-12 19:18:14 +08:00
|
|
|
export const withInstall = <T, E extends Record<string, any>>(
|
|
|
|
main: T,
|
|
|
|
extra?: E
|
|
|
|
) => {
|
2021-09-22 01:19:35 +08:00
|
|
|
;(main as SFCWithInstall<T>).install = (app): void => {
|
2021-09-12 19:18:14 +08:00
|
|
|
for (const comp of [main, ...Object.values(extra ?? {})]) {
|
|
|
|
app.component(comp.name, comp)
|
|
|
|
}
|
2021-07-25 15:26:00 +08:00
|
|
|
}
|
2021-09-12 19:18:14 +08:00
|
|
|
|
|
|
|
if (extra) {
|
|
|
|
for (const [key, comp] of Object.entries(extra)) {
|
|
|
|
;(main as any)[key] = comp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return main as SFCWithInstall<T> & E
|
2021-07-25 15:26:00 +08:00
|
|
|
}
|
2021-09-22 01:19:35 +08:00
|
|
|
|
|
|
|
export const withInstallFunction = <T>(fn: T, name: string) => {
|
2022-02-24 11:24:34 +08:00
|
|
|
;(fn as SFCWithInstall<T>).install = (app: App) => {
|
|
|
|
;(fn as SFCInstallWithContext<T>)._context = app._context
|
2021-09-22 01:19:35 +08:00
|
|
|
app.config.globalProperties[name] = fn
|
|
|
|
}
|
|
|
|
|
2022-02-24 11:24:34 +08:00
|
|
|
return fn as SFCInstallWithContext<T>
|
2021-09-22 01:19:35 +08:00
|
|
|
}
|
2021-09-24 19:11:56 +08:00
|
|
|
|
2022-06-22 01:01:11 +08:00
|
|
|
export const withInstallDirective = <T>(directive: T, name: string) => {
|
|
|
|
;(directive as SFCWithInstall<T>).install = (app: App): void => {
|
|
|
|
app.directive(name, directive)
|
|
|
|
}
|
|
|
|
|
|
|
|
return directive as SFCWithInstall<T>
|
|
|
|
}
|
|
|
|
|
2021-09-24 19:11:56 +08:00
|
|
|
export const withNoopInstall = <T>(component: T) => {
|
|
|
|
;(component as SFCWithInstall<T>).install = NOOP
|
|
|
|
|
|
|
|
return component as SFCWithInstall<T>
|
|
|
|
}
|