rollup-plugin-sourcemaps2

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

Rollup plugin that loads existing source maps from sourceMappingURL comments in files, enabling proper source map chaining when input files already contain source maps (e.g., after transpilation with Babel, TypeScript, or ESBuild). Current stable version is 0.5.6, with monthly patch releases and a major update to Rollup 4 in v0.5.0. Key differentiators: actively maintained fork of the abandoned rollup-plugin-sourcemaps, supports both async and sync loading, handles multiple sourceMappingURLs, and encodes non-ASCII characters correctly. Requires Rollup >=4 and Node >=18.

error Error: Could not resolve source map for 'xxx.js' (sourcemap not found)
cause The sourceMappingURL points to a relative path that does not exist or is inaccessible.
fix
Ensure the source map file exists at the referenced location, or use filter/include/exclude options to skip that file.
error TypeError: sourcemaps is not a function
cause Using wrong import (e.g., destructuring a named export instead of default import).
fix
Use import sourcemaps from 'rollup-plugin-sourcemaps2' (default import).
error Error: The 'sourceMap' option for Rollup must be an object, not a boolean (when using output.sourcemap: true)
cause While this plugin works with boolean sourcemap: true, some Rollup versions expect an object for advanced settings. Rare but possible.
fix
Set output.sourcemap: true or output.sourcemap: 'inline' as needed.
breaking Rollup 4 required: v0.5.0 dropped support for Rollup <4. Ensure your build uses Rollup 4.x.
fix Upgrade Rollup to v4 or later: npm install rollup@4
breaking Node 18 required: Package engine requires Node >=18.0.0.
fix Upgrade Node.js to v18 or later.
deprecated The original rollup-plugin-sourcemaps is abandoned. Migrate to this fork.
fix Replace rollup-plugin-sourcemaps with rollup-plugin-sourcemaps2 in package.json and imports.
gotcha Babel inputSourceMap conflict: Using this plugin together with @rollup/plugin-babel without setting inputSourceMap to false may cause duplicate/incorrect source maps.
fix Set babel({ inputSourceMap: false }) in your Rollup config.
gotcha Multiple sourceMappingURLs: Prior to v0.5.0, the plugin resolved only the first source mapping URL. In v0.5.0+, it resolves the last URL, which may break builds relying on old behavior.
fix If you need the old behavior, pin to v0.4.x or update your source maps to have only one mapping URL.
npm install rollup-plugin-sourcemaps2
yarn add rollup-plugin-sourcemaps2
pnpm add rollup-plugin-sourcemaps2

Shows setup with rollup-plugin-babel where inputSourceMap is disabled to avoid conflicts, and sourcemaps plugin loads existing maps.

import sourcemaps from 'rollup-plugin-sourcemaps2';
import babel from '@rollup/plugin-babel';
import { defineConfig } from 'rollup';

export default defineConfig({
  input: 'src/index.js',
  plugins: [
    babel({ babelHelpers: 'bundled', inputSourceMap: false }),
    sourcemaps({
      // include/exclude patterns (optional)
      exclude: /node_modules/,
      filter: (file) => file.endsWith('.js')
    })
  ],
  output: {
    file: 'dist/bundle.js',
    format: 'esm',
    sourcemap: true
  }
});