Babel Plugin Codegen

4.1.5 · maintenance · verified Tue Apr 21

Babel-plugin-codegen is a build-time code generation utility for JavaScript and TypeScript projects, currently at stable version 4.1.5. It enables developers to execute synchronous Node.js code during the Babel compilation step, replacing sections of source code with the string output of that execution. Unlike `babel-plugin-preval`, which replaces values, `babel-plugin-codegen` replaces entire code blocks by transforming the generated string into an Abstract Syntax Tree (AST) node. It can be used directly as a Babel plugin or integrated via `babel-plugin-macros` for a more flexible, macro-style API, supporting template literals, special `codegen:` import comments, and `codegen.require()` calls. The package has a slow release cadence, with the last update in September 2021, suggesting a maintenance phase focusing on stability and bug fixes rather than active feature development. This tool is particularly useful for generating boilerplate, adapting to environmental configurations, or consolidating dynamic code into static bundles during the build process, reducing runtime overhead.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `babel-plugin-codegen` via its macro API to include external TypeScript code into your main file at build time. It shows the necessary Babel configuration, a source file whose content will be 'codegenned,' and how the main application file uses the `codegen` macro to perform the build-time code injection and subsequent usage.

{
  "// babel.config.js": "",
  "module.exports": "{\n  plugins: [\n    'babel-plugin-macros', // Required to use `codegen/macro`\n    // Alternatively, 'babel-plugin-codegen' if not using the macro\n  ],\n  presets: [\n    '@babel/preset-typescript',\n    ['@babel/preset-env', { targets: { node: 'current' } }],\n  ],\n};",
  "// src/dynamic-data.ts": "",
  "export const GREETING = \"Hello from codegen!\";\nexport function getCurrentTime(): string {\n  return new Date().toLocaleTimeString();\n}\n",
  "// src/index.ts": "",
  "import codegen from 'babel-plugin-codegen/macro';\nimport * as path from 'path';\n\n// This entire `codegen` block will be replaced at build time\n// with the content of `src/dynamic-data.ts`.\ncodegen`\n  const fs = require('fs');\n  const pathToCode = path.resolve(__dirname, './dynamic-data.ts');\n  const fileContent = fs.readFileSync(pathToCode, 'utf8');\n  // The script's module.exports becomes the replacement code.\n  module.exports = fileContent;\n`;\n\n// After Babel processing, the above codegen block is replaced, and\n// these generated exports become directly available in the file.\nconsole.log(GREETING); // "Hello from codegen!"\nconsole.log(`Current time generated: ${getCurrentTime()}`);\n\n// To compile and run:\n// 1. npm install --save-dev @babel/cli @babel/core @babel/preset-env @babel/preset-typescript babel-plugin-codegen babel-plugin-macros\n// 2. npx babel src/index.ts --out-file dist/index.js --extensions ".ts"\n// 3. node dist/index.js"
}

view raw JSON →