minify glsl code in typescript string literal
npm i -D typescript-glslminify-transformer ts-patch
add following to tsconfig.json
{
"compilerOptions": {
"plugins": [ { "transform": "typescript-glslminify-transformer" } ]
}
}
then use tspc to compile
tspc
if you want use other prefix than /* glsl */
, you can pass it as options
{
"compilerOptions": {
"plugins": [ { "transform": "typescript-glslminify-transformer", "customPrefixes": ["glsl", "wgsl"] } ]
}
}
every string literal with /* glsl */
comment will be minified
const glslString = /* glsl */ "gl_FragColor = vec4(vUv, 0.0, 1.0);";
const glslTemplateString = /* glsl */ `
#define PI 3.141592653589793
precision mediump float;
varying vec2 vUv;
void main () {
#ifdef SOME_DEFINE
someFunction();
#endif
gl_FragColor = vec4(vUv, 0.0, 1.0);
}
`;
const glslTemplateStringWithPlaceholders = /* glsl */`
precision mediump float;
varying vec2 vUv;
void main () {
#ifdef ${"SOME" + "_DEFINE"}
someFunction();
#endif
gl_FragColor = vec4(vUv, ${1}, ${2});
}
`;
will be transformed to
const glslString = /* glsl */ "gl_FragColor=vec4(vUv,0.0,1.0);";
const glslTemplateString = /* glsl */ "#define PI 3.141592653589793\nprecision mediump float;varying vec2 vUv;void main () {\n#ifdef SOME_DEFINE\nsomeFunction();\n#endif\ngl_FragColor=vec4(vUv,0.0,1.0);}\n";
const glslTemplateStringWithPlaceholders = /* glsl */ `precision mediump float;varying vec2 vUv;void main () {\n#ifdef \n${"SOME" + "_DEFINE"}someFunction();\n#endif\ngl_FragColor=vec4(vUv,${1},${2});}\n`;