Skip to content

Commit 71529b1

Browse files
committed
refactor(@angular/build): extract sourcemap adjustment logic in Vitest plugin
This commit refactors the sourcemap source adjustment logic within the Vitest plugin into a standalone helper function, `adjustSourcemapSources`. This improves the readability of the `load` hook and isolates the logic for verifying and updating sourcemap paths. Additionally, the `map` parameter is now typed using `ExistingRawSourceMap` from `rolldown` instead of `any`.
1 parent aa7381e commit 71529b1

File tree

1 file changed

+36
-18
lines changed
  • packages/angular/build/src/builders/unit-test/runners/vitest

1 file changed

+36
-18
lines changed

packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { readFile } from 'node:fs/promises';
1111
import { createRequire } from 'node:module';
1212
import { platform } from 'node:os';
1313
import path from 'node:path';
14+
import type { ExistingRawSourceMap } from 'rolldown';
1415
import type {
1516
BrowserConfigOptions,
1617
InlineConfig,
@@ -286,26 +287,9 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
286287
const sourceMapFile = buildResultFiles.get(sourceMapPath);
287288
const sourceMapText = sourceMapFile ? await loadResultFile(sourceMapFile) : undefined;
288289

289-
// Vitest will include files in the coverage report if the sourcemap contains no sources.
290-
// For builder-internal generated code chunks, which are typically helper functions,
291-
// a virtual source is added to the sourcemap to prevent them from being incorrectly
292-
// included in the final coverage report.
293290
const map = sourceMapText ? JSON.parse(sourceMapText) : undefined;
294291
if (map) {
295-
if (!map.sources?.length && !map.sourcesContent?.length && !map.mappings) {
296-
map.sources = ['virtual:builder'];
297-
} else if (!vitestConfig.coverage.enabled && Array.isArray(map.sources)) {
298-
map.sources = (map.sources as string[]).map((source) => {
299-
if (source.startsWith('angular:')) {
300-
return source;
301-
}
302-
303-
// source is relative to the workspace root because the output file is at the root of the output.
304-
const absoluteSource = path.join(workspaceRoot, source);
305-
306-
return toPosixPath(path.relative(path.dirname(id), absoluteSource));
307-
});
308-
}
292+
adjustSourcemapSources(map, !vitestConfig.coverage.enabled, workspaceRoot, id);
309293
}
310294

311295
return {
@@ -338,6 +322,40 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
338322
];
339323
}
340324

325+
/**
326+
* Adjusts the sources field in a sourcemap to ensure correct source mapping and coverage reporting.
327+
*
328+
* @param map The raw sourcemap to adjust.
329+
* @param rebaseSources Whether to rebase the source paths relative to the test file.
330+
* @param workspaceRoot The root directory of the workspace.
331+
* @param id The ID (path) of the file being loaded.
332+
*/
333+
function adjustSourcemapSources(
334+
map: ExistingRawSourceMap,
335+
rebaseSources: boolean,
336+
workspaceRoot: string,
337+
id: string,
338+
): void {
339+
if (!map.sources?.length && !map.sourcesContent?.length && !map.mappings) {
340+
// Vitest will include files in the coverage report if the sourcemap contains no sources.
341+
// For builder-internal generated code chunks, which are typically helper functions,
342+
// a virtual source is added to the sourcemap to prevent them from being incorrectly
343+
// included in the final coverage report.
344+
map.sources = ['virtual:builder'];
345+
} else if (rebaseSources && map.sources) {
346+
map.sources = map.sources.map((source) => {
347+
if (!source || source.startsWith('angular:')) {
348+
return source;
349+
}
350+
351+
// source is relative to the workspace root because the output file is at the root of the output.
352+
const absoluteSource = path.join(workspaceRoot, source);
353+
354+
return toPosixPath(path.relative(path.dirname(id), absoluteSource));
355+
});
356+
}
357+
}
358+
341359
function createSourcemapSupportPlugin(): VitestPlugins[0] {
342360
return {
343361
name: 'angular:source-map-support',

0 commit comments

Comments
 (0)