UNPKG

@stacksjs/dtsx

Version:

A modern, fast .d.ts generation tool, powered by Bun.

11 lines (10 loc) 7 kB
// @bun import{Le as y}from"./chunk-sc0mtgkd.js";import{existsSync as C,mkdirSync as f,readFileSync as m,writeFileSync as u}from"fs";import{dirname as d,relative as p,resolve as n}from"path";async function t(z,q,H){let V=new Map,Z=[],v=new Set,M=0,T=0,w=0,B=[];for(let J of z){let Q=q.get(J);if(!Q)continue;B.push({file:J,decls:y(Q,J,H.keepComments)})}for(let{file:J,decls:Q}of B){M+=Q.length;for(let G of Q)if(G.kind==="import"){T++;let X=G.source||"";if(X.startsWith("."))continue;if(!V.has(X))V.set(X,{source:X,specifiers:new Map,isTypeOnly:G.isTypeOnly||!1,isSideEffect:G.isSideEffect||!1});let Y=V.get(X);if(G.specifiers)for(let $ of G.specifiers){let N=$.alias||$.name;if(!Y.specifiers.has(N))Y.specifiers.set(N,{name:$.name,alias:$.alias,isType:$.isType||!1})}if(!G.isTypeOnly)Y.isTypeOnly=!1}else if(G.isExported||G.kind==="export"){if(w++,G.kind==="export"&&G.source&&(G.source.startsWith(".")||G.source.startsWith("/")))continue;if(G.name&&v.has(G.name))continue;if(G.name)v.add(G.name);Z.push({declaration:G,file:J})}}let R=[];R.push("/**"),R.push(" * Bundled TypeScript declarations"),R.push(` * Generated from ${z.length} source files`),R.push(" */"),R.push("");let j=H.importOrder||["bun"],E=Array.from(V.values()),U=Array(E.length);for(let J=0;J<E.length;J++){let Q=E[J].source,G=j.length;for(let X=0;X<j.length;X++)if(Q.startsWith(j[X])){G=X;break}U[J]=G}let W=E.map((J,Q)=>Q);W.sort((J,Q)=>{let G=U[J],X=U[Q];if(G!==X)return G-X;return E[J].source.localeCompare(E[Q].source)});let k=W.map((J)=>E[J]);for(let J of k)if(J.isSideEffect)R.push(`import '${J.source}';`);else if(J.specifiers.size>0){let G=Array.from(J.specifiers.values()).map((Y)=>{let $=Y.isType?"type ":"";return Y.alias?`${$}${Y.name} as ${Y.alias}`:`${$}${Y.name}`}),X=J.isTypeOnly?"type ":"";R.push(`import ${X}{ ${G.join(", ")} } from '${J.source}';`)}if(k.length>0)R.push("");let _=new Map;for(let{declaration:J,file:Q}of Z){if(!_.has(Q))_.set(Q,[]);_.get(Q).push(J)}for(let[J,Q]of _){let G=p(H.cwd,J);R.push(`// From: ${G}`);for(let X of Q){if(H.keepComments&&X.leadingComments)for(let Y of X.leadingComments)R.push(Y);R.push(x(X))}R.push("")}let L=R.join(` `);return{content:L,files:z,declarationCount:M,importCount:T,exportCount:w,size:Buffer.byteLength(L,"utf8"),warnings:[]}}function x(z){if(z.text){let H=z.text.trim();if(z.isExported&&!H.startsWith("export"))H=`export ${H}`;if(!H.endsWith(";")&&!H.endsWith("}"))H+=";";return H}let q=[];if(z.isExported)q.push("export");if(z.isDefault)q.push("default");switch(z.kind){case"function":if(q.push("declare function"),q.push(z.name),z.generics)q.push(z.generics);if(q.push(`(${s(z.parameters)})`),z.returnType)q.push(`: ${z.returnType}`);break;case"variable":if(q.push("declare const"),q.push(z.name),z.typeAnnotation)q.push(`: ${z.typeAnnotation}`);break;case"interface":if(q.push("interface"),q.push(z.name),z.generics)q.push(z.generics);if(z.extends)q.push(`extends ${z.extends}`);if(q.push("{"),z.members)for(let H of z.members)q.push(` ${H.name}${H.typeAnnotation?`: ${H.typeAnnotation}`:""};`);q.push("}");break;case"type":if(q.push("type"),q.push(z.name),z.generics)q.push(z.generics);q.push("="),q.push(z.typeAnnotation||"unknown");break;case"class":if(q.push("declare class"),q.push(z.name),z.generics)q.push(z.generics);if(z.extends)q.push(`extends ${z.extends}`);if(z.implements&&z.implements.length>0)q.push(`implements ${z.implements.join(", ")}`);if(q.push("{"),z.members)for(let H of z.members)q.push(` ${x(H)}`);q.push("}");break;case"enum":if(q.push("declare enum"),q.push(z.name),q.push("{"),z.members){let H=z.members.map((V)=>V.value!==void 0?`${V.name} = ${V.value}`:V.name);q.push(` ${H.join(`, `)}`)}q.push("}");break;default:return z.text||""}return q.join(" ")}function s(z){if(!z||z.length===0)return"";return z.map((q)=>{let H="";if(q.rest)H+="...";if(H+=q.name,q.optional)H+="?";if(q.type)H+=`: ${q.type}`;if(q.defaultValue)H+=` = ${q.defaultValue}`;return H}).join(", ")}async function D(z,q={}){let H=[],V=new Map,Z=[],v=[],M=new Set,T=0,w=0,B=0;for(let U of z){if(!C(U)){H.push(`File not found: ${U}`);continue}let W=m(U,"utf-8");v.push(U);let k=W.split(` `),_=[],L=0,J=!1;for(let Q of k){let G=Q.trim();if(!G&&!J)continue;if(G.startsWith("import ")){w++;let Y=G.match(/import\s+(type\s+)?(?:\{([^}]+)\}|(\w+)|\*\s+as\s+(\w+))\s+from\s+['"]([^'"]+)['"]/);if(Y){let[,$,N,a,i,O]=Y;if(q.externals?.some((K)=>O.startsWith(K))){Z.push(G);continue}if(O.startsWith("."))continue;if(!V.has(O))V.set(O,{source:O,specifiers:new Map,isTypeOnly:!!$,isSideEffect:!1});let I=V.get(O);if(N){let K=N.split(",").map((S)=>S.trim());for(let S of K){let[F,P]=S.split(/\s+as\s+/).map((g)=>g.trim()),b=F.startsWith("type "),h=b?F.replace("type ",""):F;I.specifiers.set(P||h,{name:h,alias:P!==h?P:void 0,isType:b})}}if(!$)I.isTypeOnly=!1}continue}if(/^(export\s+)?(declare\s+)?(interface|type|class|function|const|let|var|enum|namespace|module)\s/.test(G)){if(J=!0,_=[Q],L=(Q.match(/\{/g)||[]).length-(Q.match(/\}/g)||[]).length,L===0&&(G.endsWith(";")||!G.includes("{"))){let Y=A(G);if(!M.has(Y)||!q.mergeDuplicates){if(M.add(Y),Z.push(Q),T++,G.startsWith("export"))B++}J=!1,_=[]}}else if(J){if(_.push(Q),L+=(Q.match(/\{/g)||[]).length-(Q.match(/\}/g)||[]).length,L<=0){let Y=_.join(` `),$=A(_[0]);if(!M.has($)||!q.mergeDuplicates){if(M.add($),Z.push(Y),T++,_[0].trim().startsWith("export"))B++}J=!1,_=[]}}else if(G.startsWith("export ")&&!G.includes("declare"))Z.push(Q),B++}}let R=[];if(q.banner)R.push(q.banner),R.push("");if(q.includeReferences&&q.references){for(let U of q.references)R.push(`/// <reference types="${U}" />`);R.push("")}let j=Array.from(V.values()).sort((U,W)=>U.source.localeCompare(W.source));for(let U of j)if(U.specifiers.size>0){let k=Array.from(U.specifiers.values()).map((L)=>{let J=L.isType?"type ":"";return L.alias?`${J}${L.name} as ${L.alias}`:`${J}${L.name}`}),_=U.isTypeOnly?"type ":"";R.push(`import ${_}{ ${k.join(", ")} } from '${U.source}';`)}if(j.length>0)R.push("");if(q.ambient&&q.moduleName){R.push(`declare module '${q.moduleName}' {`);for(let U of Z){let W=U.split(` `).map((k)=>` ${k.replace(/^(\s*)(export\s+)?declare\s+/,"$1$2")}`).join(` `);R.push(W)}R.push("}")}else{if(q.sortDeclarations)Z.sort((U,W)=>{let k=A(U),_=A(W);return k.localeCompare(_)});for(let U of Z)R.push(U)}if(q.footer)R.push(""),R.push(q.footer);let E=R.join(` `);return{content:E,files:v,declarationCount:T,importCount:w,exportCount:B,size:Buffer.byteLength(E,"utf-8"),warnings:H}}function A(z){let q=z.match(/(interface|type|class|function|const|let|var|enum|namespace|module)\s+(\w+)/);return q?`${q[1]}:${q[2]}`:z.trim()}async function o(z,q,H={}){let V=await D(z,H),Z=d(q);if(!C(Z))f(Z,{recursive:!0});return u(q,V.content),V}function e(z={}){return{bundle:(q)=>D(q,z),bundleAndWrite:(q,H)=>o(q,H,{...z,output:H})}}async function qq(z,q=process.cwd()){let H=Array.isArray(z)?z:[z],V=[];for(let Z of H){if(Z.includes("*"))continue;let v=n(q,Z);if(C(v))V.push(v)}return V} export{t as gc,D as hc,o as ic,e as jc,qq as kc};