A Rollup plugin for generating source code modules at bundle time. Current stable version is 2.1.0, released with fixes and support for Bun and Parcel loaders. This plugin allows dynamic generation of modules based on configuration, useful for injecting environment variables, version info, or computed code. Key differentiator: it integrates directly into Rollup's build process, avoiding separate pre-build steps. Ships TypeScript types, supports ESM and CJS outputs, requires Node >=16.0. Alternative to virtual modules or manual file writes.
npm install rollup-plugin-codegenVerified import paths — ran on the pinned version, not inferred.
Shows how to configure codegen plugin to generate a module with environment variable.
Use `import { codegen } from 'rollup-plugin-codegen'` instead of `import codegen from 'rollup-plugin-codegen'`Upgrade Node.js to version 16 or later.
Ensure each module id is a unique relative path string (e.g., 'src/generated/env').
Change `import codegen from 'rollup-plugin-codegen'` to `import { codegen } from 'rollup-plugin-codegen'`Run `npm install rollup-plugin-codegen` or `yarn add rollup-plugin-codegen`.
Ensure the module id is a relative path that does not conflict with existing files. Use a path like 'src/generated/env' that doesn't exist on disk.