Skip to content

Commit b51de76

Browse files
committed
fix(@angular/build): resolve library bundle sourcemap sources relative to output directory
Use `sourcemapPathTransform` when generating FESM and declaration bundles in the library builder so that emitted sourcemap `sources` entries are computed relative to the actual output directory rather than the relative bundle directory.
1 parent be574a4 commit b51de76

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎packages/angular/build/src/builders/library/pipeline/bundler.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ function processRolldownOutput(output: RolldownOutput['output'], dir: string): M
295295
* @param preserveSymlinks Whether to preserve symlinks during module resolution.
296296
* @param extension Output file extension ('mjs' or 'd.ts').
297297
* @param sourcemap Whether to emit sourcemaps.
298-
* @param findEntryPoint Lookup function to resolve chunk entry point ownership.
298+
* @param outputPath The absolute base output directory path.
299299
* @returns A promise resolving to the multi-bundle output.
300300
*/
301301
async function executeMultiBundle(
@@ -304,9 +304,11 @@ async function executeMultiBundle(
304304
preserveSymlinks: boolean,
305305
extension: 'mjs' | 'd.ts',
306306
sourcemap: boolean,
307+
outputPath: string,
307308
): Promise<MultiBundleOutput> {
308309
const isDts = extension === 'd.ts';
309310
const dir = isDts ? TYPES_OUTPUT_DIR : FESM_OUTPUT_DIR;
311+
const outputDir = path.join(outputPath, dir);
310312
const bundle = await rolldown({
311313
context: 'this',
312314
input,
@@ -326,6 +328,12 @@ async function executeMultiBundle(
326328
entryFileNames: `[name].${extension}`,
327329
chunkFileNames: `[name]-[hash].${extension}`,
328330
sourcemap,
331+
sourcemapPathTransform: (sourcePath, sourcemapPath) => {
332+
const absoluteSourcePath = path.resolve(path.dirname(sourcemapPath), sourcePath);
333+
const relativeSourcePath = path.relative(outputDir, absoluteSourcePath);
334+
335+
return toPosixPath(relativeSourcePath);
336+
},
329337
hoistTransitiveImports: false,
330338
comments: { jsdoc: isDts, legal: true, annotation: true },
331339
});
@@ -359,6 +367,7 @@ async function bundleAllEsm(
359367
options.preserveSymlinks,
360368
'mjs',
361369
true,
370+
options.outputPath,
362371
);
363372
}
364373

@@ -389,10 +398,13 @@ async function bundleAllDts(
389398
dtsInput: true,
390399
tsconfig: false,
391400
sourcemap: dtsSourcemap,
401+
generator: 'oxc',
392402
});
403+
393404
const dtsPlugins = rawDtsPlugins.filter(
394405
(plugin) => plugin.name !== 'rolldown-plugin-dts:resolver',
395406
);
407+
396408
assert(
397409
dtsPlugins.length < rawDtsPlugins.length,
398410
'Expected "rolldown-plugin-dts:resolver" plugin to be present in rolldown-plugin-dts.',
@@ -404,5 +416,6 @@ async function bundleAllDts(
404416
options.preserveSymlinks,
405417
'd.ts',
406418
dtsSourcemap,
419+
options.outputPath,
407420
);
408421
}

‎packages/angular/build/src/builders/library/tests/behavior/build_spec.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
2626
expect(fesmContent).toContain('LibComponent');
2727
expect(fesmContent).toContain('ɵcmp');
2828

29+
harness.expectFile('dist/lib/fesm2022/lib.mjs.map').toExist();
30+
const fesmMap = JSON.parse(harness.readFile('dist/lib/fesm2022/lib.mjs.map'));
31+
expect(fesmMap.sources).toContain('../../../projects/lib/src/lib/lib.ts');
32+
2933
harness.expectFile('dist/lib/types/lib.d.ts').toExist();
3034
const dtsContent = harness.readFile('dist/lib/types/lib.d.ts');
3135
expect(dtsContent).toContain('LibComponent');

‎packages/angular/build/src/builders/library/tests/options/declaration-map_spec.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) =>
3838
expect(harness.hasFile('dist/lib/fesm2022/lib.mjs.map')).toBeTrue();
3939
// DTS sourcemaps should be generated
4040
expect(harness.hasFile('dist/lib/types/lib.d.ts.map')).toBeTrue();
41+
const dtsMap = JSON.parse(harness.readFile('dist/lib/types/lib.d.ts.map'));
42+
expect(dtsMap.sources).toContain('../../../projects/lib/src/lib/lib.ts');
4143
});
4244
});
4345
});

0 commit comments

Comments
 (0)