Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minify shaders before embedding into js bundle #141

Open
maksnester opened this issue Sep 17, 2020 · 3 comments
Open

Minify shaders before embedding into js bundle #141

maksnester opened this issue Sep 17, 2020 · 3 comments

Comments

@maksnester
Copy link

Hello, I can't find any example of minifying glsl shaders before injecting them into js bundle when using webpack. Could you please suggest any related repositories?

@Qix-
Copy link

Qix- commented Apr 24, 2021

Not sure there are any. It would require a C-like parser, of which I'm not sure there are (m)any available on npm.

If someone knows of a good one, let me know and I can take a whack at an implementation.

@austintheriot
Copy link

Would also love to see something along these lines!

@Qix-
Copy link

Qix- commented Mar 4, 2022

Hey you're in luck. I needed it recently and after what seemed like ages of searching finally found something.

You'll have to incorporate it into your build somehow (I usually write my build scripts custom) but shouldn't be too difficult to figure out. The plugin works with esbuild typically but in my case I had a niche usecase (I wanted to transform glsl tagged template literals, so I wrote my own acorn parser). If you use esbuild and separate files then it might work out of the box for you. Otherwise, use the code below to manually invoke the minifier, even if you don't use esbuild.

{
    "dependencies": {
        "@befunky/esbuild-plugin-glslify-minify": "1.2.0"
    }
}
import GlslMinifier from '@befunky/esbuild-plugin-glslify-minify/lib/minify.js';

async function minifyGLSL(text) {
	const minifier = new GlslMinifier({
		output: 'sourceOnly',
		stripVersion: false,
		nomangle: ['BL_NUM_STEPS'], // List of identifiers not to mangle; set as empty array if you don't care
		preserveUniforms: true,
		preserveDefines: false,
		preserveVariables: false
	});

	const result = await minifier.execute(text);
	return result.sourceCode;
}

Works quite well for me.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants