{"id":22195,"library":"rollup-plugin-regexp","title":"rollup-plugin-regexp","description":"A Rollup plugin for dynamic find-and-replace operations in bundled output using regular expressions or string matching. Version 5.0.1 is the latest stable release, published with a moderate cadence. Unlike static string replacement plugins, it supports RegExp patterns and replacement functions, enabling codemods and dynamic content injection. Ships TypeScript types and uses magic-string for accurate sourcemap preservation. A key differentiator is the shorthand syntax allowing key-value pairs for multiple replacements.","status":"active","version":"5.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/kingyue737/rollup-plugin-modify","tags":["javascript","rollup-plugin","replace","find","regex","regexp","modify","codemod","magic-string","typescript"],"install":[{"cmd":"npm install rollup-plugin-regexp","lang":"bash","label":"npm"},{"cmd":"yarn add rollup-plugin-regexp","lang":"bash","label":"yarn"},{"cmd":"pnpm add rollup-plugin-regexp","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used internally for plugin utilities like creating a filter from include/exclude patterns","package":"@rollup/pluginutils","optional":false},{"reason":"Handles source transformations while preserving sourcemaps","package":"magic-string","optional":false}],"imports":[{"note":"Package is ESM-only; no CommonJS export. Use dynamic import if needed in CJS context.","wrong":"const regexpPlugin = require('rollup-plugin-regexp')","symbol":"default (rollup-plugin-regexp)","correct":"import regexpPlugin from 'rollup-plugin-regexp'"},{"note":"Named export is available but default export is more commonly used. Both work identically.","wrong":"import RegexpPlugin from 'rollup-plugin-regexp'","symbol":"RegexpPlugin (named export)","correct":"import { RegexpPlugin } from 'rollup-plugin-regexp'"},{"note":"Types are exported from the package, so no separate @types/ package is needed.","symbol":"TypeScript types","correct":"import type { RollupRegexpOptions } from 'rollup-plugin-regexp'"}],"quickstart":{"code":"import regexpPlugin from 'rollup-plugin-regexp';\nimport { defineConfig } from 'rollup';\n\nexport default defineConfig({\n  input: 'src/index.js',\n  output: {\n    dir: 'dist',\n    format: 'esm'\n  },\n  plugins: [\n    regexpPlugin({\n      find: /process\\.env\\.MY_VAR/g,\n      replace: () => JSON.stringify(process.env.MY_VAR ?? 'default')\n    }),\n    // Multiple replacements shorthand:\n    regexpPlugin({\n      '__BUILD_TIME__': new Date().toISOString(),\n      '__VERSION__': '1.0.0'\n    })\n  ]\n});","lang":"typescript","description":"Shows default import, a regex replacement using a function, and the shorthand multiple replacement syntax."},"warnings":[{"fix":"Use import syntax or downgrade to 'rollup-plugin-regexp@4'","message":"Version 5.0.0 dropped support for CommonJS (require). If using Node.js <12 or bundling for CommonJS, pin to version 4.x.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Update imports from 'rollup-plugin-modify' to 'rollup-plugin-regexp'","message":"Prior to v5, package was named 'rollup-plugin-modify'. The package was renamed; old imports break.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Ensure replacement function correctly handles all parameters: match, p1, p2, ..., offset, string.","message":"Replacement function receives the match array from String.prototype.replace(). If using a global regex, the function is called multiple times but the 'offset' parameter may be off if replacements change string length.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use external filter plugins or omit; the plugin only transforms matched files.","message":"The 'include' and 'exclude' options (from @rollup/pluginutils) were deprecated in v5 in favor of Rollup's built-in filter plugins like 'rollup-plugin-filter'.","severity":"deprecated","affected_versions":">=5.0.0"},{"fix":"Order plugins carefully or consolidate replacements into a single instance.","message":"When using multiple instances with overlapping patterns, execution order matters and may cause unintended replacements if one instance modifies the same code as another.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Use dynamic import: const regexpPlugin = await import('rollup-plugin-regexp'); or use import syntax. Alternatively, downgrade to v4.","cause":"Attempting to require() an ESM-only package in a CommonJS module.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module [...] not supported."},{"fix":"Ensure import is default: import regexpPlugin from 'rollup-plugin-regexp' (no curly braces).","cause":"Wrong import style – likely using default import when package was required as an object.","error":"TypeError: regexpPlugin is not a function"},{"fix":"Check that 'find' is a string or RegExp, and 'replace' is a string or function.","cause":"Passing an undefined or non-string value as 'find' or 'replace' option.","error":"Cannot read properties of undefined (reading 'replace')"},{"fix":"Ensure 'find' is a string or RegExp. If using shorthand syntax, keys must be strings.","cause":"Passing an object or boolean to 'find'.","error":"The 'regexpPlugin' option 'find' must be a string or RegExp."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}