Registry / serialization / codegen.macro

codegen.macro

JSON →
library4.1.0jsnpmunverified

codegen.macro v4.1.0 is a Babel macro that enables compile-time code generation using babel-plugin-macros. It allows you to write code that generates other code at build time, typically by evaluating a template literal or invoking a function that produces source code as a string. This is useful for metaprogramming, generating repetitive code, or embedding dynamic values into static output. Unlike babel-plugin-codegen, which requires direct Babel plugin configuration, codegen.macro works within any project that has babel-plugin-macros configured, making it easier to adopt in existing setups. It ships TypeScript types and has minimal dependencies. The package is stable and follows a low release cadence.

npm install codegen.macro
INSTALL
IMPORT
SIG · CODEGEN.MACRO
C
codegen.macro
serializationjavascriptv4.1.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

default (codegen)
✓ import codegen from 'codegen.macro'
✗ const codegen = require('codegen.macro')
This package is a Babel macro, so it must be imported with the default import syntax at the module level. Using require() will not trigger the macro transformation.
codegen used as template literal tag
✓ codegen`// some generated code`
The macro is intended to be used as a template literal tag. The content inside the backticks is evaluated at build time and replaced with the generated code.
codegen used as function call
✓ codegen('./generator.js')
✗ codegen('./generator.js') // This is valid
codegen also supports a function call syntax where you pass a path to a file that exports a generator function.

Demonstrates basic usage of codegen macro to generate a string constant at build time using a template literal.

import codegen from 'codegen.macro' const name = 'World' // Generate a greeting at build time codegen` const greeting = 'Hello, ${name}!' module.exports = greeting ` // The above will be replaced during build with: const greeting = 'Hello, World!'
Debug
Known issues
gotchaThe import must be exactly `import codegen from 'codegen.macro'`. Using different import styles or paths will not trigger the macro.
fix
Ensure the import statement matches the expected syntax and that babel-plugin-macros is properly configured.
affects: all
gotchaThe generated code must use CommonJS module.exports to export values, as the macro evaluates the string as a Node.js script.
fix
Use `module.exports = ...` inside the generated code string.
affects: all
deprecatedAll APIs from babel-plugin-codegen are available but the macro only supports a subset. Refer to the babel-plugin-codegen snapshots for full API.
fix
Check babel-plugin-codegen documentation for alternative APIs if needed.
affects: >=1.0.0
Errors
Common errors & fixes
codegen.macro: Macro not found. Did you forget to install babel-plugin-macros?
babel-plugin-macros is not installed or configured in Babel.
fix
Install babel-plugin-macros and add it to your Babel config (e.g., in .babelrc or babel.config.js).
codegen: The code inside the template literal is not valid JavaScript.
The string inside the tagged template literal contains syntax errors.
fix
Ensure the generated code is syntactically correct JavaScript.
Upgrade
Version history
4.1.0latest on npm
Audit
Dependencies
babel-plugin-macrosrequiredRequired runtime dependency for the macro to function; must be installed and configured in Babel.
Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
codegen.macro — npm install codegen.macro · libregistry