UNPKG

esbuild-plugin-glsl

Version:
11 lines (10 loc) 1.82 kB
/** * esbuild-plugin-glsl v1.2.2 build Sat Sep 30 2023 * https://github.com/vanruesc/esbuild-plugin-glsl * Copyright 2020 Raoul van Rüschen * @license Zlib */ function L(l){let a=/[ \t]*(?:(?:\/\*[\s\S]*?\*\/)|(?:\/\/.*\n))/g,c=/\s*([{}=*,+/><&|[\]()\\!?:;-])\s*/g,e=/(\w<\w+>)\s*(\w)/g,r=l.replace(/\r/g,"").replace(a,""),i=!0,n=!1;return r=r.split(/\n+/).reduce((s,t)=>(t=t.trim().replace(/\s{2,}|\t/," "),t[0]==="#"?(n&&s.push(` `),s.push(t,` `),n=!1):t.length>0&&(t=t.replace(c,"$1"),!i&&/\w/.test(t[0])&&(t=" "+t),t=t.replace(e,"$1 $2"),s.push(t),i=!/\w/.test(t[t.length-1]),n=!0),s),[]).join(""),r.replace(/\n{2,}/g,` `)}import*as y from"fs";import*as O from"util";import*as h from"path";var S=O.promisify(y.readFile);async function w(l,a,c){let e=await S(l,"utf8");if(!c)return{contents:e};let r=[],i=[],n=new Set,s=/#include +["']([.\\/\w-]+)["']/g,t=/\r|\n|\r\n/g,o=s.exec(e);for(;o!==null;){let d=o[0],m=o[1],g=h.join(h.dirname(l),m);try{let p=a.get(g);if(p===void 0){let u=await w(g,a,c);u.warnings?.forEach(f=>i.push(f)),u.watchFiles?.forEach(f=>n.add(f)),p=u.contents,a.set(g,p)}r.push({file:g,contents:p,target:d}),n.add(g),o=s.exec(e)}catch{if(o===null)break;let u=e.split(t),f=u.indexOf(o[0]),x=u[f];i.push({text:`File from <${o[0]}> not found`,location:{file:m,line:f+1,length:m.length,column:x.indexOf(m),lineText:x}}),r.push({file:g,contents:"",target:o[0]}),o=s.exec(e)}}for(let d of r)e=e.replace(d.target,d.contents);return a.set(l,e),{contents:e,warnings:i,watchFiles:[...n]}}function E({minify:l=!1,resolveIncludes:a=!0}={}){let c=new Map;return{name:"glsl",setup(e){async function r(i){let{contents:n,warnings:s,watchFiles:t}=await w(i.path,c,a);return{contents:l?L(n):n,warnings:s,watchFiles:t,loader:"text"}}e.onLoad({filter:/\.(?:frag|vert|glsl|wgsl)$/},r)}}}export{E as default,E as glsl};