diff --git a/packages/compiler-cli/ngcc/src/sourcemaps/source_file.ts b/packages/compiler-cli/ngcc/src/sourcemaps/source_file.ts index 49310b3b50..09f1feae7c 100644 --- a/packages/compiler-cli/ngcc/src/sourcemaps/source_file.ts +++ b/packages/compiler-cli/ngcc/src/sourcemaps/source_file.ts @@ -290,11 +290,16 @@ export function parseMappings( const generatedLineMappings = rawMappings[generatedLine]; for (const rawMapping of generatedLineMappings) { if (rawMapping.length >= 4) { + const originalSource = sources[rawMapping[1] !]; + if (originalSource === null || originalSource === undefined) { + // the original source is missing so ignore this mapping + continue; + } const generatedColumn = rawMapping[0]; const name = rawMapping.length === 5 ? rawMap.names[rawMapping[4]] : undefined; const mapping: Mapping = { generatedSegment: {line: generatedLine, column: generatedColumn}, - originalSource: sources[rawMapping[1] !] !, + originalSource, originalSegment: {line: rawMapping[2] !, column: rawMapping[3] !}, name }; mappings.push(mapping); diff --git a/packages/compiler-cli/ngcc/test/sourcemaps/source_file_spec.ts b/packages/compiler-cli/ngcc/test/sourcemaps/source_file_spec.ts index 7284e26fa3..3a3df16d7b 100644 --- a/packages/compiler-cli/ngcc/test/sourcemaps/source_file_spec.ts +++ b/packages/compiler-cli/ngcc/test/sourcemaps/source_file_spec.ts @@ -174,6 +174,28 @@ runInEachFileSystem(() => { }, ]); }); + + it('should ignore mappings to missing source files', () => { + const bSourceMap: RawSourceMap = { + mappings: encode([[[1, 0, 0, 0], [4, 0, 0, 3], [4, 0, 0, 6], [5, 0, 0, 7]]]), + names: [], + sources: ['c.js'], + version: 3 + }; + const bSource = new SourceFile(_('/foo/src/b.js'), 'abcdef', bSourceMap, false, [null]); + const aSourceMap: RawSourceMap = { + mappings: encode([[[0, 0, 0, 0], [2, 0, 0, 3], [4, 0, 0, 2], [5, 0, 0, 5]]]), + names: [], + sources: ['b.js'], + version: 3 + }; + const aSource = + new SourceFile(_('/foo/src/a.js'), 'abdecf', aSourceMap, false, [bSource]); + + // These flattened mappings are just the mappings from a to b. + // (The mappings to c are dropped since there is no source file to map to.) + expect(aSource.flattenedMappings).toEqual(parseMappings(aSourceMap, [bSource])); + }); }); describe('renderFlattenedSourceMap()', () => {