prepr

raw JSON →
2.0.1 verified Fri May 01 auth: no javascript

String preprocessor implementing C/GLSL preprocessor-style directives (#define, #if, #ifdef, #elif, #else, #endif) for JavaScript environments. Version 2.0+ uses ESM and uses 'subscript' for expression evaluation. Designed for glsl-transpiler, it supports variable interpolation and macro functions. Lightweight, no external dependencies. Ideal for preprocessing GLSL shader code or any string that benefits from compile-time conditionals.

error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/prepr/index.js from /path/to/user/code.js not supported.
cause Using CommonJS require() on an ESM-only package after v2.0.0.
fix
Change to import: import prepr from 'prepr' and ensure project type: module or use .mjs extension.
error SyntaxError: Unexpected token 'export'
cause Trying to require() prepr in a CommonJS file without ESM support.
fix
Either convert to ESM or use dynamic import: const prepr = (await import('prepr')).default
breaking v2.0.0 changed from CommonJS to ESM. `require('prepr')` no longer works.
fix Use `import prepr from 'prepr'` in an ESM context or use dynamic import.
breaking v2.0.0 replaced `expression-eval` with `subscript` for expression evaluation. Some edge cases in expressions may behave differently.
fix Test expressions with new evaluator; avoid unsupported features.
gotcha The preprocessor does not support recursive macro expansion or concatenation (e.g., ## operator).
fix Use provided variables or macros directly; do not rely on full C preprocessor.
npm install prepr
yarn add prepr
pnpm add prepr

Demonstrates basic usage of prepr with #define, #ifdef, and variable interpolation.

import prepr from 'prepr';

const input = `
  #define A 2
  #ifdef A
    var a = A;
  #endif
  var b = myVar;
`;

const result = prepr(input, {
  myVar: 1
});

console.log(result);
// Output:
//
//   var a = 2;
//   var b = 1;
//