esbuild-plugin-glsl
Version:
An esbuild plugin that adds support for shader imports.
16 lines (15 loc) • 2.45 kB
JavaScript
/**
* esbuild-plugin-glsl v1.4.2 build Sat Jun 27 2026
* https://github.com/vanruesc/esbuild-plugin-glsl
* Copyright 2020 Raoul van Rüschen
* @license Zlib
*/
function y(e,o=null){let l=/(?:\/\*[\s\S]*?\*\/)|(?:\/\/.*(?:\n|$))/g,s=/(?:\/[/*]!)|(?:@license)|(?:@preserve)/m;if(o===null)return e.replace(l,"");let n=0;for(let a of e.matchAll(l))if(s.test(a[0])){let i=`[[LEGAL_COMMENT_${n++}]]`;o.push({contents:a[0],placeholder:i}),e=e.replace(a[0],i)}else e=e.replace(a[0],"");return e}function L(e,o){let l=/\s*([{}=*,+/><&|[\]()\\!?:;-])\s*/g,s=/(\w<\w+>)\s*(\w)/g,n=e.replace(/\r/g,""),a=[];n=y(n,o?a:null);let i=!0,c=!1;n=n.split(/\n+/).reduce((r,t)=>(t=t.trim().replace(/\s{2,}|\t/," "),t.startsWith("#")?(c&&r.push(`
`),r.push(t,`
`),c=!1):t.length>0&&(t=t.replace(l,"$1"),!i&&/\w/.test(t[0])&&(t=" "+t),t=t.replace(s,"$1 $2"),r.push(t),i=!/\w/.test(t[t.length-1]),c=!0),r),[]).join("");for(let r of a)n=n.replace(r.placeholder,`
${r.contents.trim()}
`);return n.replace(/\n{2,}/g,`
`).trim()}import*as w from"fs";import*as O from"util";import*as u from"path";var C=O.promisify(w.readFile);async function h(e,o,l){let s=await C(e,"utf8");if(!l)return{contents:s};let n=[],a=[],i=new Set,c=/^[ \t]*#include +["']([.\\/\w-]+)["']/gm,r=c.exec(s);for(;r!==null;){let t=r[0],d=r[1],g=u.join(u.dirname(e),d);try{let f=o.get(g);if(f===void 0){let m=await h(g,o,l);m.warnings?.forEach(p=>a.push(p)),m.watchFiles?.forEach(p=>i.add(p)),f=m.contents,o.set(g,f)}n.push({file:g,contents:f,target:t}),i.add(g),r=c.exec(s)}catch{if(r===null)break;let m=s.lastIndexOf(`
`,r.index)+1,p=s.indexOf(`
`,r.index),x=s.slice(m,p===-1?void 0:p);a.push({text:`File from <${r[0].trim()}> not found`,location:{file:e,line:s.slice(0,m).split(`
`).length,length:d.length,column:x.indexOf(d),lineText:x}}),n.push({file:g,contents:"",target:r[0]}),r=c.exec(s)}}for(let t of n)s=s.replace(t.target,t.contents);return o.set(e,s),{contents:s,warnings:a,watchFiles:[...i]}}function S(e){return e.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$\{/g,()=>"\\${")}function v(e){return{name:"glsl",setup(o){async function l(s){let n=await h(s.path,new Map,e?.resolveIncludes??!0);n.loader="js";let a=e?.minify??o.initialOptions.minify??!1,i=e?.preserveLegalComments??o.initialOptions.legalComments!=="none",c=a?L(n.contents,i):n.contents;return n.contents=`export default \`${S(c)}\``,n}o.onLoad({filter:/\.(?:frag|vert|glsl|wgsl)$/},l)}}}export{v as default,v as glsl};