Skip to content

Commit 713dc9e

Browse files
fix(minifier): preserve whitespace after #if directives (#24)
1 parent 8ce4ace commit 713dc9e

File tree

3 files changed

+176
-4
lines changed

3 files changed

+176
-4
lines changed

src/minifier.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export function minify(
5353
if (
5454
isSymbol(token.value) &&
5555
((tokens[i - 2]?.value === '#' && tokens[i - 1]?.value === 'include') ||
56+
(tokens[i - 2]?.value === '#' && tokens[i - 1]?.value === 'if') ||
57+
(tokens[i - 2]?.value === '#' && tokens[i - 1]?.value === 'elif') ||
5658
(tokens[i - 3]?.value === '#' && tokens[i - 2]?.value === 'define'))
5759
) {
5860
// Move padding after #define arguments

tests/__snapshots__/index.test.ts.snap

Lines changed: 166 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ const bool c=true;
1010
#endif
1111
uniform float foo,bar;uniform sampler2D map;in vec2 vUv;out vec4 pc_FragColor;
1212
#include <three_test>
13-
layout(std140)uniform Uniforms1{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;};layout(std140)uniform Uniforms2{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;}d;struct e{float intensity;vec3 position;float one,two;};uniform e Light[4];void main(){vec4 f=vec4(Light[0].position.xyz*Light[0].intensity,0.0);vec4 g=projectionMatrix*modelViewMatrix*vec4(0,0,0,1);vec4 h=d.projectionMatrix*d.modelViewMatrix*vec4(0,0,0,1);if(false){}pc_FragColor=vec4(texture(map,vUv).rgb,0.0);float i=0.0;pc_FragColor.a+=1.0+i;}"
13+
layout(std140)uniform Uniforms1{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;};layout(std140)uniform Uniforms2{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;}d;struct e{
14+
#if !defined(BLA)
15+
int y;
16+
#else
17+
float z;
18+
#endif
19+
};struct f{float intensity;vec3 position;float one,two;};uniform f Light[4];void main(){vec4 g=vec4(Light[0].position.xyz*Light[0].intensity,0.0);vec4 h=projectionMatrix*modelViewMatrix*vec4(0,0,0,1);vec4 i=d.projectionMatrix*d.modelViewMatrix*vec4(0,0,0,1);if(false){}pc_FragColor=vec4(texture(map,vUv).rgb,0.0);float j=0.0;pc_FragColor.a+=1.0+j;}"
1420
`;
1521
1622
exports[`minify > can mangle GLSL 2`] = `
@@ -40,7 +46,13 @@ const bool c=true;
4046
#endif
4147
uniform float d,e;uniform sampler2D f;in vec2 g;out vec4 h;
4248
#include <three_test>
43-
layout(std140)uniform i{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;};layout(std140)uniform j{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;}k;struct l{float intensity;vec3 position;float one,two;};uniform l m[4];void main(){vec4 n=vec4(m[0].position.xyz*m[0].intensity,0.0);vec4 o=projectionMatrix*modelViewMatrix*vec4(0,0,0,1);vec4 p=k.projectionMatrix*k.modelViewMatrix*vec4(0,0,0,1);if(false){}h=vec4(texture(f,g).rgb,0.0);float e=0.0;h.a+=1.0+e;}"
49+
layout(std140)uniform i{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;};layout(std140)uniform j{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;}k;struct l{
50+
#if !defined(BLA)
51+
int y;
52+
#else
53+
float z;
54+
#endif
55+
};struct m{float intensity;vec3 position;float one,two;};uniform m n[4];void main(){vec4 o=vec4(n[0].position.xyz*n[0].intensity,0.0);vec4 p=projectionMatrix*modelViewMatrix*vec4(0,0,0,1);vec4 q=k.projectionMatrix*k.modelViewMatrix*vec4(0,0,0,1);if(false){}h=vec4(texture(f,g).rgb,0.0);float e=0.0;h.a+=1.0+e;}"
4456
`;
4557
4658
exports[`minify > can mangle externals in GLSL 2`] = `
@@ -55,7 +67,7 @@ Map {
5567
"Uniforms1" => "i",
5668
"Uniforms2" => "j",
5769
"globals" => "k",
58-
"Light" => "m",
70+
"Light" => "n",
5971
}
6072
`;
6173
@@ -98,7 +110,13 @@ const bool isTest=true;
98110
#endif
99111
uniform float foo,bar;uniform sampler2D map;in vec2 vUv;out vec4 pc_FragColor;
100112
#include <three_test>
101-
layout(std140)uniform Uniforms1{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;};layout(std140)uniform Uniforms2{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;}globals;struct LightData{float intensity;vec3 position;float one,two;};uniform LightData Light[4];void main(){vec4 lightNormal=vec4(Light[0].position.xyz*Light[0].intensity,0.0);vec4 clipPosition=projectionMatrix*modelViewMatrix*vec4(0,0,0,1);vec4 clipPositionGlobals=globals.projectionMatrix*globals.modelViewMatrix*vec4(0,0,0,1);if(false){}pc_FragColor=vec4(texture(map,vUv).rgb,0.0);float bar=0.0;pc_FragColor.a+=1.0+bar;}"
113+
layout(std140)uniform Uniforms1{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;};layout(std140)uniform Uniforms2{mat4 projectionMatrix;mat4 modelViewMatrix;mat3 normalMatrix;float one,two;}globals;struct X{
114+
#if !defined(BLA)
115+
int y;
116+
#else
117+
float z;
118+
#endif
119+
};struct LightData{float intensity;vec3 position;float one,two;};uniform LightData Light[4];void main(){vec4 lightNormal=vec4(Light[0].position.xyz*Light[0].intensity,0.0);vec4 clipPosition=projectionMatrix*modelViewMatrix*vec4(0,0,0,1);vec4 clipPositionGlobals=globals.projectionMatrix*globals.modelViewMatrix*vec4(0,0,0,1);if(false){}pc_FragColor=vec4(texture(map,vUv).rgb,0.0);float bar=0.0;pc_FragColor.a+=1.0+bar;}"
102120
`;
103121
104122
exports[`minify > can minify WGSL 1`] = `"struct LightData{intensity:f32,position:vec3<f32>,one:f32,two:f32,};struct Uniforms{projectionMatrix:mat4x4<f32>,modelViewMatrix:mat4x4<f32>,normalMatrix:mat3x3<f32>,one:f32,two:f32,lights:array<LightData,4>,};@binding(0)@group(0)var<uniform>uniforms:Uniforms;@binding(1)@group(0)var sample:sampler;@binding(2)@group(0)var map:texture_2d<f32>;struct VertexIn{@location(0)position:vec4<f32>,@location(1)uv:vec2<f32>,};struct VertexOut{@builtin(position)position:vec4<f32>,@location(0)uv:vec2<f32>,};@vertex fn vert_main(input:VertexIn)->VertexOut{var output:VertexOut;output.position=input.position;output.uv=input.uv;return output;}@fragment fn frag_main(uv:vec2<f32>)->vec4<f32>{var lightNormal=vec4<f32>(uniforms.lights[0].position*uniforms.lights[0].intensity,0.0);var clipPosition=uniforms.projectionMatrix*uniforms.modelViewMatrix*vec4<f32>(0.0,0.0,0.0,1.0);var color=textureSample(map,sample,uv);color.a+=1.0;return color;}"`;
@@ -911,6 +929,150 @@ exports[`tokenize > can tokenize GLSL 1`] = `
911929
"type": "whitespace",
912930
"value": "
913931
932+
",
933+
},
934+
{
935+
"type": "keyword",
936+
"value": "struct",
937+
},
938+
{
939+
"type": "whitespace",
940+
"value": " ",
941+
},
942+
{
943+
"type": "identifier",
944+
"value": "X",
945+
},
946+
{
947+
"type": "whitespace",
948+
"value": " ",
949+
},
950+
{
951+
"type": "symbol",
952+
"value": "{",
953+
},
954+
{
955+
"type": "whitespace",
956+
"value": "
957+
",
958+
},
959+
{
960+
"type": "symbol",
961+
"value": "#",
962+
},
963+
{
964+
"type": "keyword",
965+
"value": "if",
966+
},
967+
{
968+
"type": "whitespace",
969+
"value": " ",
970+
},
971+
{
972+
"type": "symbol",
973+
"value": "!",
974+
},
975+
{
976+
"type": "keyword",
977+
"value": "defined",
978+
},
979+
{
980+
"type": "symbol",
981+
"value": "(",
982+
},
983+
{
984+
"type": "identifier",
985+
"value": "BLA",
986+
},
987+
{
988+
"type": "symbol",
989+
"value": ")",
990+
},
991+
{
992+
"type": "whitespace",
993+
"value": "
994+
",
995+
},
996+
{
997+
"type": "keyword",
998+
"value": "int",
999+
},
1000+
{
1001+
"type": "whitespace",
1002+
"value": " ",
1003+
},
1004+
{
1005+
"type": "identifier",
1006+
"value": "y",
1007+
},
1008+
{
1009+
"type": "symbol",
1010+
"value": ";",
1011+
},
1012+
{
1013+
"type": "whitespace",
1014+
"value": "
1015+
",
1016+
},
1017+
{
1018+
"type": "symbol",
1019+
"value": "#",
1020+
},
1021+
{
1022+
"type": "keyword",
1023+
"value": "else",
1024+
},
1025+
{
1026+
"type": "whitespace",
1027+
"value": "
1028+
",
1029+
},
1030+
{
1031+
"type": "keyword",
1032+
"value": "float",
1033+
},
1034+
{
1035+
"type": "whitespace",
1036+
"value": " ",
1037+
},
1038+
{
1039+
"type": "identifier",
1040+
"value": "z",
1041+
},
1042+
{
1043+
"type": "symbol",
1044+
"value": ";",
1045+
},
1046+
{
1047+
"type": "whitespace",
1048+
"value": "
1049+
",
1050+
},
1051+
{
1052+
"type": "symbol",
1053+
"value": "#",
1054+
},
1055+
{
1056+
"type": "keyword",
1057+
"value": "endif",
1058+
},
1059+
{
1060+
"type": "whitespace",
1061+
"value": "
1062+
",
1063+
},
1064+
{
1065+
"type": "symbol",
1066+
"value": "}",
1067+
},
1068+
{
1069+
"type": "symbol",
1070+
"value": ";",
1071+
},
1072+
{
1073+
"type": "whitespace",
1074+
"value": "
1075+
9141076
",
9151077
},
9161078
{

tests/index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ const glsl = /* glsl */ `#version 300 es
4040
float one, two;
4141
} globals;
4242
43+
struct X {
44+
#if !defined(BLA)
45+
int y;
46+
#else
47+
float z;
48+
#endif
49+
};
50+
4351
struct LightData {
4452
float intensity;
4553
vec3 position;

0 commit comments

Comments
 (0)