Rollbar Sourcemap Webpack Plugin
This Webpack plugin automates the upload of generated sourcemaps to Rollbar after a production build. It addresses the challenge of making minified JavaScript stacktraces useful in Rollbar by ensuring the corresponding sourcemaps are always available. The current stable version is 3.3.0, with ongoing development and maintenance indicated by recent badges and version bumps. Its primary differentiator is deep integration with the Webpack build process, abstracting away the manual `curl` requests or shell scripts typically used for sourcemap uploads, thus reducing setup complexity and potential for error. It requires Webpack 4 or higher since version 3.0.0, streamlining the deployment of sourcemaps for error monitoring.
Common errors
-
TypeError: RollbarSourceMapPlugin is not a constructor
cause Attempting to use named import syntax `import { RollbarSourceMapPlugin }` when the plugin is exported as a default.fixChange the import statement to `import RollbarSourceMapPlugin from 'rollbar-sourcemap-webpack-plugin';` for ESM, or `const RollbarSourceMapPlugin = require('rollbar-sourcemap-webpack-plugin');` for CommonJS. -
Error: Missing required option: accessToken (or version/publicPath)
cause One of the mandatory configuration options (`accessToken`, `version`, or `publicPath`) was not provided to the plugin.fixReview the plugin configuration in your `webpack.config.js` and ensure all required options (`accessToken`, `version`, `publicPath`) are present and correctly valued. -
Rollbar API Error: 400 Bad Request (or similar upload failure message)
cause Often caused by an incorrect `accessToken`, an invalid `version` string, or a `publicPath` that doesn't correctly map to the deployed assets on your CDN.fixDouble-check your Rollbar `post_server_item` access token. Ensure your `version` string is unique and valid. Verify that the `publicPath` configuration matches the exact base URL where your production JavaScript bundles are hosted.
Warnings
- breaking Version 3.0.0 introduced a breaking change requiring Webpack 4 or higher. Projects using Webpack 3 or older must remain on `rollbar-sourcemap-webpack-plugin` v2.x.
- gotcha The `accessToken`, `version`, and `publicPath` options are all required for the plugin to function correctly. Omitting any of these will lead to upload failures.
- gotcha Using `devtool: 'hidden-source-map'` is recommended for production builds when using this plugin, as it generates sourcemaps without adding comments to the bundled files, which could expose source map URLs publicly. Other `devtool` options might embed source map references or generate inline sourcemaps which are not suitable for external upload.
Install
-
npm install rollbar-sourcemap-webpack-plugin -
yarn add rollbar-sourcemap-webpack-plugin -
pnpm add rollbar-sourcemap-webpack-plugin
Imports
- RollbarSourceMapPlugin
import { RollbarSourceMapPlugin } from 'rollbar-sourcemap-webpack-plugin';import RollbarSourceMapPlugin from 'rollbar-sourcemap-webpack-plugin';
- RollbarSourceMapPlugin
const RollbarSourceMapPlugin = require('rollbar-sourcemap-webpack-plugin');
Quickstart
const RollbarSourceMapPlugin = require('rollbar-sourcemap-webpack-plugin');
const path = require('path');
const PUBLIC_PATH = 'https://my.cdn.net/assets';
module.exports = {
mode: 'production',
devtool: 'hidden-source-map',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: PUBLIC_PATH,
filename: 'index-[contenthash].js'
},
plugins: [
new RollbarSourceMapPlugin({
accessToken: process.env.ROLLBAR_ACCESS_TOKEN ?? 'aaaabbbbccccddddeeeeffff00001111', // Use environment variable in production
version: process.env.GIT_SHA ?? 'default-version-123',
publicPath: PUBLIC_PATH,
includeChunks: ['main'] // Specify chunk name if desired
})
]
};