@stacksjs/dtsx
Version:
A modern, fast .d.ts generation tool, powered by Bun.
7 lines (6 loc) • 5.14 kB
JavaScript
// @bun
import{readFile as j,writeFile as Q}from"fs/promises";import{basename as N,dirname as X,relative as O,resolve as V}from"path";function Y(k){return`${String.fromCharCode(47,47,35)} sourceMappingURL=${k}`}class Z{file;sourceRoot;sources=[];sourcesContent=[];names=[];mappings=[];sourceIndexMap=new Map;nameIndexMap=new Map;constructor(k){this.file=k.file,this.sourceRoot=k.sourceRoot}addSource(k,q){let y=this.sourceIndexMap.get(k);if(y===void 0)y=this.sources.length,this.sources.push(k),this.sourcesContent.push(q??null),this.sourceIndexMap.set(k,y);else if(q&&!this.sourcesContent[y])this.sourcesContent[y]=q;return y}addName(k){let q=this.nameIndexMap.get(k);if(q===void 0)q=this.names.length,this.names.push(k),this.nameIndexMap.set(k,q);return q}addMapping(k){let q=this.addSource(k.source),y;if(k.name)y=this.addName(k.name);this.mappings.push({generated:k.generated,original:k.original,sourceIndex:q,nameIndex:y})}generate(k=!1){this.mappings.sort((y,z)=>{if(y.generated.line!==z.generated.line)return y.generated.line-z.generated.line;return y.generated.column-z.generated.column});let q={version:3,file:this.file,sources:this.sources,names:this.names,mappings:this.encodeMappings()};if(this.sourceRoot)q.sourceRoot=this.sourceRoot;if(k)q.sourcesContent=this.sourcesContent;return q}encodeMappings(){let k=[],q=0,y=0,z=0,A=0,B=0,D=0,H=[];for(let E of this.mappings){while(q<E.generated.line)k.push(H.join(",")),H=[],q++,y=0;let J=[];if(J.push(E.generated.column-y),y=E.generated.column,J.push(E.sourceIndex-z),z=E.sourceIndex,J.push(E.original.line-A),A=E.original.line,J.push(E.original.column-B),B=E.original.column,E.nameIndex!==void 0)J.push(E.nameIndex-D),D=E.nameIndex;H.push(_(J))}return k.push(H.join(",")),k.join(";")}toString(k=!1){return JSON.stringify(this.generate(k))}toDataUrl(k=!1){let q=this.toString(k);return`data:application/json;charset=utf-8;base64,${Buffer.from(q).toString("base64")}`}toComment(k=!1){return Y(this.toDataUrl(k))}}class R{map;decodedMappings=null;constructor(k){this.map=typeof k==="string"?JSON.parse(k):k}originalPositionFor(k){let q=this.getMappings(),y=0,z=q.length-1,A=null;while(y<=z){let B=Math.floor((y+z)/2),D=q[B];if(D.generated.line===k.line)if(D.generated.column===k.column){A=D;break}else if(D.generated.column<k.column)A=D,y=B+1;else z=B-1;else if(D.generated.line<k.line)y=B+1;else z=B-1}if(!A)return{source:null,line:null,column:null,name:null};return{source:this.map.sources[A.sourceIndex]||null,line:A.original.line,column:A.original.column,name:A.nameIndex!==void 0?this.map.names[A.nameIndex]:null}}generatedPositionFor(k){let q=this.map.sources.indexOf(k.source);if(q===-1)return null;let y=this.getMappings();for(let z of y)if(z.sourceIndex===q&&z.original.line===k.line&&z.original.column<=k.column)return z.generated;return null}getMappings(){if(!this.decodedMappings)this.decodedMappings=G(this.map.mappings);return this.decodedMappings}sourceContentFor(k){let q=this.map.sources.indexOf(k);if(q===-1||!this.map.sourcesContent)return null;return this.map.sourcesContent[q]}}function _(k){return k.map(F).join("")}function F(k){let B="",D=k<0?-k<<1|1:k<<1;do{let H=D&31;if(D>>>=5,D>0)H|=32;B+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[H]}while(D>0);return B}function G(k){let q=[],y="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",z=0,A=0,B=0,D=0,H=0,E=0,J=k.split(";");for(let T of J){if(A=0,T){let $=T.split(",");for(let U of $){if(!U)continue;let K=w(U,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");if(K.length>=1)A+=K[0];if(K.length>=4){B+=K[1],D+=K[2],H+=K[3];let W={generated:{line:z,column:A},original:{line:D,column:H},sourceIndex:B};if(K.length>=5)E+=K[4],W.nameIndex=E;q.push(W)}}}z++}return q}function w(k,q){let z=[],A=0,B=0;for(let D of k){let H=q.indexOf(D);if(H===-1)continue;let E=H&32;if(B+=(H&31)<<A,E)A+=5;else{let J=B&1;B>>=1,z.push(J?-B:B),B=0,A=0}}return z}function I(k){let q=new Z({file:N(k.generatedFile),sourceRoot:k.sourceRoot}),y=O(X(k.generatedFile),k.sourceFile);q.addSource(y,k.sourceContent);for(let z of k.mappings)q.addMapping({generated:{line:z.generatedLine,column:z.generatedColumn},original:{line:z.originalLine,column:z.originalColumn},source:y,name:z.name});return q}function f(k,q){return`${k}
${Y(q)}`}function L(k,q,y=!1){return`${k}
${q.toComment(y)}`}async function S(k,q,y=!1){await Q(k,q.toString(y))}async function b(k){let q=await j(k,"utf-8");return new R(q)}async function x(k,q){let y=k.match(/\/\/# sourceMappingURL=data:application\/json[^,]+,(\S+)/);if(y){let A=y[1],B=Buffer.from(A,"base64").toString("utf-8");return new R(B)}let z=k.match(/\/\/# sourceMappingURL=(.+)$/);if(z){let A=V(X(q),z[1].trim());try{return await b(A)}catch{return null}}return null}function h(k,q){let y=k,z=q,A=y.split(`
`),B=[],D=0,H=0;for(let E of z){while(D+A[H].length+1<=E.generatedStart&&H<A.length-1)D+=A[H].length+1,H++;let J=E.generatedStart-D;B.push({generatedLine:H,generatedColumn:Math.max(0,J),originalLine:E.originalLine,originalColumn:E.originalColumn,name:E.name})}return B}
export{Z as h,R as i,I as j,f as k,L as l,S as m,b as n,x as o,h as p};