vite-plugin-dep-sourcemaps
v0.1.0
Published
Chain a bundle's sourcemaps through its dependencies' sourcemaps so stack traces resolve to the original source instead of the bundled dist — works under Rolldown / Vite 8 where external dependency sourcemaps aren't chained yet.
Maintainers
Readme
vite-plugin-dep-sourcemaps
Chain a bundle's sourcemaps through its dependencies' own sourcemaps, so
stack traces (e.g. in Sentry) resolve to a dependency's original source
instead of its bundled dist.
Useful under Rolldown / Vite 8, which doesn't yet chain pre-existing
external dependency sourcemaps on its own
(see vitejs/vite#11743 and
vitejs/vite#21965). The classic
rollup-plugin-sourcemaps works via the load hook, which Rolldown ignores —
this plugin instead post-processes the finished output maps, so it works
regardless.
The problem
When you bundle a dependency that ships its own dist/*.js + dist/*.js.map
(built from .ts/.tsx), the ideal build sourcemap should point at that
dependency's original source. Under Rollup this "just works"; under Rolldown
it currently doesn't — the map points at the bundled dist/index.js, so an
error in a dependency shows up at the wrong place.
Install
npm i -D vite-plugin-dep-sourcemapsUsage
// vite.config.ts
import { defineConfig } from 'vite';
import depSourcemaps from 'vite-plugin-dep-sourcemaps';
export default defineConfig({
build: {
sourcemap: true, // required
},
plugins: [depSourcemaps()],
});By default it chains any source resolving under node_modules. Order it before
plugins that consume the final maps (e.g. @sentry/vite-plugin) so they see the
remapped output.
Options
depSourcemaps({
// Only chain deps whose resolved path matches (string = substring, RegExp = test).
// Default: 'node_modules'
include: [/@myscope\//, 'some-lib'],
// Skip deps whose resolved path matches.
exclude: [/node_modules\/big-untyped-lib\//],
});| Option | Type | Default | Description |
| --------- | ----------------------------- | ---------------- | ------------------------------------------------------ |
| include | string \| RegExp \| Array | 'node_modules' | Only chain dependency sources matching these. |
| exclude | string \| RegExp \| Array | [] | Skip dependency sources matching these. |
How it works
Runs in the writeBundle hook (after the bundle is written). For every emitted
*.js.map, it re-composes the map through the deps' own sidecar *.js.map
files using @ampproject/remapping,
following each //# sourceMappingURL one level down to the original source. It
breaks cycles, resolves relative paths against the map's location, and leaves a
map untouched if anything goes wrong.
Requirements
build.sourcemapenabled.- Dependencies must ship sidecar
<file>.js.mapfiles (ideally withsourcesContent). Inline sourcemaps aren't chained.
License
MIT © Javad Tavakoli
