Registry / serialization / glsl-transpiler-fork-for-gm

glsl-transpiler-fork-for-gm

JSON →
library2.0.21jsnpmunverified

Transforms GLSL shader source code into optimized JavaScript, converting vectors and matrices to arrays, expanding swizzles, and applying expression optimizations. The latest version (2.0.21) ships as an ESM package with optional CommonJS support. It works both as a programmatic API and as a stream transform, integrating with glsl-parser and glsl-tokenizer. Key differentiators include runtime variable replacement for uniforms/attributes/varyings, built-in stdlib for environment compatibility, debug facilities (print/show), and support for GLSL ES 100 and 300. Alternatives like glsl.js or raw string manipulation lack its AST-level optimization and streaming pipeline. The package is a fork of the original stackgl/glsl-transpiler with ongoing maintenance.

npm install glsl-transpiler-fork-for-gm
INSTALL
IMPORT
SIG · GLSL-TRANSPILER-FO
G
glsl-transpiler-fork-for-gm
serializationjavascriptv2.0.21
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import GLSL from 'glsl-transpiler'
const GLSL = require('glsl-transpiler')
Package is ESM-first; CommonJS require works but is discouraged.
compileStream
import compile from 'glsl-transpiler/stream.js'
const compile = require('glsl-transpiler/stream')
Stream subpath must include .js extension and is ESM-only.
GLSL type
import type { CompilerOptions } from 'glsl-transpiler'
TypeScript users should import types from the package.

Shows how to transpile a basic vertex shader to JavaScript with uniforms/attributes as object properties.

import GLSL from 'glsl-transpiler'; const compile = GLSL({ uniform: (name) => `uniforms.${name}`, attribute: (name) => `attributes.${name}`, version: '300 es', optimize: true }); const result = compile(` precision mediump float; attribute vec2 uv; attribute vec4 color; varying vec4 fColor; uniform vec2 uScreenSize; void main(void) { fColor = color; vec2 position = vec2(uv.x, -uv.y) * 1.0; position.x *= uScreenSize.y / uScreenSize.x; gl_Position = vec4(position, 0, 1); } `); console.log('Compiled JS:', result);
Debug
Known issues
breakingVersion 2.x drops Node.js 10 support and requires ESM imports or newer Node.js with --experimental-modules.
fix
Use Node.js >=12 or enable ESM compatibility.
affects: >=2.0.0
deprecatedThe stream API import path changed from 'glsl-transpiler/stream' to 'glsl-transpiler/stream.js'.
fix
Update import to include .js extension.
affects: >=2.0.0
gotchaThe default export is a factory function, not a class. Calling new GLSL() will cause a TypeError.
fix
Call GLSL(options) directly without new.
affects: >=1.0.0
gotchatexture2D expects an ndarray or an object with width/height properties, not a plain array.
fix
Pass texture data as ndarray or provide width and height fields.
affects: >=1.0.0
deprecatedThe 'includes' option now expects an object with function names to include, not just a boolean.
fix
Pass an object like { normalize: false } instead of false.
affects: >=2.0.0
breakingRemoved support for GLSL version '100 es' in favor of '100' and '300 es'.
fix
Use version '100' or '300 es'.
affects: >=2.0.0
Errors
Common errors & fixes
Cannot find module 'glsl-transpiler' or its corresponding type declarations.
Package is ESM-only and may not be resolved correctly in a CJS project or TypeScript without proper module configuration.
fix
Set "type": "module" in package.json or use .mjs extension for import.
TypeError: GLSL is not a constructor
Using new with the default export, which is a factory function.
fix
Remove new: const compile = GLSL(options);
Error: Invalid GLSL version. Must be '100' or '300 es'.
Passing '100 es' as version, which is deprecated.
fix
Use '100' or '300 es' instead.
TypeError: Cannot read properties of undefined (reading 'type')
Missing or incorrect options passed to the compiler; often the uniform/attribute callback returns undefined.
fix
Ensure callbacks return a string that resolves to a valid JS expression.
Upgrade
Version history
2.0.21latest on npm
Audit
Dependencies
glsl-parserrequiredParse GLSL source into AST for transformation
glsl-tokenizerrequiredTokenize GLSL source before parsing
Agent activity
2 hits · last 30 days
node
2
Resources