{"id":19410,"library":"codegen.macro","title":"codegen.macro","description":"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.","status":"active","version":"4.1.0","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","babel-plugin-macros","babel-macros","typescript"],"install":[{"cmd":"npm install codegen.macro","lang":"bash","label":"npm"},{"cmd":"yarn add codegen.macro","lang":"bash","label":"yarn"},{"cmd":"pnpm add codegen.macro","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required runtime dependency for the macro to function; must be installed and configured in Babel.","package":"babel-plugin-macros","optional":false}],"imports":[{"note":"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.","wrong":"const codegen = require('codegen.macro')","symbol":"default (codegen)","correct":"import codegen from 'codegen.macro'"},{"note":"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.","wrong":"","symbol":"codegen used as template literal tag","correct":"codegen`// some generated code`"},{"note":"codegen also supports a function call syntax where you pass a path to a file that exports a generator function.","wrong":"codegen('./generator.js') // This is valid","symbol":"codegen used as function call","correct":"codegen('./generator.js')"}],"quickstart":{"code":"import codegen from 'codegen.macro'\n\nconst name = 'World'\n// Generate a greeting at build time\ncodegen`\n  const greeting = 'Hello, ${name}!'\n  module.exports = greeting\n`\n\n// The above will be replaced during build with: const greeting = 'Hello, World!'","lang":"typescript","description":"Demonstrates basic usage of codegen macro to generate a string constant at build time using a template literal."},"warnings":[{"fix":"Ensure the import statement matches the expected syntax and that babel-plugin-macros is properly configured.","message":"The import must be exactly `import codegen from 'codegen.macro'`. Using different import styles or paths will not trigger the macro.","severity":"gotcha","affected_versions":"all"},{"fix":"Use `module.exports = ...` inside the generated code string.","message":"The generated code must use CommonJS module.exports to export values, as the macro evaluates the string as a Node.js script.","severity":"gotcha","affected_versions":"all"},{"fix":"Check babel-plugin-codegen documentation for alternative APIs if needed.","message":"All APIs from babel-plugin-codegen are available but the macro only supports a subset. Refer to the babel-plugin-codegen snapshots for full API.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Install babel-plugin-macros and add it to your Babel config (e.g., in .babelrc or babel.config.js).","cause":"babel-plugin-macros is not installed or configured in Babel.","error":"codegen.macro: Macro not found. Did you forget to install babel-plugin-macros?"},{"fix":"Ensure the generated code is syntactically correct JavaScript.","cause":"The string inside the tagged template literal contains syntax errors.","error":"codegen: The code inside the template literal is not valid JavaScript."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}