Skip to content

Commit

Permalink
🐌 Add minify option
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofugaro committed Oct 14, 2021
1 parent f9ae35e commit 51b56ba
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"type": "module",
"exports": "./src/index.js",
"types": "./src/index.d.ts",
"engines": {
"node": ">=12"
},
Expand All @@ -36,4 +37,4 @@
"esbuild": "^0.13.4",
"glsl-noise": "^0.0.0"
}
}
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ build({
}).catch(() => process.exit(1))
```

You can also minify the imported shaders with `glslifyInline({ minify: true })`.

After that, you can use glslify normally with esbuild:

```js
Expand Down
6 changes: 5 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Plugin } from 'esbuild'

declare function glslInline(): Plugin
type Options = {
minify?: boolean
}

declare function glslInline(options?: Options): Plugin
export { glslInline }
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import fs from 'fs/promises'
// a string and use it in another. Read more at:
// https://github.com/onnovisser/babel-plugin-glsl#imported-function-names
import compile from 'babel-plugin-glsl/lib/compile.js'
import { minifyShader } from './minifyShader.js'

export function glslifyInline() {
export function glslifyInline({ minify = false } = {}) {
return {
name: 'glslifyInline',
setup(build) {
Expand Down Expand Up @@ -36,7 +37,10 @@ export function glslifyInline() {
return cache[glslifyImport]
}

const contents = compile(glslifyImport)
let contents = compile(glslifyImport)
if (minify) {
contents = minifyShader(contents)
}
cache[glslifyImport] = contents
return contents
})
Expand Down
41 changes: 41 additions & 0 deletions src/minifyShader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Minifies the given GLSL source code.
*
* Based on https://github.com/vwochnik/rollup-plugin-glsl
* Pasted from https://github.com/vanruesc/esbuild-plugin-glsl/blob/main/src/minifyShader.ts
*
* @param source The source code.
* @return The minified code.
*/

export function minifyShader(source) {
const commentsRegExp = /[ \t]*(?:(?:\/\*[\s\S]*?\*\/)|(?:\/\/.*\n))/g
const symbolsRegExp = /\s*({|}|=|\*|,|\+|\/|>|<|&|\||\[|\]|\(|\)|-|!|;)\s*/g

let result = source.replace(/\r/g, '').replace(commentsRegExp, '')
let wrap = false

result = result
.split(/\n+/)
.reduce((acc, line) => {
line = line.trim().replace(/\s{2,}|\t/, ' ')

if (line[0] === '#') {
if (wrap) {
acc.push('\n')
}

acc.push(line, '\n')
wrap = false
} else {
line = line.replace(/(else)$/m, '$1 ')
acc.push(line.replace(symbolsRegExp, '$1'))
wrap = true
}

return acc
}, [])
.join('')

return result.replace(/\n{2,}/g, '\n')
}
32 changes: 32 additions & 0 deletions test/snapshots/test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,35 @@ Generated by [AVA](https://avajs.dev).
\`;␊
console.log(head, main);␊
`

## works with a basic example but minified

> Snapshot 1
`// test/fixtures/basic.js␊
var head = \`␊
uniform float time;␊
uniform float speed;␊
vec3 mod289_2482186953(vec3 x){return x-floor(x*(1.0/289.0))*289.0;}vec4 mod289_2482186953(vec4 x){return x-floor(x*(1.0/289.0))*289.0;}vec4 permute_2482186953(vec4 x){return mod289_2482186953(((x*34.0)+1.0)*x);}vec4 taylorInvSqrt_2482186953(vec4 r){return 1.79284291400159-0.85373472095314*r;}float noise(vec3 v){const vec2 C=vec2(1.0/6.0,1.0/3.0);const vec4 D_2482186953=vec4(0.0,0.5,1.0,2.0);vec3 i=floor(v+dot(v,C.yyy));vec3 x0=v-i+dot(i,C.xxx);vec3 g_2482186953=step(x0.yzx,x0.xyz);vec3 l=1.0-g_2482186953;vec3 i1=min(g_2482186953.xyz,l.zxy);vec3 i2=max(g_2482186953.xyz,l.zxy);vec3 x1=x0-i1+C.xxx;vec3 x2=x0-i2+C.yyy;vec3 x3=x0-D_2482186953.yyy;i=mod289_2482186953(i);vec4 p=permute_2482186953(permute_2482186953(permute_2482186953(i.z+vec4(0.0,i1.z,i2.z,1.0))+i.y+vec4(0.0,i1.y,i2.y,1.0))+i.x+vec4(0.0,i1.x,i2.x,1.0));float n_=0.142857142857;vec3 ns=n_*D_2482186953.wyz-D_2482186953.xzx;vec4 j=p-49.0*floor(p*ns.z*ns.z);vec4 x_=floor(j*ns.z);vec4 y_=floor(j-7.0*x_);vec4 x=x_*ns.x+ns.yyyy;vec4 y=y_*ns.x+ns.yyyy;vec4 h=1.0-abs(x)-abs(y);vec4 b0=vec4(x.xy,y.xy);vec4 b1=vec4(x.zw,y.zw);vec4 s0=floor(b0)*2.0+1.0;vec4 s1=floor(b1)*2.0+1.0;vec4 sh=-step(h,vec4(0.0));vec4 a0=b0.xzyw+s0.xzyw*sh.xxyy;vec4 a1_2482186953=b1.xzyw+s1.xzyw*sh.zzww;vec3 p0_2482186953=vec3(a0.xy,h.x);vec3 p1=vec3(a0.zw,h.y);vec3 p2=vec3(a1_2482186953.xy,h.z);vec3 p3=vec3(a1_2482186953.zw,h.w);vec4 norm=taylorInvSqrt_2482186953(vec4(dot(p0_2482186953,p0_2482186953),dot(p1,p1),dot(p2,p2),dot(p3,p3)));p0_2482186953*=norm.x;p1*=norm.y;p2*=norm.z;p3*=norm.w;vec4 m=max(0.6-vec4(dot(x0,x0),dot(x1,x1),dot(x2,x2),dot(x3,x3)),0.0);m=m*m;return 42.0*dot(m*m,vec4(dot(p0_2482186953,x0),dot(p1,x1),dot(p2,x2),dot(p3,x3)));}␊
\`;␊
var main = \`␊
vec3 displacement = normal * noise(vec3(uv, time * speed));␊
\`;␊
console.log(head, main);␊
`

## works with a basic example but minified

> Snapshot 1
`// test/fixtures/basic.js␊
var head = \`␊
uniform float time;␊
uniform float speed;␊
vec3 mod289_2482186953(vec3 x){return x-floor(x*(1.0/289.0))*289.0;}vec4 mod289_2482186953(vec4 x){return x-floor(x*(1.0/289.0))*289.0;}vec4 permute_2482186953(vec4 x){return mod289_2482186953(((x*34.0)+1.0)*x);}vec4 taylorInvSqrt_2482186953(vec4 r){return 1.79284291400159-0.85373472095314*r;}float noise(vec3 v){const vec2 C=vec2(1.0/6.0,1.0/3.0);const vec4 D_2482186953=vec4(0.0,0.5,1.0,2.0);vec3 i=floor(v+dot(v,C.yyy));vec3 x0=v-i+dot(i,C.xxx);vec3 g_2482186953=step(x0.yzx,x0.xyz);vec3 l=1.0-g_2482186953;vec3 i1=min(g_2482186953.xyz,l.zxy);vec3 i2=max(g_2482186953.xyz,l.zxy);vec3 x1=x0-i1+C.xxx;vec3 x2=x0-i2+C.yyy;vec3 x3=x0-D_2482186953.yyy;i=mod289_2482186953(i);vec4 p=permute_2482186953(permute_2482186953(permute_2482186953(i.z+vec4(0.0,i1.z,i2.z,1.0))+i.y+vec4(0.0,i1.y,i2.y,1.0))+i.x+vec4(0.0,i1.x,i2.x,1.0));float n_=0.142857142857;vec3 ns=n_*D_2482186953.wyz-D_2482186953.xzx;vec4 j=p-49.0*floor(p*ns.z*ns.z);vec4 x_=floor(j*ns.z);vec4 y_=floor(j-7.0*x_);vec4 x=x_*ns.x+ns.yyyy;vec4 y=y_*ns.x+ns.yyyy;vec4 h=1.0-abs(x)-abs(y);vec4 b0=vec4(x.xy,y.xy);vec4 b1=vec4(x.zw,y.zw);vec4 s0=floor(b0)*2.0+1.0;vec4 s1=floor(b1)*2.0+1.0;vec4 sh=-step(h,vec4(0.0));vec4 a0=b0.xzyw+s0.xzyw*sh.xxyy;vec4 a1_2482186953=b1.xzyw+s1.xzyw*sh.zzww;vec3 p0_2482186953=vec3(a0.xy,h.x);vec3 p1=vec3(a0.zw,h.y);vec3 p2=vec3(a1_2482186953.xy,h.z);vec3 p3=vec3(a1_2482186953.zw,h.w);vec4 norm=taylorInvSqrt_2482186953(vec4(dot(p0_2482186953,p0_2482186953),dot(p1,p1),dot(p2,p2),dot(p3,p3)));p0_2482186953*=norm.x;p1*=norm.y;p2*=norm.z;p3*=norm.w;vec4 m=max(0.6-vec4(dot(x0,x0),dot(x1,x1),dot(x2,x2),dot(x3,x3)),0.0);m=m*m;return 42.0*dot(m*m,vec4(dot(p0_2482186953,x0),dot(p1,x1),dot(p2,x2),dot(p3,x3)));}␊
\`;␊
var main = \`␊
vec3 displacement = normal * noise(vec3(uv, time * speed));␊
\`;␊
console.log(head, main);␊
`
Binary file modified test/snapshots/test.js.snap
Binary file not shown.
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,20 @@ test('works with a basic example', async (t) => {

t.snapshot(generated)
})

test('works with a basic example but minified', async (t) => {
const input = path.resolve(__dirname, './fixtures/basic.js')
const output = path.resolve(__dirname, './fixtures/generated/basic-minified.js')

await esbuild.build({
entryPoints: [input],
outfile: output,
bundle: true,
format: 'esm',
plugins: [glslifyInline({ minify: true })],
})

const generated = await fs.readFile(output, 'utf-8')

t.snapshot(generated)
})

0 comments on commit 51b56ba

Please sign in to comment.