UNPKG

vuestic-ui

Version:
1 lines 2.82 kB
{"version":3,"file":"createRenderFn.mjs","sources":["../../../../../src/services/config-transport/createRenderFn.ts"],"sourcesContent":["import { withCtx, h, DefineComponent, VNode, isVNode, Text, createBlock } from 'vue'\nimport type { SlotProp } from '../component-config'\n\ntype VueInternalRenderFunction = Function\n\nexport const renderSlotNode = (node: VNode, ctx = null) => {\n return withCtx(() => [node], ctx)\n}\n\nexport const makeVNode = <T>(node: SlotProp<T>) => {\n if (typeof node === 'string') {\n return h(Text, node)\n }\n\n return isVNode(node) ? node : createBlock(node)\n}\n\nconst renderSlots = (slots: Record<string, VNode | VueInternalRenderFunction>, ctx = null) => {\n return Object.keys(slots).reduce((acc, slotName) => {\n const slot = slots[slotName]\n // Maybe already compiled slot or just VNode provided by user\n acc[slotName] = typeof slot === 'function' ? slot : renderSlotNode(slot, ctx)\n\n return acc\n }, {} as Record<string, VueInternalRenderFunction>)\n}\n\nexport const createRenderFn = (component: DefineComponent): VueInternalRenderFunction | undefined => {\n const originalRenderFn = component.render || component.ssrRender\n\n if (!originalRenderFn) { return undefined }\n\n const compiledRenderedFn = originalRenderFn.name === '_sfc_render' || originalRenderFn.name === '_sfc_ssrRender'\n\n return function (...args: any[]) {\n const ctx = args[0]\n\n const slots = ctx.$.slots\n\n const customCtx = new Proxy(ctx, {\n get (target, key: any) {\n if (key === '$slots') {\n return renderSlots(slots)\n }\n\n return target[key]\n },\n })\n\n // When compile rendered function, it doesn't require thisArg\n const thisArg = compiledRenderedFn ? undefined : customCtx\n\n return originalRenderFn.call(thisArg, customCtx, ...args.slice(1))\n }\n}\n"],"names":[],"mappings":";AAKO,MAAM,iBAAiB,CAAC,MAAa,MAAM,SAAS;AACzD,SAAO,QAAQ,MAAM,CAAC,IAAI,GAAG,GAAG;AAClC;AAEa,MAAA,YAAY,CAAI,SAAsB;AAC7C,MAAA,OAAO,SAAS,UAAU;AACrB,WAAA,EAAE,MAAM,IAAI;AAAA,EACrB;AAEA,SAAO,QAAQ,IAAI,IAAI,OAAO,YAAY,IAAI;AAChD;AAEA,MAAM,cAAc,CAAC,OAA0D,MAAM,SAAS;AAC5F,SAAO,OAAO,KAAK,KAAK,EAAE,OAAO,CAAC,KAAK,aAAa;AAC5C,UAAA,OAAO,MAAM,QAAQ;AAEvB,QAAA,QAAQ,IAAI,OAAO,SAAS,aAAa,OAAO,eAAe,MAAM,GAAG;AAErE,WAAA;AAAA,EACT,GAAG,CAA+C,CAAA;AACpD;AAEa,MAAA,iBAAiB,CAAC,cAAsE;AAC7F,QAAA,mBAAmB,UAAU,UAAU,UAAU;AAEvD,MAAI,CAAC,kBAAkB;AAAS,WAAA;AAAA,EAAU;AAE1C,QAAM,qBAAqB,iBAAiB,SAAS,iBAAiB,iBAAiB,SAAS;AAEhG,SAAO,YAAa,MAAa;AACzB,UAAA,MAAM,KAAK,CAAC;AAEZ,UAAA,QAAQ,IAAI,EAAE;AAEd,UAAA,YAAY,IAAI,MAAM,KAAK;AAAA,MAC/B,IAAK,QAAQ,KAAU;AACrB,YAAI,QAAQ,UAAU;AACpB,iBAAO,YAAY,KAAK;AAAA,QAC1B;AAEA,eAAO,OAAO,GAAG;AAAA,MACnB;AAAA,IAAA,CACD;AAGK,UAAA,UAAU,qBAAqB,SAAY;AAE1C,WAAA,iBAAiB,KAAK,SAAS,WAAW,GAAG,KAAK,MAAM,CAAC,CAAC;AAAA,EAAA;AAErE;"}