Skip to content

Commit b45f336

Browse files
petebacondarwinmatsko
authored andcommitted
fix(ngcc): do not inline source-maps for non-inline typings source-maps (angular#37363)
Inline source-maps in typings files can impact IDE performance so ngcc should only add such maps if the original typings file contains inline source-maps. Fixes angular#37324 PR Close angular#37363
1 parent c3651ce commit b45f336

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

packages/compiler-cli/ngcc/src/rendering/source_maps.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,16 @@ export function renderSourceAndMap(
4242

4343
const rawMergedMap: RawSourceMap = generatedFile.renderFlattenedSourceMap();
4444
const mergedMap = fromObject(rawMergedMap);
45-
if (generatedFile.sources[0]?.inline) {
46-
// The input source-map was inline so make the output one inline too.
45+
const firstSource = generatedFile.sources[0];
46+
if (firstSource && (firstSource.rawMap !== null || !sourceFile.isDeclarationFile) &&
47+
firstSource.inline) {
48+
// We render an inline source map if one of:
49+
// * there was no input source map and this is not a typings file;
50+
// * the input source map exists and was inline.
51+
//
52+
// We do not generate inline source maps for typings files unless there explicitly was one in
53+
// the input file because these inline source maps can be very large and it impacts on the
54+
// performance of IDEs that need to read them to provide intellisense etc.
4755
return [
4856
{path: generatedPath, contents: `${generatedFile.contents}\n${mergedMap.toComment()}`}
4957
];

packages/compiler-cli/ngcc/test/rendering/dts_renderer_spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import {fromObject} from 'convert-source-map';
89
import MagicString from 'magic-string';
10+
import {encode} from 'sourcemap-codec';
911
import * as ts from 'typescript';
1012

1113
import {absoluteFrom, getFileSystem} from '../../../src/ngtsc/file_system';
@@ -195,5 +197,47 @@ runInEachFileSystem(() => {
195197
result.find(f => f.path === _('/node_modules/test-package/typings/file.d.ts'))!;
196198
expect(typingsFile.contents).toContain(`\n// ADD MODUlE WITH PROVIDERS PARAMS\n`);
197199
});
200+
201+
it('should render an external source map for files whose original file does not have a source map',
202+
() => {
203+
const {
204+
renderer,
205+
decorationAnalyses,
206+
privateDeclarationsAnalyses,
207+
moduleWithProvidersAnalyses
208+
} = createTestRenderer('test-package', [INPUT_PROGRAM], [INPUT_DTS_PROGRAM]);
209+
210+
const result = renderer.renderProgram(
211+
decorationAnalyses, privateDeclarationsAnalyses, moduleWithProvidersAnalyses);
212+
213+
const typingsFile =
214+
result.find(f => f.path === _('/node_modules/test-package/typings/file.d.ts'))!;
215+
expect(typingsFile.contents).toContain('//# sourceMappingURL=file.d.ts.map');
216+
});
217+
218+
it('should render an internal source map for files whose original file has an internal source map',
219+
() => {
220+
const sourceMap = fromObject({
221+
'version': 3,
222+
'file': 'file.d.ts',
223+
'sources': ['file.d.ts'],
224+
'names': [],
225+
'mappings': encode([[]]),
226+
'sourcesContent': [INPUT_DTS_PROGRAM.contents],
227+
});
228+
INPUT_DTS_PROGRAM.contents += sourceMap.toComment();
229+
const {
230+
renderer,
231+
decorationAnalyses,
232+
privateDeclarationsAnalyses,
233+
moduleWithProvidersAnalyses
234+
} = createTestRenderer('test-package', [INPUT_PROGRAM], [INPUT_DTS_PROGRAM]);
235+
const result = renderer.renderProgram(
236+
decorationAnalyses, privateDeclarationsAnalyses, moduleWithProvidersAnalyses);
237+
238+
const typingsFile =
239+
result.find(f => f.path === _('/node_modules/test-package/typings/file.d.ts'))!;
240+
expect(typingsFile.contents).toContain('//# sourceMappingURL=data:application/json');
241+
});
198242
});
199243
});

packages/compiler-cli/ngcc/test/rendering/renderer_spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,24 @@ UndecoratedBase.ɵdir = ɵngcc0.ɵɵdefineDirective({ type: UndecoratedBase, vie
640640
expect(mapFile.path).toEqual(_('/node_modules/test-package/src/file.js.map'));
641641
expect(JSON.parse(mapFile.contents)).toEqual(MERGED_OUTPUT_PROGRAM_MAP.toObject());
642642
});
643+
644+
645+
it('should render an internal source map for files whose original file does not have a source map',
646+
() => {
647+
const sourceFiles: TestFile[] = [JS_CONTENT];
648+
const {
649+
decorationAnalyses,
650+
renderer,
651+
switchMarkerAnalyses,
652+
privateDeclarationsAnalyses
653+
} = createTestRenderer('test-package', sourceFiles, undefined);
654+
const [sourceFile, mapFile] = renderer.renderProgram(
655+
decorationAnalyses, switchMarkerAnalyses, privateDeclarationsAnalyses);
656+
expect(sourceFile.path).toEqual(_('/node_modules/test-package/src/file.js'));
657+
expect(sourceFile.contents)
658+
.toEqual(RENDERED_CONTENTS + '\n' + OUTPUT_PROGRAM_MAP.toComment());
659+
expect(mapFile).toBeUndefined();
660+
});
643661
});
644662

645663
describe('@angular/core support', () => {

0 commit comments

Comments
 (0)