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