rollup-plugin-path-rewrite

raw JSON →
1.0.2 verified Mon Apr 27 auth: no javascript

A Rollup plugin for rewriting output file or directory paths, primarily used with Rollup's preserveModules mode in ES module builds. Version 1.0.2 (latest, stable) supports Rollup versions 1.x through 4.x via peer dependencies and ships TypeScript definitions. Unlike generic file transformers, it focuses on post-build path manipulation, allowing regex-based path matching and string/function-based replacements. Ideal for reorganizing declaration files or assets in module-preserving output.

error TypeError: pathRewrite is not a function
cause Using named import instead of default import.
fix
Change import { pathRewrite } to import pathRewrite
error Error: Could not resolve 'rollup-plugin-path-rewrite' (missing peer dep rollup)
cause Rollup is not installed as a dev dependency or peer dependency.
fix
Run npm install rollup --save-dev
error InputError: The plugin does not modify files in place; no output directories matched your filter.
cause The `test` regex does not match any generated output paths, or `preserveModules` is not enabled.
fix
Ensure preserveModules: true is set and the test regex matches the file extensions you expect.
breaking Plugin only operates on generated output paths; it does not modify file contents. If you need content transformation, use a different plugin.
fix Ensure your use case is purely path rewriting; for content changes, use rollup-plugin-replace or similar.
gotcha The `replaceTo` option can be a string or function. If a string, it does NOT support regex replacement groups like $1. Use a function for regex group replacements.
fix For group replacements: `replaceTo: (path) => path.replace(/pattern/, '$1')`
gotcha The plugin relies on Rollup's output options like `preserveModules` to generate separate files. Without `preserveModules`, there may be fewer paths to rewrite.
fix If paths are not being rewritten, verify that `preserveModules: true` is set in the output config.
deprecated No known deprecations.
npm install rollup-plugin-path-rewrite
yarn add rollup-plugin-path-rewrite
pnpm add rollup-plugin-path-rewrite

Minimal rollup config using pathRewrite to move .d.ts files into a types/ subdirectory while preserving modules.

// rollup.config.js
import pathRewrite from 'rollup-plugin-path-rewrite';

export default {
  input: 'src/index.js',
  output: {
    dir: 'dist',
    format: 'es',
    preserveModules: true,
  },
  plugins: [
    pathRewrite({
      test: /\.d\.ts$/i,
      replaceTo: (path) => `types/${path}`,
    }),
  ],
};