{"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,"search_vec":"'adopt':92 'allow':21 'babel':6,17,67,73,84,116,120 'babel-macro':119 'babel-plugin-codegen':66 'babel-plugin-macro':16,83,115 'build':31 'cadenc':113 'code':13,25,29,46,57 'codegen':69 'codegen.macro':1,2,76 'compil':11 'compile-tim':10 'configur':75,87 'depend':103 'direct':72 'dynam':60 'easier':90 'embed':59 'enabl':9 'evalu':35 'exist':94 'follow':109 'function':42 'generat':14,27,55 'invok':40 'javascript':114 'liter':38 'low':111 'macro':7,19,86,118,121 'make':88 'metaprogram':54 'minim':102 'output':64 'packag':105 'plugin':18,68,74,85,117 'produc':44 'project':80 'releas':112 'repetit':56 'requir':71 'setup':95 'ship':97 'sourc':45 'stabl':107 'static':63 'string':49 'templat':37 'time':12,32 'type':99 'typescript':98,122 'typic':33 'unlik':65 'use':15,52 'v4.1.0':3 'valu':61 'within':78 'work':77 'write':24","created_at":"2026-04-25T11:17:39.472592+00:00","updated_at":"2026-04-25T11:17:39.472592+00:00","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,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":null,"docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/codegen.macro","openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-24","install_tag":null}}