Rollup plugin for grabbing source maps from sourceMappingURLs
npx @tessl/cli install tessl/npm-rollup-plugin-sourcemaps@0.6.0Rollup Plugin Sourcemaps is a plugin for loading files with existing source maps during the Rollup bundling process. It automatically detects and resolves sourceMappingURL comments in source code, loads external source map files, and integrates them into Rollup's compilation pipeline to maintain accurate debugging information.
npm install rollup-plugin-sourcemapsimport sourcemaps from "rollup-plugin-sourcemaps";
import type { SourcemapsPluginOptions } from "rollup-plugin-sourcemaps";
import type { Plugin } from "rollup";
import type { CreateFilter } from "@rollup/pluginutils";For CommonJS:
const sourcemaps = require("rollup-plugin-sourcemaps");import sourcemaps from "rollup-plugin-sourcemaps";
export default {
input: 'src/index.js',
plugins: [sourcemaps()],
output: {
sourcemap: true,
file: 'dist/bundle.js',
},
};With options:
import sourcemaps from "rollup-plugin-sourcemaps";
export default {
input: 'src/index.js',
plugins: [
sourcemaps({
include: '**/*.js',
exclude: 'node_modules/**',
})
],
output: {
sourcemap: true,
file: 'dist/bundle.js',
},
};Creates a Rollup plugin instance for handling source maps in the build process.
/**
* Creates a Rollup plugin for loading files with existing source maps
* @param options - Optional configuration for the plugin
* @returns Rollup plugin instance
*/
export default function sourcemaps(options?: SourcemapsPluginOptions): Plugin;The returned plugin implements the Rollup Plugin interface:
interface Plugin {
/** Plugin identifier */
name: string;
/**
* Processes files and their source maps during the load phase
* @param id - File identifier/path being loaded
* @returns Promise resolving to null (delegate to next plugin), code string, or code with source map
*/
load(id: string): Promise<null | string | { code: string; map: ExistingRawSourceMap }>;
}Usage Examples:
// Basic usage with no options
const plugin = sourcemaps();
// With file filtering
const plugin = sourcemaps({
include: ['src/**/*.js', 'lib/**/*.ts'],
exclude: ['**/*.test.js']
});
// With custom file reader
const plugin = sourcemaps({
readFile: (path, callback) => {
// Custom file reading logic
fs.readFile(path, callback);
}
});export interface SourcemapsPluginOptions {
/**
* Pattern or array of patterns to include files for source map processing
* Uses the same format as @rollup/pluginutils createFilter
*/
include?: Parameters<CreateFilter>[0];
/**
* Pattern or array of patterns to exclude files from source map processing
* Uses the same format as @rollup/pluginutils createFilter
*/
exclude?: Parameters<CreateFilter>[1];
/**
* Custom file reading function for loading source files and source maps
* Defaults to fs.readFile if not provided
*/
readFile?(
path: string,
callback: (error: Error | null, data: Buffer | string) => void
): void;
}
/** Type alias for the CreateFilter function from @rollup/pluginutils */
type CreateFilter = (include?: FilterPattern, exclude?: FilterPattern, options?: { resolve?: string | false | null }) => (id: string | unknown) => boolean;
/** File pattern matching type used by include/exclude options */
type FilterPattern = string | RegExp | Array<string | RegExp> | null;The include and exclude options accept:
"**/*.js"["src/**/*.js", "lib/**/*.ts"]/\.tsx?$/(id) => id.includes('src')The readFile option allows customization of how files are loaded:
sourcemaps({
readFile: (path, callback) => {
// Example: Add caching layer
if (cache.has(path)) {
callback(null, cache.get(path));
} else {
fs.readFile(path, (err, data) => {
if (!err) cache.set(path, data);
callback(err, data);
});
}
}
});The plugin handles various error conditions gracefully:
All warnings are issued through Rollup's warning system and can be handled by the build configuration.
@rollup/pluginutils to filter files based on include/exclude patternssource-map-resolveThe plugin internally uses:
@rollup/pluginutils: For file filtering functionalitysource-map-resolve: For resolving source maps and source contentfs module: For file system operations (unless custom readFile provided)/** Rollup source map representation */
interface ExistingRawSourceMap {
version: number;
sources: string[];
names: string[];
mappings: string;
file?: string;
sourceRoot?: string;
sourcesContent?: string[];
}