esbuild-plugin-tailwindcss
Version:
Esbuild plugin for TailwindCSS
2 lines (1 loc) • 1.16 kB
JavaScript
import L from"fs/promises";import A from"path";import $ from"@tailwindcss/postcss";import b from"autoprefixer";import v from"postcss";import C from"postcss-modules";import y from"crypto";var c=t=>y.createHash("sha256").update(t).digest("hex");var u=t=>n=>{let{postcssPlugins:i={},cssModules:{enabled:d=!1,filter:m=/\.module\.css$/,exclude:g=[],options:f={}}={}}=t,o="tailwindcss-module",e=new Map,h=async s=>{let w=A.basename(s.path),l=d&&m.test(w),p=[...i.prepend||[],$(),...i.disableAutoprefixer?[]:[b()],...i.append||[]];l&&p.push(C({...f,globalModulePaths:g,getJSON:(j,S)=>e.set(s.path,S)}));let M=await L.readFile(s.path,"utf8"),{css:r}=await v(p).process(M,{from:s.path});if(!l)return{contents:r,loader:"css"};let a=`${o}://${c(s.path)}`,R=JSON.stringify(e.get(s.path)),x=`import "${a}"; export default ${R};`;return e.set(a,r),{contents:x,loader:"js"}},P=s=>({contents:e.get(s.path)?.toString(),loader:"css"}),O=s=>({path:s.path,namespace:o});n.onLoad({filter:/\.css$/},h),n.onLoad({filter:/.*/,namespace:o},P),n.onResolve({filter:new RegExp(`^${o}://`)},O)};var T=(t={})=>({name:"tailwindcss",setup:u(t)});var G=T;export{G as default,T as tailwindPlugin};