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.macroVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of codegen macro to generate a string constant at build time using a template literal.
Ensure the import statement matches the expected syntax and that babel-plugin-macros is properly configured.
Use `module.exports = ...` inside the generated code string.
Check babel-plugin-codegen documentation for alternative APIs if needed.
Install babel-plugin-macros and add it to your Babel config (e.g., in .babelrc or babel.config.js).
Ensure the generated code is syntactically correct JavaScript.