rollup-plugin-re

raw JSON →
1.0.7 verified Mon Apr 27 auth: no javascript deprecated

A Rollup plugin for content transformation using regex patterns, string replacements, define macros, and custom transforms. Version 1.0.7 is the latest stable release, but the package appears unmaintained since 2018. It allows multiple replace patterns, include/exclude filters, and supports replacing by regex, string, file content, or custom transform function. Differentiates from @rollup/plugin-replace by offering pattern-based and macro-based replacements alongside simple string replacements.

error TypeError: replace is not a function
cause Using named import instead of default import.
fix
Use: import replace from 'rollup-plugin-re' (without curly braces).
error Error: Cannot find module 'minimatch'
cause Missing dependency 'minimatch' (it is not a peer dependency but required at runtime).
fix
Run: npm install minimatch --save-dev
error The 'excludes' option is not recognized. Did you mean 'exclude'?
cause Typo in option name: 'excludes' instead of 'exclude'.
fix
Change option name to 'exclude'.
deprecated Package is no longer maintained. Use @rollup/plugin-replace or modern alternatives.
fix Migrate to @rollup/plugin-replace for simple string replacements, or use rollup-plugin-string-replace for regex support.
gotcha The 'excludes' option is misspelled as 'excludes' in code examples, but actually 'exclude' is correct.
fix Use 'exclude' instead of 'excludes' in the plugin options object.
gotcha Patterns with 'match' property are not applied correctly; 'match' is intended for internal file matching but may cause confusion.
fix Avoid using 'match'; use 'include'/'exclude' for file filtering and 'test'/'replace' for content transformation.
gotcha The 'transform' function in a pattern must return a string; returning an object with 'code' and 'map' is not supported.
fix Return only the transformed code string from transform functions.
npm install rollup-plugin-re
yarn add rollup-plugin-re
pnpm add rollup-plugin-re

Basic Rollup setup using rollup-plugin-re for define macros, string replacements, and regex-based replacements.

import { rollup } from 'rollup';
import replace from 'rollup-plugin-re';

rollup({
  input: 'src/main.js',
  plugins: [
    replace({
      include: '**/*.js',
      excludes: 'node_modules/**',
      defines: {
        DEBUG: false,
      },
      replaces: {
        __VERSION__: '1.0.0',
      },
      patterns: [
        {
          test: /process\.env\.NODE_ENV/,
          replace: '"production"',
        },
      ],
    }),
  ],
  output: {
    file: 'bundle.js',
    format: 'cjs',
  },
}).then(() => console.log('done'));