spglsl
raw JSON → 0.3.1 verified Fri May 01 auth: no javascript maintenance
spglsl (v0.3.1) is a WebGL GLSL minifier and compiler based on Google ANGLE. It preprocesses directives, removes whitespace, applies constant folding, and mangles non-exported functions and variable names. Optimized for js13k game jams and similar size-constrained WebGL projects, it is distributed as a precompiled WASM binary via npm, runs on Node.js (>=14.17.4), and includes Rollup/Vite plugins. Unlike other GLSL minifiers (e.g., glsl-minify), spglsl provides full ANGLE-based compilation validation and controllable mangling via globals map. Release cadence is low; last major update 2022.
Common errors
error Cannot find module 'spglsl' ↓
cause Package not installed or path incorrect.
fix
Run 'npm install spglsl' and ensure it's in node_modules.
error spglslAngleCompile is not a function ↓
cause Incorrect import (default import instead of named import).
fix
Use 'import { spglslAngleCompile } from "spglsl";'.
error error: no viable overloaded '=' ↓
cause Unsupported GLSL syntax (e.g., struct assignment).
fix
Rewrite using supported constructs or disable minify.
Warnings
breaking spglsl is WASM-based and may fail on Node.js <14.17.4 due to missing WebAssembly support. ↓
fix Upgrade Node.js to >=14.17.4.
gotcha Max call stack size exceeded error when processing very large shaders (e.g., >1000 lines) due to recursive AST visitor. ↓
fix Split shader into smaller files or increase Node.js --stack-size.
deprecated rollupPluginSpglsl is no longer maintained and may break with Rollup >=3. ↓
fix Use a custom plugin or fork.
Install
npm install spglsl yarn add spglsl pnpm add spglsl Imports
- spglslAngleCompile wrong
const { spglslAngleCompile } = require('spglsl')correctimport { spglslAngleCompile } from 'spglsl' - rollupPluginSpglsl wrong
import rollupPluginSpglsl from 'spglsl'correctimport { rollupPluginSpglsl } from 'spglsl' - SpglslAngleCompileOptions
import type { SpglslAngleCompileOptions } from 'spglsl'
Quickstart
import { spglslAngleCompile } from 'spglsl';
import { readFileSync } from 'fs';
async function main() {
const result = await spglslAngleCompile({
mainFilePath: 'shader.frag',
mainSourceCode: readFileSync('shader.frag', 'utf8'),
minify: true,
mangle: true,
mangle_global_map: { myUniform: 'u', myVarying: 'v' }
});
if (!result.valid) {
console.error(result.infoLog);
throw new Error('Compilation failed');
}
console.log(result.output);
}
main().catch(console.error);