esbuild-plugin-glsl
Advanced tools
Comparing version
/** | ||
* esbuild-plugin-glsl v1.2.3 build Sat Mar 08 2025 | ||
* esbuild-plugin-glsl v1.3.0 build Sat Mar 15 2025 | ||
* https://github.com/vanruesc/esbuild-plugin-glsl | ||
@@ -7,5 +7,6 @@ * Copyright 2020 Raoul van Rüschen | ||
*/ | ||
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.startsWith("#")?(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}; | ||
function E(e,o=null){let c=/^[ \t]*(?:(?:\/\*[\s\S]*?\*\/)|(?:\/\/.*\n))/gm,n=/^[ \t]*(?:\/[/*]!)|(?:@license)|(?:@preserve)/m;if(o===null)return e.replace(c,"");let s=0;for(let r of e.matchAll(c))if(n.test(r[0])){let a=`[[LEGAL_COMMENT_${s++}]]`;o.push({contents:r[0],placeholder:a}),e=e.replace(r[0],a)}else e=e.replace(r[0],"");return e}function L(e,o){let c=/\s*([{}=*,+/><&|[\]()\\!?:;-])\s*/g,n=/(\w<\w+>)\s*(\w)/g,s=e.replace(/\r/g,""),r=[];s=E(s,o?r:null);let a=!0,i=!1;s=s.split(/\n+/).reduce((l,t)=>(t=t.trim().replace(/\s{2,}|\t/," "),t.startsWith("#")?(i&&l.push(` | ||
`),l.push(t,` | ||
`),i=!1):t.length>0&&(t=t.replace(c,"$1"),!a&&/\w/.test(t[0])&&(t=" "+t),t=t.replace(n,"$1 $2"),l.push(t),a=!/\w/.test(t[t.length-1]),i=!0),l),[]).join("");for(let l of r)s=s.replace(l.placeholder,` | ||
${l.contents.trim()}`);return s.replace(/\n{2,}/g,` | ||
`)}import*as O from"fs";import*as y from"util";import*as h from"path";var R=y.promisify(O.readFile);async function w(e,o,c){let n=await R(e,"utf8");if(!c)return{contents:n};let s=[],r=[],a=new Set,i=/#include +["']([.\\/\w-]+)["']/g,l=/\r|\n|\r\n/g,t=i.exec(n);for(;t!==null;){let u=t[0],d=t[1],g=h.join(h.dirname(e),d);try{let f=o.get(g);if(f===void 0){let p=await w(g,o,c);p.warnings?.forEach(m=>r.push(m)),p.watchFiles?.forEach(m=>a.add(m)),f=p.contents,o.set(g,f)}s.push({file:g,contents:f,target:u}),a.add(g),t=i.exec(n)}catch{if(t===null)break;let p=n.split(l),m=p.indexOf(t[0]),x=p[m];r.push({text:`File from <${t[0]}> not found`,location:{file:d,line:m+1,length:d.length,column:x.indexOf(d),lineText:x}}),s.push({file:g,contents:"",target:t[0]}),t=i.exec(n)}}for(let u of s)n=n.replace(u.target,u.contents);return o.set(e,n),{contents:n,warnings:r,watchFiles:[...a]}}function P({minify:e=!1,resolveIncludes:o=!0,preserveLegalComments:c=!0}={}){let n=new Map;return{name:"glsl",setup(s){async function r(a){let{contents:i,warnings:l,watchFiles:t}=await w(a.path,n,o);return{contents:e?L(i,c):i,warnings:l,watchFiles:t,loader:"text"}}s.onLoad({filter:/\.(?:frag|vert|glsl|wgsl)$/},r)}}}export{P as default,P as glsl}; |
@@ -9,3 +9,3 @@ import { Plugin } from "esbuild"; | ||
* | ||
* Default is `false`. | ||
* @defaultValue false | ||
*/ | ||
@@ -15,7 +15,16 @@ minify?: boolean; | ||
* Enables or disables shader include resolution. | ||
* | ||
* When enabled, shaders can be included with the custom `#include "path"` directive. | ||
* | ||
* Default is `true`. | ||
* @defaultValue true | ||
*/ | ||
resolveIncludes?: boolean; | ||
/** | ||
* Enables or disables preservation of legal comments. | ||
* | ||
* Legal comments either start with `//!` or `/*!` or include `@license` or `@preserve`. | ||
* | ||
* @defaultValue true | ||
*/ | ||
preserveLegalComments?: boolean; | ||
} | ||
@@ -28,3 +37,3 @@ /** | ||
*/ | ||
declare function glsl({ minify, resolveIncludes }?: GLSLOptions): Plugin; | ||
declare function glsl({ minify, resolveIncludes, preserveLegalComments }?: GLSLOptions): Plugin; | ||
export { glsl, glsl as default }; |
@@ -9,3 +9,4 @@ import { OnLoadResult } from "esbuild"; | ||
* @param cache - A collection of shader includes that have already been loaded. | ||
* @return An object that contains: | ||
* @param resolveIncludes - Controls whether shader includes should be inlined. | ||
* @return A promise that resolves with an object that contains: | ||
* * `contents`: The parsed code. | ||
@@ -12,0 +13,0 @@ * * `warnings`: Any log messages generated during path resolution. |
/** | ||
* Minifies the given GLSL source code. | ||
* A comment that contains legal information. | ||
*/ | ||
/** | ||
* Minifies the given source code. | ||
* | ||
* Based on https://github.com/vwochnik/rollup-plugin-glsl | ||
* | ||
* @param source The source code. | ||
* @param source - The source code. | ||
* @param preserveLegalComments - Controls whether license comments should be preserved. | ||
* @return The minified code. | ||
*/ | ||
export declare function minifyShader(source: string): string; | ||
export declare function minifyShader(source: string, preserveLegalComments: boolean): string; |
{ | ||
"name": "esbuild-plugin-glsl", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "A GLSL plugin for esbuild.", | ||
@@ -45,3 +45,3 @@ "homepage": "https://github.com/vanruesc/esbuild-plugin-glsl", | ||
"engines": { | ||
"node": ">=18" | ||
"node": ">= 18" | ||
}, | ||
@@ -48,0 +48,0 @@ "ava": { |
@@ -32,10 +32,11 @@ # esbuild-plugin-glsl | ||
| Option | Description | Default | | ||
|--------|-------------|---------| | ||
| minify | Enables or disables basic shader minification. | `false` | | ||
| resolveIncludes | When enabled, shaders can include other shaders with the custom `#include "path"` directive. | `true` | | ||
| Option | Description | Default | | ||
|-----------------------|----------------------------------------------------------------------------------------------|---------| | ||
| minify | Enables or disables basic shader minification. | `false` | | ||
| resolveIncludes | When enabled, shaders can include other shaders with the custom `#include "path"` directive. | `true` | | ||
| preserveLegalComments | Preserves comments that start with `//!` or `/*!` or include `@license` or `@preserve`. | `true` | | ||
### TypeScript | ||
To make the TypeScript compiler know how to handle shader sources, add a `shaders.d.ts` file to your project: | ||
To make the TypeScript compiler know how to handle shader sources, add a `shaders.d.ts` [ambient declaration file](https://basarat.gitbook.io/typescript/type-system/intro/d.ts) to your project: | ||
@@ -42,0 +43,0 @@ ```ts |
Sorry, the diff of this file is not supported yet
11873
18.21%100
21.95%67
1.52%