15 lines
13 KiB
JavaScript
15 lines
13 KiB
JavaScript
var ae=["esm.sh","skypack","esm.run","jsdelivr.esm","fastly.jsdelivr.esm","gcore.jsdelivr.esm","testingcf.jsdelivr.esm","jsdelivr.b-cdn.esm","jspm"],pe=["jsdelivr","fastly.jsdelivr","unpkg","gcore.jsdelivr","testingcf.jsdelivr","jsdelivr.b-cdn","npmcdn"],de=["jsdelivr.gh","fastly.jsdelivr.gh","statically","gcore.jsdelivr.gh","testingcf.jsdelivr.gh","jsdelivr.b-cdn.gh"],B={getModuleUrl:(t,{isModule:r=!0,defaultCDN:i="esm.sh",external:m}={})=>{t=t.replace(/#nobundle/g,"");let u=d=>!m||!d.includes("https://esm.sh")?d:d.includes("?")?`${d}&external=${m}`:`${d}?external=${m}`,c=ce(t,r,i);return c?u(c):r?u("https://esm.sh/"+t):"https://cdn.jsdelivr.net/npm/"+t},getUrl:(t,r)=>t.startsWith("http")||t.startsWith("data:")?t:ce(t,!1,r||Ee())||t,cdnLists:{npm:pe,module:ae,gh:de},checkCDNs:async(t,r)=>{let i=[r,...B.cdnLists.npm].filter(Boolean);for(let m of i)try{if((await fetch(B.getUrl(t,m),{method:"HEAD"})).ok)return m}catch{}return B.cdnLists.npm[0]}},Ee=()=>{if(globalThis.appCDN)return globalThis.appCDN;try{return new URL(location.href).searchParams.get("appCDN")||B.cdnLists.npm[0]}catch{return B.cdnLists.npm[0]}},ce=(t,r,i)=>{let m=r&&t.startsWith("unpkg:")?"?module":"";t.startsWith("gh:")?t=t.replace("gh",de[0]):t.includes(":")||(t=(i||(r?ae[0]:pe[0]))+":"+t);for(let u of Ce){let[c,d]=u;if(c.test(t))return t.replace(c,d)+m}return null},Ce=[[/^(esm\.sh:)(.+)/i,"https://esm.sh/$2"],[/^(npm:)(.+)/i,"https://esm.sh/$2"],[/^(node:)(.+)/i,"https://esm.sh/$2"],[/^(jsr:)(.+)/i,"https://esm.sh/jsr/$2"],[/^(skypack:)(.+)/i,"https://cdn.skypack.dev/$2"],[/^(jsdelivr:)(.+)/i,"https://cdn.jsdelivr.net/npm/$2"],[/^(fastly\.jsdelivr:)(.+)/i,"https://fastly.jsdelivr.net/npm/$2"],[/^(gcore\.jsdelivr:)(.+)/i,"https://gcore.jsdelivr.net/npm/$2"],[/^(testingcf\.jsdelivr:)(.+)/i,"https://testingcf.jsdelivr.net/npm/$2"],[/^(jsdelivr\.b-cdn:)(.+)/i,"https://jsdelivr.b-cdn.net/npm/$2"],[/^(jsdelivr\.gh:)(.+)/i,"https://cdn.jsdelivr.net/gh/$2"],[/^(fastly\.jsdelivr\.gh:)(.+)/i,"https://fastly.jsdelivr.net/gh/$2"],[/^(gcore\.jsdelivr\.gh:)(.+)/i,"https://gcore.jsdelivr.net/gh/$2"],[/^(testingcf\.jsdelivr\.gh:)(.+)/i,"https://testingcf.jsdelivr.net/gh/$2"],[/^(jsdelivr\.b-cdn\.gh:)(.+)/i,"https://jsdelivr.b-cdn.net/gh/$2"],[/^(statically:)(.+)/i,"https://cdn.statically.io/gh/$2"],[/^(esm\.run:)(.+)/i,"https://esm.run/$2"],[/^(jsdelivr\.esm:)(.+)/i,"https://cdn.jsdelivr.net/npm/$2/+esm"],[/^(fastly\.jsdelivr\.esm:)(.+)/i,"https://fastly.jsdelivr.net/npm/$2/+esm"],[/^(gcore\.jsdelivr\.esm:)(.+)/i,"https://gcore.jsdelivr.net/npm/$2/+esm"],[/^(testingcf\.jsdelivr\.esm:)(.+)/i,"https://testingcf.jsdelivr.net/npm/$2/+esm"],[/^(jsdelivr\.b-cdn\.esm:)(.+)/i,"https://jsdelivr.b-cdn.net/npm/$2/+esm"],[/^(jspm:)(.+)/i,"https://jspm.dev/$2"],[/^(esbuild:)(.+)/i,"https://esbuild.vercel.app/$2"],[/^(bundle\.run:)(.+)/i,"https://bundle.run/$2"],[/^(unpkg:)(.+)/i,"https://unpkg.com/$2"],[/^(npmcdn:)(.+)/i,"https://npmcdn.com/$2"],[/^(bundlejs:)(.+)/i,"https://deno.bundlejs.com/?file&q=$2"],[/^(bundle:)(.+)/i,"https://deno.bundlejs.com/?file&q=$2"],[/^(deno:)(.+)/i,"https://deno.bundlejs.com/?file&q=https://deno.land/x/$2/mod.ts"],[/^(https:\/\/deno\.land\/.+)/i,"https://deno.bundlejs.com/?file&q=$1"],[/^(github:|https:\/\/github\.com\/)(.[^\/]+?)\/(.[^\/]+?)\/(?!releases\/)(?:(?:blob|raw)\/)?(.+?\/.+)/i,"https://deno.bundlejs.com/?file&q=https://cdn.jsdelivr.net/gh/$2/$3@$4"],[/^(gist\.github:)(.+?\/[0-9a-f]+\/raw\/(?:[0-9a-f]+\/)?.+)$/i,"https://gist.githack.com/$2"],[/^(gitlab:|https:\/\/gitlab\.com\/)([^\/]+.*\/[^\/]+)\/(?:raw|blob)\/(.+?)(?:\?.*)?$/i,"https://deno.bundlejs.com/?file&q=https://gl.githack.com/$2/raw/$3"],[/^(bitbucket:|https:\/\/bitbucket\.org\/)([^\/]+\/[^\/]+)\/(?:raw|src)\/(.+?)(?:\?.*)?$/i,"https://deno.bundlejs.com/?file&q=https://bb.githack.com/$2/raw/$3"],[/^(bitbucket:)snippets\/([^\/]+\/[^\/]+)\/revisions\/([^\/\#\?]+)(?:\?[^#]*)?(?:\#file-(.+?))$/i,"https://bb.githack.com/!api/2.0/snippets/$2/$3/files/$4"],[/^(bitbucket:)snippets\/([^\/]+\/[^\/\#\?]+)(?:\?[^#]*)?(?:\#file-(.+?))$/i,"https://bb.githack.com/!api/2.0/snippets/$2/HEAD/files/$3"],[/^(bitbucket:)\!api\/2.0\/snippets\/([^\/]+\/[^\/]+\/[^\/]+)\/files\/(.+?)(?:\?.*)?$/i,"https://bb.githack.com/!api/2.0/snippets/$2/files/$3"],[/^(api\.bitbucket:)2.0\/snippets\/([^\/]+\/[^\/]+\/[^\/]+)\/files\/(.+?)(?:\?.*)?$/i,"https://bb.githack.com/!api/2.0/snippets/$2/files/$3"],[/^(rawgit:)(.+?\/[0-9a-f]+\/raw\/(?:[0-9a-f]+\/)?.+)$/i,"https://gist.githack.com/$2"],[/^(rawgit:|https:\/\/raw\.githubusercontent\.com)(\/[^\/]+\/[^\/]+|[0-9A-Za-z-]+\/[0-9a-f]+\/raw)\/(.+)/i,"https://deno.bundlejs.com/?file&q=https://raw.githack.com/$2/$3"]];var{getUrl:Je,getModuleUrl:Ne}=B;var me=Ne("path-browserify@1.0.1");var Fe=/^\uFEFF?/,fe=/^([ \t]*declare module )(['"])(.+?)(\2[ \t]*{?.*)$/,Y=/^([ \t]*(?:export )?(?:import .+? )= require\()(['"])(.+?)(\2\);.*)$/,he=/^([ \t]*(?:export|import) ?(?:(?:\* (?:as [^ ,]+)?)|.*)?,? ?(?:[^ ,]+ ?,?)(?:\{(?:[^ ,]+ ?,?)*\})? ?from )(['"])([^ ,]+)(\2;.*)$/,qe=/^[ \t]*\/\/\/[ \t]*<reference[ \t]+path=(["'])(.*?)\1?[ \t]*\/>.*$/,ee=/^\w+(?:[\.-]\w+)*$/,Te=/^([\./].*|.:.*)$/,De=/^[ \t]*(?:static )?private (?:static )?/,Me=/^([ \t]*)(static |)(public |)(static |)(.*)/;async function Ge(t){let r=await import(me);h(typeof t=="object"&&t,"options must be an object");let i=t.main,m=t.name,u=b(t.baseDir,t.main.split("/").slice(0,-1).join("/")),c=b(t.newline,`
|
|
`),d=b(t.indent," ")||" ",C=b(t.prefix,""),N=b(t.separator,"/")||"/",W=b(t.externals,!1),F=b(t.exclude,null),ge=b(t.referenceExternals,!1),te=b(t.emitOnIncludedFileNotFound,!1),se=b(t.emitOnNoIncludedFileNotFound,!1),_=b(t.headerText,""),z=!1,q=b(t.verbose,!1);h(i,'option "main" must be defined'),h(m,'option "name" must be defined'),h(typeof c=="string",'option "newline" must be a string'),h(typeof d=="string",'option "indent" must be a string'),h(typeof C=="string",'option "prefix" must be a string'),h(N.length>0,'option "separator" must have non-zero length'),s("### settings object passed ###"),ve(t),s("### settings ###"),s("main: %s",i),s("name: %s",m),s("baseDir: %s",u),s("mainFile: %s",i),s("externals: %s",W?"yes":"no"),s("exclude: %s",F),s("comments: %s",z?"yes":"no"),s("emitOnIncludedFileNotFound: %s",te?"yes":"no"),s("emitOnNoIncludedFileNotFound: %s",se?"yes":"no"),s("headerText %s",_);let xe=_?"/*"+_+`*/
|
|
`:"",T;typeof F=="function"?T=F:F instanceof RegExp?T=e=>F.test(e):T=()=>!1;let A=(e=>{if(!e.includes("/"))return e;if(!e.startsWith("@"))return e.split("/")[0];let[o,n,...a]=e.split("/");return`${o}/${n}`})(m),[be,je]=i.split(A,2),I=be+A+je?.split("/")[0]+"/";s(`
|
|
### find typings ###`);let V=e=>e.startsWith(I);s("source typings (will be included in output if actually used)"),s("excluded typings (will always be excluded from output)");let $=Object.create(null),oe=[],J=null,R=[],Ue=e=>R.indexOf(e)!==-1;{s(`
|
|
### parse files ###`);let e=[i],o=Object.create(null);for(;e.length>0;){let n=e.shift();if(!n||o[n])continue;o[n]=!0;let a=await ke(n);a&&(J||(J=a),$[a.file]=a,Ie(e,a.refs,a.relativeImports))}}s(`
|
|
### map exports ###`);let X=Object.create(null);Object.keys($).forEach(e=>{let o=$[e];o.exports.forEach(n=>{h(!(n in X),"already got export for: "+n),X[n]=o,s("- %s -> %s",n,o.file)})}),s(`
|
|
### determine typings to include ###`);let H=[],L=[],O=[];{let e=[J],o=Object.create(null);for(s("queue"),s(e);e.length>0;){let n=e.shift();!n||o[n.file]||(o[n.file]=!0,s("%s (%s)",n.name,n.file),L.push(n),n.externalImports.forEach(a=>{let p=X[a];if(p){if(!W){s(" - exclude external %s",a),w(O,p?p?.file:a);return}if(T(r.relative(u,p?.file),!0)){s(" - exclude external filter %s",a),w(H,p?.file);return}s(" - include external %s",a),h(p,a),e.push(p)}}),n.relativeImports.forEach(a=>{let p=$[a];if(p){if(T(r.relative(u,p?.file),!1)){s(" - exclude internal filter %s",a),w(H,p?.file);return}s(" - import relative %s",a),h(p,a),e.push(p)}}))}}s(`
|
|
### rewrite global external modules ###`),L.forEach(e=>{s(e.name),e.relativeRef.forEach(o=>{o.modified=Pe(o.original,Z),s(" - %s ==> %s",o.original,o.modified)}),e.importLineRef.forEach(o=>{Y.test(o.original)?o.modified=Oe(o.original,Z):o.modified=Se(o.original,Z),s(" - %s ==> %s",o.original,o.modified)})}),s(`
|
|
### build output ###`);let U=xe;O.length>0&&(U+="// Dependencies for this module:"+c,O.forEach(e=>{ge?U+=Re(r.relative(u,e).replace(/\\/g,"/"))+c:U+="// "+r.relative(u,e).replace(/\\/g,"/")+c})),oe.length>0&&(U+=c,U+=oe.join(c)+c),U+=c,U+=L.filter(e=>(e.lines=e.lines.filter(o=>o.skip!==!0),e.lines.length>0)).map(e=>V(e.file)?$e(e.file,e.lines.map(o=>ue(e.indent,d)(o))):e.lines.map(o=>ue(e.indent,d)(o)).join(c)+c).join(c)+c;let G=e=>L.filter(o=>o.file===e).length!==0,v={fileMap:$,includeFilesNotFound:[],noIncludeFilesNotFound:[],options:t};s("## files not found ##");for(let e in $){let o=$[e];o.fileExists||(G(o.file)?(v.includeFilesNotFound.push(o.file),ne(" X Included file NOT FOUND %s ",o.file)):(v.noIncludeFilesNotFound.push(o.file),s(" X Not used file not found %s",o.file)))}return s(`
|
|
### write output ###`),(v.includeFilesNotFound.length===0||v.includeFilesNotFound.length>0&&te)&&(v.noIncludeFilesNotFound.length===0||v.noIncludeFilesNotFound.length>0&&se)?v.emitted=!0:(ne(" XXX Not emit due to exist files not found."),s("See documentation for emitOnIncludedFileNotFound and emitOnNoIncludedFileNotFound options."),v.emitted=!1),q&&(s(`
|
|
### statistics ###`),s("excludedTypings"),H.forEach(e=>{s(" - %s",e)}),s("used external typings"),R.forEach(e=>{G(e)&&s(" - %s",e)}),s("unused external typings"),R.forEach(e=>{G(e)||s(" - %s",e)}),s("external dependencies"),O.forEach(e=>{s(" - %s",e)})),s(`
|
|
### done ###
|
|
`),U;function h(e,o){!e&&q&&console.error(o||"assertion failed")}function ve(e){q&&console.log(e)}function s(...e){q&&console.log(...e)}function ne(...e){q&&console.log(...e)}function re(e){return r.relative(u,r.dirname(e)+r.sep+r.basename(e).replace(/\.d\.ts$/,""))}function K(e){return e===i?m:ie(e)}function ie(e){return C+A+N+ye(re(e))}function Z(e){return ie(i)+N+C+N+e}function ye(e){return e.replace(/\.\./g,"--").replace(/[\\\/]/g,N)}function we(e){let o=d;return(e.length===0?"":o+e.join(c+o))+c}function $e(e,o){let n="";return n+="declare module '"+K(e)+"' {"+c,n+=we(o),n+="}"+c,n}async function ke(e){let o=re(e);s("%s (%s)",o,e);let n={file:e,name:o,indent:d,exp:K(e),refs:[],externalImports:[],relativeImports:[],exports:[],lines:[],fileExists:!0,importLineRef:[],relativeRef:[]};try{let l=new URL(e),f=new URL(i);if(l.origin!==f.origin&&l.origin===window.location.origin)throw s(" X - Invalid URL: %s",e),new Error}catch{return n}let a=await fetch(e);if(!a.ok&&(e=e+"/index.d.ts",a=await fetch(e),!a.ok))return s(" X - File not found: %s",e),n.fileExists=!1,n;let p=(await a.text()).replace(Fe,"").replace(/\s*$/,"");if(p.includes(I)){let l=e.substring(0,e.lastIndexOf("/"))+"/";p=p.replace(new RegExp(We(I)+"(.*)","g"),f=>r.relative(l,f))}n.indent=d||" ";let y=[],D,Q=!1,le=()=>{y.length>0&&(/^[ \t]*\/\*\*/.test(y[0])?D=y:z&&y.forEach(l=>n.lines.push({original:l})),y=[]),Q=!1},Be=()=>{D&&(D.forEach(l=>{let f=l.match(/^([ \t]*)(\*.*)/);f?n.lines.push({original:f[1]+" "+f[2]}):n.lines.push({original:l})}),D=null)};for(let l of p.split(`
|
|
`)){let f;if(/^[((=====)(=*)) \t]*\*+\//.test(l)){y.push(l),le();continue}if(/^[ \t]*\/\*/.test(l)){y.push(l),Q=!0,/\*+\/[ \t]*$/.test(l)&&le();continue}if(Q){y.push(l);continue}if(/^\s*$/.test(l)){n.lines.push({original:""});continue}if(/^\/\/\//.test(l)){let k=Le(l);if(k){let g=r.resolve(r.dirname(e),k);if(V(g))s(" - reference source typing %s (%s)",k,g);else{let E=r.relative(u,g).replace(/\\/g,"/");s(" - reference external typing %s (%s) (relative: %s)",k,g,E),Ue(g)||R.push(g)}w(n.refs,g);continue}}if(/^\/\//.test(l)){z&&n.lines.push({original:l});continue}if(De.test(l)){D=null;continue}if(Be(),l.indexOf("from")>=0&&(f=l.match(he))||l.indexOf("require")>=0&&(f=l.match(Y))){let[k,g,E,x,S]=f;h(x);let j=r.resolve(r.dirname(e),x);if(j.startsWith("/")&&(j=j.replace("/https:/","https://").replace(".js",".d.ts")),Te.test(x)||x.startsWith(I)){let M={original:g+E+K(j)+S};n.lines.push(M);let P=j;(await fetch(P)).ok||(P+=".d.ts"),s(" - import relative %s (%s)",x,P),w(n.relativeImports,P),n.importLineRef.push(M)}else{let M={original:l};s(" - import external %s",x),w(n.externalImports,x),W&&n.importLineRef.push(M),n.lines.push(M)}}else if(f=l.match(fe)){let[k,g,E,x,S]=f;h(x),s(" - declare %s",x),w(n.exports,x);let j={original:l};n.relativeRef.push(j),n.lines.push(j)}else{if(f=l.match(Me)){let[k,g,E,x,S,j]=f;l=g+E+S+j}V(e)?n.lines.push({original:l.replace(/^(export )?declare /g,"$1")}):n.lines.push({original:l})}}return n}}function w(t,r){return t.indexOf(r)<0&&t.push(r),t}function Ie(t,...r){return r.forEach(i=>i.forEach(m=>w(t,m))),t}function Re(t){return'/// <reference path="'+t.replace(/\\/g,"/")+'" />'}function Le(t){let r=t.match(qe);return r?r[2]:null}function Oe(t,r){let i=t.match(Y);return i&&ee.test(i[3])?i[1]+i[2]+r(i[3])+i[4]:t}function Se(t,r){if(t.indexOf("from")<0)return t;let i=t.match(he);return i&&ee.test(i[3])?i[1]+i[2]+r(i[3])+i[4]:t}function Pe(t,r){let i=t.match(fe);if(i){let[m,u,c,d,C]=i;if(ee.test(d))return u+c+r(d)+C}return t}function ue(t,r){return i=>i.modified||i.original}function b(t,r){return typeof t>"u"?r:t}function We(t){return t.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")}export{Ge as bundle};
|