| 
									
										
										
										
											2019-04-28 20:48:35 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2019-04-28 20:48:35 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-04-06 08:30:08 +01:00
										 |  |  | import {fromObject, generateMapFileComment, SourceMapConverter} from 'convert-source-map'; | 
					
						
							| 
									
										
										
										
											2019-04-28 20:48:35 +01:00
										 |  |  | import MagicString from 'magic-string'; | 
					
						
							|  |  |  | import * as ts from 'typescript'; | 
					
						
							| 
									
										
										
										
											2020-04-06 08:30:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-30 17:03:41 +00:00
										 |  |  | import {absoluteFrom, absoluteFromSourceFile, ReadonlyFileSystem} from '../../../src/ngtsc/file_system'; | 
					
						
							| 
									
										
										
										
											2020-05-14 20:06:12 +01:00
										 |  |  | import {Logger} from '../../../src/ngtsc/logging'; | 
					
						
							| 
									
										
										
										
											2020-05-14 21:12:35 +01:00
										 |  |  | import {RawSourceMap, SourceFileLoader} from '../../../src/ngtsc/sourcemaps'; | 
					
						
							| 
									
										
										
										
											2020-04-06 08:30:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {FileToWrite} from './utils'; | 
					
						
							| 
									
										
										
										
											2019-04-28 20:48:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export interface SourceMapInfo { | 
					
						
							|  |  |  |   source: string; | 
					
						
							|  |  |  |   map: SourceMapConverter|null; | 
					
						
							|  |  |  |   isInline: boolean; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Merge the input and output source-maps, replacing the source-map comment in the output file | 
					
						
							|  |  |  |  * with an appropriate source-map comment pointing to the merged source-map. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export function renderSourceAndMap( | 
					
						
							| 
									
										
										
										
											2020-12-30 17:03:41 +00:00
										 |  |  |     logger: Logger, fs: ReadonlyFileSystem, sourceFile: ts.SourceFile, | 
					
						
							| 
									
										
										
										
											2020-02-27 19:50:14 +00:00
										 |  |  |     generatedMagicString: MagicString): FileToWrite[] { | 
					
						
							| 
									
										
										
										
											2020-02-16 21:07:30 +01:00
										 |  |  |   const generatedPath = absoluteFromSourceFile(sourceFile); | 
					
						
							|  |  |  |   const generatedMapPath = absoluteFrom(`${generatedPath}.map`); | 
					
						
							|  |  |  |   const generatedContent = generatedMagicString.toString(); | 
					
						
							|  |  |  |   const generatedMap: RawSourceMap = generatedMagicString.generateMap( | 
					
						
							|  |  |  |       {file: generatedPath, source: generatedPath, includeContent: true}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 19:50:14 +00:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2020-06-14 23:19:36 +01:00
										 |  |  |     const loader = new SourceFileLoader(fs, logger, {}); | 
					
						
							| 
									
										
										
										
											2020-02-27 19:50:14 +00:00
										 |  |  |     const generatedFile = loader.loadSourceFile( | 
					
						
							|  |  |  |         generatedPath, generatedContent, {map: generatedMap, mapPath: generatedMapPath}); | 
					
						
							| 
									
										
										
										
											2020-02-16 21:07:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-27 19:50:14 +00:00
										 |  |  |     const rawMergedMap: RawSourceMap = generatedFile.renderFlattenedSourceMap(); | 
					
						
							|  |  |  |     const mergedMap = fromObject(rawMergedMap); | 
					
						
							| 
									
										
										
										
											2020-05-30 20:49:44 +01:00
										 |  |  |     const firstSource = generatedFile.sources[0]; | 
					
						
							|  |  |  |     if (firstSource && (firstSource.rawMap !== null || !sourceFile.isDeclarationFile) && | 
					
						
							|  |  |  |         firstSource.inline) { | 
					
						
							|  |  |  |       // We render an inline source map if one of:
 | 
					
						
							|  |  |  |       // * there was no input source map and this is not a typings file;
 | 
					
						
							|  |  |  |       // * the input source map exists and was inline.
 | 
					
						
							|  |  |  |       //
 | 
					
						
							|  |  |  |       // We do not generate inline source maps for typings files unless there explicitly was one in
 | 
					
						
							|  |  |  |       // the input file because these inline source maps can be very large and it impacts on the
 | 
					
						
							|  |  |  |       // performance of IDEs that need to read them to provide intellisense etc.
 | 
					
						
							| 
									
										
										
										
											2020-02-27 19:50:14 +00:00
										 |  |  |       return [ | 
					
						
							|  |  |  |         {path: generatedPath, contents: `${generatedFile.contents}\n${mergedMap.toComment()}`} | 
					
						
							|  |  |  |       ]; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-12-30 17:03:41 +00:00
										 |  |  |       const sourceMapComment = generateMapFileComment(`${fs.basename(generatedPath)}.map`); | 
					
						
							| 
									
										
										
										
											2020-02-27 19:50:14 +00:00
										 |  |  |       return [ | 
					
						
							|  |  |  |         {path: generatedPath, contents: `${generatedFile.contents}\n${sourceMapComment}`}, | 
					
						
							|  |  |  |         {path: generatedMapPath, contents: mergedMap.toJSON()} | 
					
						
							|  |  |  |       ]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } catch (e) { | 
					
						
							| 
									
										
										
										
											2020-04-06 08:30:08 +01:00
										 |  |  |     logger.error(`Error when flattening the source-map "${generatedMapPath}" for "${ | 
					
						
							|  |  |  |         generatedPath}": ${e.toString()}`);
 | 
					
						
							| 
									
										
										
										
											2020-02-16 21:07:30 +01:00
										 |  |  |     return [ | 
					
						
							| 
									
										
										
										
											2020-02-27 19:50:14 +00:00
										 |  |  |       {path: generatedPath, contents: generatedContent}, | 
					
						
							|  |  |  |       {path: generatedMapPath, contents: fromObject(generatedMap).toJSON()}, | 
					
						
							| 
									
										
										
										
											2020-02-16 21:07:30 +01:00
										 |  |  |     ]; | 
					
						
							| 
									
										
										
										
											2019-04-28 20:48:35 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | } |