UNPKG

theme-json-generator

Version:

Generate WordPress theme.json from your tailwind.config with Webpack.

3 lines (2 loc) 3.18 kB
function e(){return e=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},e.apply(this,arguments)}var r=require("path"),t=require("fs"),n={path:r.resolve("./"),from:"theme.config.js",to:"theme.json",schema:"https://schemas.wp.org/trunk/theme.json",version:2};function o(e,r){if(void 0===r&&(r=!1),(e=Array.isArray(e)?e:[e]).length)for(var t=0;t<e.length;t++)i(e[t],r);else r&&r.error("Missing parameters required to generate JSON file.")}function i(o,i){void 0===i&&(i=!1);var s=e({},n,o),a=function(e,r){void 0===r&&(r=!1);var t=e.from,n=e.to;if(!e.path||!t||!n){var o="Missing parameters required to generate theme.json file.";return r?r.error(o):console.warn(o),!1}if(t.includes("../")||n.includes("../")){var i="The path, from and to options cannot reference a directory outside of the current base working directory.";return r?r.error(i):console.warn(i),!1}return!0}(s,i);if(a){var c=function(e){var r=e.from,t=e.to,n=e.path;return{from:n+"/"+(r=(r="."===(r=r.replace(/\/\//g,"/")).charAt(0)?r.substring(1):r).startsWith("/")?r.substring(1):r),to:n+"/"+(t=(t="."===(t=t.replace(/\/\//g,"/")).charAt(0)?t.substring(1):t).startsWith("/")?t.substring(1):t)}}(s);if(t.existsSync(null==c?void 0:c.from)&&null!=c&&c.to){var u=function(e){return delete require.cache[require.resolve(e)],require(e)}(c.from),f=r.resolve(c.to),l=e({$schema:s.schema,version:s.version},u);t.writeFile(f,JSON.stringify(l,null,3),function(e){e?i?i.error(e):console.warn(e):i&&i.info(r.basename(s.to)+" created successfully!")})}else i&&i.error("Unable to locate source file. Use the `from` option to specify the relative path to the config file and the `to` option to speicify the output file.")}}function s(e,r,t){for(var n in void 0===t&&(t={}),e){var o=r?r+"-"+n:n;"object"!=typeof e[n]||Array.isArray(e[n])?t[o]=e[n]:s(e[n],o,t)}return t}function a(e){if(!e)return"";var r=["of","the","and"];return(e=e.replace(/-/g," ").replace(/_/g," ")).toLowerCase().split(" ").map(function(e,t){return r.includes(e)&&0!=t?e:e.charAt(0).toUpperCase().concat(e.substr(1))}).join(" ")}module.exports={ThemeJsonPlugin:/*#__PURE__*/function(){function e(e){this.name="theme-json-generator",this.options=e}return e.prototype.apply=function(e){var r=this;e.hooks.done.tap("ThemeJsonPlugin",function(){var t=e.getInfrastructureLogger(r.name);o(r.options,t)})},e}(),generateThemeJson:o,transform:function(e,r,t){switch(void 0===t&&(t=[]),e){case"palette":case"spacingSizes":case"fontSizes":case"fontSizes":return!!r&&function(e,r,t){void 0===r&&(r=[]),void 0===t&&(t="pallette");var n=[],o="color";"fontSizes"!==t&&"spacingSizes"!==t||(o="size");for(var i=0,s=Object.entries(e);i<s.length;i++){var c,u=s[i],f=u[0],l=u[1];n.push(((c={name:a(f),slug:f})[o]=l,c))}return r.length?n.filter(function(e){return r.includes(e.slug)}):n}(s(r),t,e);default:return!!r&&function(e,r){void 0===r&&(r=[]);for(var t={},n=0,o=Object.entries(e);n<o.length;n++){var i=o[n],s=i[0],a=i[1];r.length?r.includes(s)&&(t[s]=a.toString()):t[s]=a.toString()}return t}(s(r),t)}}}; //# sourceMappingURL=index.esm.mjs.map