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