From eaa38b260a79fa74672b6a560ebd2920c7522c2f Mon Sep 17 00:00:00 2001 From: Micah Miller-Eshleman Date: Tue, 24 May 2022 21:16:53 -0700 Subject: [PATCH 1/2] Support compressing "else" without curly braces --- index.js | 7 +++++++ test/expected/basic.bundle.js | 2 +- test/fixtures/shaders/basic.glsl | 18 ++++++++++++++++++ test/result/basic.bundle.js | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 86b7883..21feb70 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,13 @@ function compressShader(source) { result.push(line, "\n"); needNewline = false } else { + // Add space after "else" at end of prior line (unless this line starts with bracket) + if (!line.startsWith('{') + && result.length + && result[result.length - 1].endsWith('else') + ) { + result.push(' '); + } result.push(line .replace(/\s*({|}|=|\*|,|\+|\/|>|<|&|\||\[|\]|\(|\)|\-|!|;)\s*/g, "$1")) needNewline = true; diff --git a/test/expected/basic.bundle.js b/test/expected/basic.bundle.js index bfa7f87..87b0de5 100644 --- a/test/expected/basic.bundle.js +++ b/test/expected/basic.bundle.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - var shaderSource = "#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec3 aVertexPosition;attribute vec3 aVertexNormal;attribute vec2 aTextureCoord;attribute float aFace;uniform mat4 uMVMatrix;uniform mat4 uPMatrix;uniform mat3 uNMatrix;varying vec2 vTextureCoord;varying vec3 vTransformedNormal;varying vec4 vPosition;varying float vFace;void main(void){vPosition=uMVMatrix*vec4(aVertexPosition,1.0);gl_Position=uPMatrix*vPosition;vTextureCoord=aTextureCoord;vTransformedNormal=uNMatrix*aVertexNormal;vFace=aFace;}"; + var shaderSource = "#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec3 aVertexPosition;attribute vec3 aVertexNormal;attribute vec2 aTextureCoord;attribute float aFace;uniform mat4 uMVMatrix;uniform mat4 uPMatrix;uniform mat3 uNMatrix;varying vec2 vTextureCoord;varying vec3 vTransformedNormal;varying vec4 vPosition;varying float vFace;void main(void){vPosition=uMVMatrix*vec4(aVertexPosition,1.0);gl_Position=uPMatrix*vPosition;vTextureCoord=aTextureCoord;vTransformedNormal=uNMatrix*aVertexNormal;vFace=aFace;}float getFace(vec3 direction){vec3 absDirection=abs(direction);float face=-1.0;if(absDirection.x>absDirection.z){if(absDirection.x>absDirection.y)face=direction.x>0.0 ? 0.0 : 3.0;else face=direction.y>0.0 ? 1.0 : 4.0;}else{if(absDirection.z>absDirection.y)face=direction.z>0.0 ? 2.0 : 5.0;else face=direction.y>0.0 ? 1.0 : 4.0;}return face;}"; assert.equal(typeof shaderSource, 'string'); const lines = shaderSource.split(/\n/); diff --git a/test/fixtures/shaders/basic.glsl b/test/fixtures/shaders/basic.glsl index 117b52a..cae66e5 100644 --- a/test/fixtures/shaders/basic.glsl +++ b/test/fixtures/shaders/basic.glsl @@ -25,3 +25,21 @@ void main(void) { vTransformedNormal = uNMatrix * aVertexNormal; vFace = aFace; } + +// Copied from ThreeJS (else without curly braces) +float getFace( vec3 direction ) { + vec3 absDirection = abs( direction ); + float face = - 1.0; + if ( absDirection.x > absDirection.z ) { + if ( absDirection.x > absDirection.y ) + face = direction.x > 0.0 ? 0.0 : 3.0; + else + face = direction.y > 0.0 ? 1.0 : 4.0; + } else { + if ( absDirection.z > absDirection.y ) + face = direction.z > 0.0 ? 2.0 : 5.0; + else + face = direction.y > 0.0 ? 1.0 : 4.0; + } + return face; +} \ No newline at end of file diff --git a/test/result/basic.bundle.js b/test/result/basic.bundle.js index bfa7f87..87b0de5 100644 --- a/test/result/basic.bundle.js +++ b/test/result/basic.bundle.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - var shaderSource = "#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec3 aVertexPosition;attribute vec3 aVertexNormal;attribute vec2 aTextureCoord;attribute float aFace;uniform mat4 uMVMatrix;uniform mat4 uPMatrix;uniform mat3 uNMatrix;varying vec2 vTextureCoord;varying vec3 vTransformedNormal;varying vec4 vPosition;varying float vFace;void main(void){vPosition=uMVMatrix*vec4(aVertexPosition,1.0);gl_Position=uPMatrix*vPosition;vTextureCoord=aTextureCoord;vTransformedNormal=uNMatrix*aVertexNormal;vFace=aFace;}"; + var shaderSource = "#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec3 aVertexPosition;attribute vec3 aVertexNormal;attribute vec2 aTextureCoord;attribute float aFace;uniform mat4 uMVMatrix;uniform mat4 uPMatrix;uniform mat3 uNMatrix;varying vec2 vTextureCoord;varying vec3 vTransformedNormal;varying vec4 vPosition;varying float vFace;void main(void){vPosition=uMVMatrix*vec4(aVertexPosition,1.0);gl_Position=uPMatrix*vPosition;vTextureCoord=aTextureCoord;vTransformedNormal=uNMatrix*aVertexNormal;vFace=aFace;}float getFace(vec3 direction){vec3 absDirection=abs(direction);float face=-1.0;if(absDirection.x>absDirection.z){if(absDirection.x>absDirection.y)face=direction.x>0.0 ? 0.0 : 3.0;else face=direction.y>0.0 ? 1.0 : 4.0;}else{if(absDirection.z>absDirection.y)face=direction.z>0.0 ? 2.0 : 5.0;else face=direction.y>0.0 ? 1.0 : 4.0;}return face;}"; assert.equal(typeof shaderSource, 'string'); const lines = shaderSource.split(/\n/); From bce0b28369ddbc92b167e586ef63b51f05104dcf Mon Sep 17 00:00:00 2001 From: Micah Miller-Eshleman Date: Tue, 24 May 2022 21:17:33 -0700 Subject: [PATCH 2/2] Fix typo --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 21feb70..469baf2 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ function compressShader(source) { result.push(line, "\n"); needNewline = false } else { - // Add space after "else" at end of prior line (unless this line starts with bracket) + // Add space after "else" at end of prior line (unless this line starts with curly brace) if (!line.startsWith('{') && result.length && result[result.length - 1].endsWith('else')