UNPKG

next-slicezone

Version:

A component that maps other components to Prismic slices

2 lines (1 loc) 4.16 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("fs"),require("path")):"function"==typeof define&&define.amd?define(["exports","fs","path"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).NextSliceZoneResolver={},e.fs,e.path)}(this,(function(e,n,t){"use strict";function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=r(n),i=r(t);function c(e,n,t,r,o,i,c){try{var a=e[i](c),s=a.value}catch(e){return void t(e)}a.done?n(s):Promise.resolve(s).then(r,o)}function a(e){return function(){var n=this,t=arguments;return new Promise((function(r,o){var i=e.apply(n,t);function a(e){c(i,r,o,a,s,"next",e)}function s(e){c(i,r,o,a,s,"throw",e)}a(void 0)}))}}var s="sm.config.json",l="sm.json",u=/-(\w)/g;var f=/\B([A-Z])/g;function p(e){return e.replace(f,"-$1").toLowerCase()}var d={pascalize:function(e){return e?(e=e.replace(/_/g,"-").replace(u,((e,n)=>n?n.toUpperCase():"")))[0].toUpperCase()+e.slice(1):""},hyphenate:p,snakelize:function(e){return p(e).replace(/-/g,"_")}},m=e=>{const n=/^\\\\\?\\/.test(e),t=/[^\u0000-\u0080]+/.test(e);return n||t?e:e.replace(/\\/g,"/")};function y(e,n){var t=void 0!==["@/","~","/"].find((n=>0===e.indexOf(n))),r=i.default.join(n||process.cwd(),t?"":"node_modules",t?e.substring(1,e.length):e),c=i.default.join(r,s),a=o.default.existsSync(r),l={};o.default.existsSync(c)&&(l=JSON.parse(o.default.readFileSync(c)));var u=i.default.join(r,l.pathToLibrary||".",l.slicesFolder||(t?".":"slices"));return{config:l,isLocal:t,pathExists:a,pathToLib:m(r),pathToSlices:m(u)}}function h(){return(h=a((function*(e,n){var{isLocal:t,pathExists:r,pathToSlices:o}=yield y(e,n);if(!r)return console.warn('[next-slicezone] path to library "'.concat(o,'" does not exist. Skipping.')),null;var i=t?e.slice(2):e,c="".concat(t?"./":"").concat(i).concat(o.split(i).slice(1).join("")),a=d.pascalize(i);return{isLocal:t,from:i,name:a,importName:a.replace(/\//,"_"),pathToSlices:c}}))).apply(this,arguments)}var v=function(){var e=a((function*(e){var n=e||i.default.posix.join(process.cwd(),l),{libraries:t}=o.default.existsSync(n)?JSON.parse(o.default.readFileSync(n)):{};if(!t)return console.warn('[next-slicezone] expects a non-empty "libraries" array in file "'.concat(l,'"'));if(!Array.isArray(t)||!t.length)return console.error('[next-slicezone] expects "libraries" option to be a non-empty array');var r,c,s,u=i.default.dirname(n),f=yield Promise.all(t.map(function(){var e=a((function*(e){return yield function(e,n){return h.apply(this,arguments)}(e,u)}));return function(n){return e.apply(this,arguments)}}())),p=(r=f.filter((e=>e)),c=r.reduce(((e,n)=>{var{importName:t,from:r,isLocal:o}=n;return o?"".concat(e,"import * as ").concat(t," from './").concat(r,"'\n"):"".concat(e,"import { Slices as ").concat(t," } from '").concat(r,"'\n")}),""),s="const ".concat("__allSlices"," = { ").concat(r.reverse().reduce(((e,n)=>{var{importName:t}=n;return"".concat(e," ...").concat(t,",")}),"")," }"),"".concat(c,"\n").concat(s,"\n")),d="".concat(p,"\n").concat("const NotFound = ({ sliceName, slice, i }) => {\n console.error(`[sm-resolver] component \"${sliceName}\" not found at index ${i}.`)\n console.warn(`slice data: ${slice}`)\n\treturn process.env.NODE_ENV !== 'production' ? (\n <div\n style={{\n height: '30vh',\n display: 'flex',\n alignItems: 'center',\n justifyContent: 'center',\n flexDirection: 'column',\n textAlign: 'center',\n background: '#FAFAFA'\n }}\n >\n <h2>\n Slice \"{sliceName}\" not found.\n </h2>\n <p style={{ maxWidth: '320px', fontSize: '16px' }}>\n Check that you registered this component in your slices library!\n </p>\n </div>\n ) : null\n}\n\nexport default function SliceResolver({ sliceName, ...rest }) {\n\treturn __allSlices[sliceName] ? __allSlices[sliceName] : () => <NotFound sliceName={sliceName} {...rest} />\n}\n","\n ");return o.default.writeFileSync(i.default.join(u,"sm-resolver.js"),d,"utf-8")}));return function(n){return e.apply(this,arguments)}}();e.createResolver=v,Object.defineProperty(e,"__esModule",{value:!0})}));