Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/resolve-source-root-directory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rollup-plugin-sourcemaps2": patch
---

Resolve sources inside the map's `sourceRoot` directory on filesystem paths. The trailing slash of the root was stripped, so `sourceRoot: "src"` with `sources: ["index.ts"]` resolved to `index.ts` next to the map instead of `src/index.ts`, and the map lost its `sourcesContent`.
28 changes: 27 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { type PluginContext, rollup } from 'rollup';
import { describe, expect, it, test, vi } from 'vitest';

import sourcemaps, { type SourcemapsPluginOptions } from '..';
import { resolveSourceMap } from '../source-map-resolve';
import { resolveSourceMap, resolveSources } from '../source-map-resolve';

const inputPath = path.join(__dirname, '../index.ts');
const inputText = fs.readFileSync(inputPath, 'utf8');
Expand Down Expand Up @@ -332,6 +332,32 @@ it('finds last source map file definition', async () => {
expect(map?.url).toEqual('/dev/null/index.js.map');
});

it('resolves sources inside the sourceRoot directory', async () => {
const mapUrl = path.join(__dirname, 'dist', 'index.js.map');
const requested: string[] = [];

const { sourcesResolved, sourcesContent } = await resolveSources(
{
version: 3,
file: 'index.js',
sourceRoot: 'src',
sources: ['index.ts'],
names: [],
mappings: '',
},
mapUrl,
async source => {
requested.push(source);
return 'source text';
},
);

const expected = path.join(__dirname, 'dist', 'src', 'index.ts');
expect(sourcesResolved).toStrictEqual([expected]);
expect(requested).toStrictEqual([expected]);
expect(sourcesContent).toStrictEqual(['source text']);
});

it('skips directories even when the filesystem lets them be read', async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sourcemaps2-'));
// Mimic macOS devfs, where read() on the /dev directory succeeds and returns raw bytes.
Expand Down
4 changes: 3 additions & 1 deletion src/source-map-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ function resolveUrl(...args: string[]): string {
continue;
}

resolved = path.resolve(path.dirname(resolved), nextUrl);
// A trailing slash marks a directory (sourceRoot), so the next segment resolves inside it
const base = resolved.endsWith('/') ? resolved : path.dirname(resolved);
resolved = path.resolve(base, nextUrl) + (nextUrl.endsWith('/') ? '/' : '');
}

return resolved;
Expand Down