UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 3.29 kB
{"version":3,"file":"install.mjs","names":[],"sources":["../../../../../packages/utils/vue/install.ts"],"sourcesContent":["import { hasOwn, isArray } from '@vue/shared'\nimport { fromPairs, isPlainObject } from 'lodash-unified'\nimport { NOOP } from '../functions'\n\nimport type { App, Directive } from 'vue'\nimport type { SFCInstallWithContext, SFCWithInstall } from './typescript'\n\nexport const withPropsDefaultsSetter = (target: any) => {\n const _p = target.props\n const props = isArray(_p) ? fromPairs(_p.map((key) => [key, {}])) : _p\n\n target.setPropsDefaults = (defaults: Record<string, any>) => {\n if (!props) {\n return\n }\n\n for (const [key, value] of Object.entries(defaults)) {\n const prop = props[key]\n\n if (!hasOwn(props, key)) {\n continue\n }\n\n if (isPlainObject(prop)) {\n // e.g. { type: String }\n props[key] = {\n ...prop,\n default: value,\n }\n continue\n }\n\n props[key] = {\n type: prop,\n default: value,\n }\n }\n\n target.props = props\n }\n}\n\nexport const withInstall = <T, E extends Record<string, any>>(\n main: T,\n extra?: E\n) => {\n ;(main as SFCWithInstall<T>).install = (app): void => {\n for (const comp of [main, ...Object.values(extra ?? {})]) {\n app.component(comp.name, comp)\n }\n }\n\n if (extra) {\n for (const [key, comp] of Object.entries(extra)) {\n ;(main as any)[key] = comp\n }\n }\n withPropsDefaultsSetter(main)\n return main as SFCWithInstall<T> & E\n}\n\nexport const withInstallFunction = <T>(fn: T, name: string) => {\n ;(fn as SFCWithInstall<T>).install = (app: App) => {\n ;(fn as SFCInstallWithContext<T>)._context = app._context\n app.config.globalProperties[name] = fn\n }\n\n return fn as SFCInstallWithContext<T>\n}\n\nexport const withInstallDirective = <T extends Directive>(\n directive: T,\n name: string\n) => {\n ;(directive as SFCWithInstall<T>).install = (app: App): void => {\n app.directive(name, directive)\n }\n\n return directive as SFCWithInstall<T>\n}\n\nexport const withNoopInstall = <T>(component: T) => {\n ;(component as SFCWithInstall<T>).install = NOOP\n withPropsDefaultsSetter(component)\n return component as SFCWithInstall<T>\n}\n"],"mappings":";;;;;AAOA,MAAa,2BAA2B,WAAgB;CACtD,MAAM,KAAK,OAAO;CAClB,MAAM,QAAQ,QAAQ,GAAG,GAAG,UAAU,GAAG,KAAK,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,GAAG;AAEpE,QAAO,oBAAoB,aAAkC;AAC3D,MAAI,CAAC,MACH;AAGF,OAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,SAAS,EAAE;GACnD,MAAM,OAAO,MAAM;AAEnB,OAAI,CAAC,OAAO,OAAO,IAAI,CACrB;AAGF,OAAI,cAAc,KAAK,EAAE;AAEvB,UAAM,OAAO;KACX,GAAG;KACH,SAAS;KACV;AACD;;AAGF,SAAM,OAAO;IACX,MAAM;IACN,SAAS;IACV;;AAGH,SAAO,QAAQ;;;AAInB,MAAa,eACX,MACA,UACG;AACF,CAAC,KAA2B,WAAW,QAAc;AACpD,OAAK,MAAM,QAAQ,CAAC,MAAM,GAAG,OAAO,OAAO,SAAS,EAAE,CAAC,CAAC,CACtD,KAAI,UAAU,KAAK,MAAM,KAAK;;AAIlC,KAAI,MACF,MAAK,MAAM,CAAC,KAAK,SAAS,OAAO,QAAQ,MAAM,CAC5C,CAAC,KAAa,OAAO;AAG1B,yBAAwB,KAAK;AAC7B,QAAO;;AAGT,MAAa,uBAA0B,IAAO,SAAiB;AAC5D,CAAC,GAAyB,WAAW,QAAa;AAChD,EAAC,GAAgC,WAAW,IAAI;AACjD,MAAI,OAAO,iBAAiB,QAAQ;;AAGtC,QAAO;;AAGT,MAAa,wBACX,WACA,SACG;AACF,CAAC,UAAgC,WAAW,QAAmB;AAC9D,MAAI,UAAU,MAAM,UAAU;;AAGhC,QAAO;;AAGT,MAAa,mBAAsB,cAAiB;AACjD,CAAC,UAAgC,UAAU;AAC5C,yBAAwB,UAAU;AAClC,QAAO"}