@devup-ui/next-plugin
Version:
Zero Config, Zero FOUC, Zero Runtime, CSS in JS Preprocessor
2 lines (1 loc) • 3.55 kB
JavaScript
var{defineProperty:v,getOwnPropertyNames:I,getOwnPropertyDescriptor:B}=Object,T=Object.prototype.hasOwnProperty;function U(e){return this[e]}var q=(e)=>{var t=(E??=new WeakMap).get(e),r;if(t)return t;if(t=v({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function"){for(var i of I(e))if(!T.call(t,i))v(t,i,{get:U.bind(e,i),enumerable:!(r=B(e,i))||r.enumerable})}return E.set(e,t),t},E;var W=(e)=>e;function z(e,t){this[e]=W.bind(null,t)}var G=(e,t)=>{for(var r in t)v(e,r,{get:t[r],enumerable:!0,configurable:!0,set:z.bind(t,r)})};var Z={};G(Z,{default:()=>_,resetInit:()=>Y});module.exports=q(Z);var s=require("node:fs"),l=require("node:fs/promises"),C=require("node:http"),a=require("node:path"),n=require("@devup-ui/wasm"),O=!1,x=new Map,H=new C.Agent({keepAlive:!0});function w(e){return e instanceof Error?e:Error(String(e))}function K(e){let t=x.get(e);if(t!==void 0)return t;let r=Number.parseInt(s.readFileSync(e,"utf-8").trim(),10);return x.set(e,r),r}function Q(e){let t=JSON.parse(e);if(typeof t!=="object"||t===null)return{};let r=t;return{code:typeof r.code==="string"?r.code:void 0,error:typeof r.error==="string"?r.error:void 0,map:typeof r.map==="string"?r.map:void 0}}function k(e){if(!e)return null;return JSON.parse(e),e}function V(e,t,r){let i=C.request({hostname:"127.0.0.1",port:e,path:"/extract",method:"POST",headers:{"Content-Type":"application/json"},agent:H},(c)=>{let p=[];c.on("data",(o)=>p.push(o)),c.on("end",()=>{try{let o=Q(Buffer.concat(p).toString("utf-8"));if(c.statusCode!==200){r(Error(o.error??"Coordinator error"));return}if(o.code===void 0){r(Error("Coordinator response missing code"));return}let f=k(o.map);r(null,o.code,f)}catch(o){r(w(o))}})});i.on("error",(c)=>r(c)),i.write(t),i.end()}var X=function(e){let{watch:t,package:r,cssDir:i,sheetFile:c,classMapFile:p,fileMapFile:o,themeFile:f,singleCss:A,coordinatorPortFile:m,theme:N,defaultClassMap:j,defaultFileMap:J,defaultSheet:R,importAliases:L={}}=this.getOptions();if(m){this.addDependency(m);let d=this.async(),u=(g)=>{if(!s.existsSync(m)){if(g>0){setTimeout(()=>u(g-1),50);return}d(Error("Coordinator port file not found"));return}try{let h=K(m),b=a.relative(process.cwd(),this.resourcePath).replaceAll("\\","/"),y=JSON.stringify({filename:b,code:e.toString(),resourcePath:this.resourcePath});V(h,y,(S,M,D)=>{if(S)return d(S);d(null,M,D)})}catch(h){d(w(h))}};u(20);return}let F=[];if(!O)if(O=!0,t){if(this.addDependency(c),this.addDependency(p),this.addDependency(o),this.addDependency(f),s.existsSync(c))n.importSheet(JSON.parse(s.readFileSync(c,"utf-8")));if(s.existsSync(p))n.importClassMap(JSON.parse(s.readFileSync(p,"utf-8")));if(s.existsSync(o))n.importFileMap(JSON.parse(s.readFileSync(o,"utf-8")));if(s.existsSync(f))n.registerTheme(JSON.parse(s.readFileSync(f,"utf-8"))?.theme??{})}else n.importFileMap(J),n.importClassMap(j),n.importSheet(R),n.registerTheme(N);let P=this.async();try{let d=this.resourcePath,u=a.relative(a.dirname(d),i).replaceAll("\\","/"),g=a.relative(process.cwd(),d).replaceAll("\\","/");if(!u.startsWith("./"))u=`./${u}`;let{code:h,map:b,cssFile:y,updatedBaseStyle:S}=n.codeExtract(g,e.toString(),r,u,A,!1,!0,L),M=k(b);if(S&&t)F.push(l.writeFile(a.join(i,"devup-ui.css"),n.getCss(null,!1),"utf-8"));if(y&&t)F.push(l.writeFile(a.join(i,a.basename(y)),`/* ${this.resourcePath} ${Date.now()} */`),l.writeFile(c,n.exportSheet()),l.writeFile(p,n.exportClassMap()),l.writeFile(o,n.exportFileMap()));Promise.all(F).then(()=>P(null,h,M),(D)=>P(w(D)))}catch(d){P(w(d))}return},_=X,Y=()=>{O=!1,x.clear()};