From 7d3b70c2afbdc786b21049d7a31a714460fb7e59 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Tue, 17 Jul 2018 10:43:42 +0100 Subject: [PATCH] fix(ivy): ngcc is resilient to bad source-map comments (#25406) PR Close #25406 --- .../src/ngcc/src/rendering/renderer.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/compiler-cli/src/ngcc/src/rendering/renderer.ts b/packages/compiler-cli/src/ngcc/src/rendering/renderer.ts index 4a53d4b20c..a5f421352c 100644 --- a/packages/compiler-cli/src/ngcc/src/rendering/renderer.ts +++ b/packages/compiler-cli/src/ngcc/src/rendering/renderer.ts @@ -7,8 +7,9 @@ */ import {ConstantPool, Expression, Statement, WrappedNodeExpr, WritePropExpr} from '@angular/compiler'; import {SourceMapConverter, commentRegex, fromJSON, fromMapFileSource, fromObject, fromSource, generateMapFileComment, mapFileCommentRegex, removeComments, removeMapFileComments} from 'convert-source-map'; +import {readFileSync, statSync} from 'fs'; import MagicString from 'magic-string'; -import {dirname} from 'path'; +import {basename, dirname} from 'path'; import {SourceMapConsumer, SourceMapGenerator, RawSourceMap} from 'source-map'; import * as ts from 'typescript'; @@ -137,7 +138,20 @@ export abstract class Renderer { try { externalSourceMap = fromMapFileSource(file.text, dirname(file.fileName)); } catch (e) { - console.warn(e); + if (e.code === 'ENOENT') { + console.warn( + `The external map file specified in the source code comment "${e.path}" was not found on the file system.`); + const mapPath = file.fileName + '.map'; + if (basename(e.path) !== basename(mapPath) && statSync(mapPath).isFile()) { + console.warn( + `Guessing the map file name from the source file name: "${basename(mapPath)}"`); + try { + externalSourceMap = fromObject(JSON.parse(readFileSync(mapPath, 'utf8'))); + } catch (e) { + console.error(e); + } + } + } } return { source: removeMapFileComments(file.text).replace(/\n\n$/, '\n'),