install.mjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { NOOP } from "../functions.mjs";
  2. import { hasOwn, isArray } from "@vue/shared";
  3. import { fromPairs, isPlainObject } from "lodash-unified";
  4. //#region ../../packages/utils/vue/install.ts
  5. const withPropsDefaultsSetter = (target) => {
  6. const _p = target.props;
  7. const props = isArray(_p) ? fromPairs(_p.map((key) => [key, {}])) : _p;
  8. target.setPropsDefaults = (defaults) => {
  9. if (!props) return;
  10. for (const [key, value] of Object.entries(defaults)) {
  11. const prop = props[key];
  12. if (!hasOwn(props, key)) continue;
  13. if (isPlainObject(prop)) {
  14. props[key] = {
  15. ...prop,
  16. default: value
  17. };
  18. continue;
  19. }
  20. props[key] = {
  21. type: prop,
  22. default: value
  23. };
  24. }
  25. target.props = props;
  26. };
  27. };
  28. const withInstall = (main, extra) => {
  29. main.install = (app) => {
  30. for (const comp of [main, ...Object.values(extra ?? {})]) app.component(comp.name, comp);
  31. };
  32. if (extra) for (const [key, comp] of Object.entries(extra)) main[key] = comp;
  33. withPropsDefaultsSetter(main);
  34. return main;
  35. };
  36. const withInstallFunction = (fn, name) => {
  37. fn.install = (app) => {
  38. fn._context = app._context;
  39. app.config.globalProperties[name] = fn;
  40. };
  41. return fn;
  42. };
  43. const withInstallDirective = (directive, name) => {
  44. directive.install = (app) => {
  45. app.directive(name, directive);
  46. };
  47. return directive;
  48. };
  49. const withNoopInstall = (component) => {
  50. component.install = NOOP;
  51. withPropsDefaultsSetter(component);
  52. return component;
  53. };
  54. //#endregion
  55. export { withInstall, withInstallDirective, withInstallFunction, withNoopInstall, withPropsDefaultsSetter };
  56. //# sourceMappingURL=install.mjs.map