WGSL minifier for WebGPU shaders - WebAssembly build
WGSL minifier for WebGPU shaders - WebAssembly build.
This package provides a WASM build of the wgsl-minifier that runs in browsers and Node.js.
``bash`
npm install wgslmin-wasm
`html`
`javascript
import { initialize, minify } from 'wgslmin-wasm';
await initialize({ wasmURL: '/path/to/wgslmin.wasm' });
const result = minify(source, {
minifyWhitespace: true,
minifyIdentifiers: true,
minifySyntax: true,
});
`
`javascript
const { initialize, minify } = require('wgslmin-wasm');
await initialize(); // Automatically finds wgslmin.wasm
const result = minify(source);
console.log(result.code);
`
Initialize the WASM module. Must be called before minify().
`typescript`
interface InitializeOptions {
wasmURL?: string | URL; // Path or URL to wgslmin.wasm
wasmModule?: WebAssembly.Module; // Pre-compiled module
}
Minify WGSL source code.
`typescript
interface MinifyOptions {
minifyWhitespace?: boolean; // Remove whitespace (default: true)
minifyIdentifiers?: boolean; // Rename identifiers (default: true)
minifySyntax?: boolean; // Optimize syntax (default: true)
mangleExternalBindings?: boolean; // Mangle uniform/storage names (default: false)
keepNames?: string[]; // Names to preserve from renaming
}
interface MinifyResult {
code: string; // Minified code
errors: MinifyError[]; // Parse/minification errors
originalSize: number; // Input size in bytes
minifiedSize: number; // Output size in bytes
}
`
Returns true if the WASM module is initialized.
The version of the minifier.
Remove unnecessary whitespace and newlines.
Rename local variables, function parameters, and helper functions to shorter names. Entry points and API-facing declarations are preserved.
Apply syntax-level optimizations like numeric literal shortening.
Control how var and var names are handled:
- false (default): Original names are preserved in declarations, short aliases are used internally. This maintains compatibility with WebGPU's binding reflection APIs.true
- : Names are mangled directly for smaller output, but breaks binding reflection.
`javascript
// Input
const shader =
@group(0) @binding(0) var
fn getValue() -> f32 { return uniforms * 2.0; };
// With mangleExternalBindings: false (default)
// Output: "@group(0) @binding(0) var
// With mangleExternalBindings: true
// Output: "@group(0) @binding(0) var
`
Array of identifier names that should not be renamed:
`javascript`
minify(source, {
minifyIdentifiers: true,
keepNames: ['myHelper', 'computeValue'],
});
`javascript
import { initialize, minify } from 'wgslmin-wasm';
import wasmURL from 'wgslmin-wasm/wgslmin.wasm?url';
await initialize({ wasmURL });
`
`javascript
import { initialize, minify } from 'wgslmin-wasm';
// Configure webpack to handle .wasm files
await initialize({ wasmURL: new URL('wgslmin-wasm/wgslmin.wasm', import.meta.url) });
`
For better performance when creating multiple instances:
`javascript
const wasmModule = await WebAssembly.compileStreaming(
fetch('/wgslmin.wasm')
);
// Share module across workers
await initialize({ wasmModule });
``
- WASM binary: ~3.6MB
- Initialization: ~100ms (first load)
- Transform: <1ms for typical shaders
MIT