UNPKG

sd-tailwindcss-transformer

Version:

[![Release](https://badgen.net/github/release/nado1001/sd-tailwindcss-transformer)](https://badgen.net/github/release/nado1001/sd-tailwindcss-transformer) [![Test](https://github.com/nado1001/sd-tailwindcss-transformer/actions/workflows/test.yml/badge.svg

9 lines 3.45 kB
'use strict';var y=/([\p{Ll}\d])(\p{Lu})/gu,E=/(\p{Lu})([\p{Lu}][\p{Ll}])/gu,S=/(\d)\p{Ll}|(\p{L})\d/u,b=/[^\p{L}\d]+/giu,g="$1\0$2";function T(t){let e=t.trim();e=e.replace(y,g).replace(E,g),e=e.replace(b,"\0");let n=0,o=e.length;for(;e.charAt(n)==="\0";)n++;if(n===o)return [];for(;e.charAt(o-1)==="\0";)o--;return e.slice(n,o).split(/\0/g)}function F(t){let e=T(t);for(let n=0;n<e.length;n++){let o=e[n],r=S.exec(o);if(r){let i=r.index+(r[1]??r[2]).length;e.splice(n,1,o.slice(0,i),o.slice(i));}}return e}function p(t,e){let[n,o,r]=R(t,e),i=_(e?.locale),s=$(e?.locale),a=L(i,s);return n+o.map((l,c)=>c===0?i(l):a(l,c)).join("")+r}function _(t){return e=>e.toLocaleLowerCase(t)}function $(t){return e=>e.toLocaleUpperCase(t)}function L(t,e){return (n,o)=>{let r=n[0];return (o>0&&r>="0"&&r<="9"?"_"+r:e(r))+t(n.slice(1))}}function R(t,e={}){let n=e.split??(e.separateNumbers?F:T),o=e.prefixCharacters??"",r=e.suffixCharacters??"",i=0,s=t.length;for(;i<t.length;){let a=t.charAt(i);if(!o.includes(a))break;i++;}for(;s>i;){let a=s-1,l=t.charAt(a);if(!r.includes(l))break;s=a;}return [t.slice(0,i),n(t.slice(i,s)),t.slice(s)]}var C=t=>t.endsWith("-")?t:`${t}-`,w=(t,e,n,o=true)=>{let r=e.length-1;for(let i=0;i<r;++i){let s=e[i];o&&(s=p(e[i])),s in t||(t[s]={}),t=t[s];}e[r]==="DEFAULT"&&(o=false),o?t[p(e[r])]=n:t[e[r]]=n;},f=(t,e)=>t===void 0?e:t,x=(t,e,n)=>{let o=" ".repeat(e);return n!=="all"?t:o+t},d=(t,e,n=4)=>t.replace(/"(\\[^]|[^\\"])*"\s*:?/g,r=>/[0-9]/.test(r)&&/[a-zA-Z]/.test(r)?r:/:$/.test(r)?x(r.replace(/^"|"(?=\s*:$)/g,""),n,e):r).replace(/}/g,r=>x(r,n,e)),h=(t,e,n,o,r,i)=>{let s=r?`theme: { extend: ${d(e,t,4)}, },`:`theme: ${d(e,t,2)},`;return `/** @type {import('tailwindcss').Config} */ module.exports = ${(()=>{let c=`{ content: [${o}], darkMode: "${n}", ${s}`;return i.length>0&&(c+=` plugins: [${i}]`),c+=` }`,c})()}`};var j=(t,e,n,o)=>{let r=t.reduce((s,a)=>{if(a.attributes===void 0)throw new Error(`Token ${a.name} has no attributes`);return (a.attributes.category===e||e==="all")&&(n&&a.attributes.category!=="screens"?s[Object.values(a.attributes).join(".")]=o?`var(--${C(o)+a.name})`:`var(--${a.name})`:s[Object.values(a.attributes).join(".")]=a.$value||a.value),s},{}),i={};return Object.keys(r).forEach(s=>{let a=s.split(".").filter(l=>l!==e);w(i,a,r[s],a[0]!=="typography");}),JSON.stringify(i,null,2)},P=({dictionary:{allTokens:t},type:e,isVariables:n,prefix:o,extend:r,tailwind:i})=>{let s=j(t,e,n,o);if(e==="all"){let a=f(i?.darkMode,"class"),l=f(Array.isArray(i?.content)?i?.content.map(u=>`"${u}"`):i?.content,['"./src/**/*.{ts,tsx}"']),c=f(i?.plugins?.map(u=>`require("@tailwindcss/${u}")`),[]);return h(e,s,a,l,r,c)}else return `module.exports = ${d(s)}`},D=({type:t,formatType:e="js",isVariables:n=false,extend:o=true,source:r,transforms:i,buildPath:s,prefix:a,tailwind:l,preprocessors:c})=>{if(t===void 0)throw new Error("type is required");if(e!=="js"&&e!=="cjs")throw new Error('formatType must be "js" or "cjs"');let m=t!=="all"?`${t}.tailwind.${e}`:`tailwind.config.${e}`;return {preprocessors:c,source:f(r,["tokens/**/*.json"]),hooks:{formats:{tailwindFormat:({dictionary:u})=>P({dictionary:u,isVariables:n,extend:o,prefix:a,type:t,tailwind:l})}},platforms:{[t!=="all"?`tailwind/${t}`:"tailwind"]:{transforms:f(i,["attribute/cti","name/kebab"]),buildPath:f(s,"build/web/"),files:[{destination:m,format:"tailwindFormat"}]}}}};exports.getTailwindFormat=P;exports.makeSdTailwindConfig=D;