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.
npm install babel-plugin-codegenVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your Node.js environment to version 10 or newer.
Ensure the string output by your `codegen` script is valid JavaScript for your target environment, or preprocess the generated code if it requires further transpilation (e.g., modern syntax for older browsers).
Rewrite any logic within your codegen scripts to be entirely synchronous. If external data is needed, fetch it as part of your build process *before* Babel runs, or use synchronous file I/O operations.
Only execute trusted code within your `codegen` scripts. Review any external scripts or user-provided input that contributes to the codegen process for potential security vulnerabilities.
Install `babel-plugin-macros` as a development dependency (`npm install --save-dev babel-plugin-macros`) and add it to your Babel configuration's `plugins` array (e.g., `plugins: ['babel-plugin-macros']`).
If using the macro, install `babel-plugin-macros` (`npm i -D babel-plugin-macros`) and add it to your Babel configuration's `plugins` array. If using the plugin directly, ensure 'babel-plugin-codegen' is in your Babel plugins.
Use absolute paths or resolve paths relative to `__dirname` within your codegen script (e.g., `path.resolve(__dirname, './some-code.js')`) to ensure correct file resolution from the Babel execution context.
Ensure the *output* string from your codegen script produces valid JavaScript/TypeScript for its final insertion context. If the target file is an ESM module, the codegen script should output ESM syntax (`export ...`). Verify your Babel presets handle the generated module syntax correctly.
No dependency data recorded yet.