fix(ivy): ngcc is resilient to bad source-map comments (#25406)

PR Close #25406
This commit is contained in:
Pete Bacon Darwin 2018-07-17 10:43:42 +01:00 committed by Matias Niemelä
parent 7ce291c72a
commit 7d3b70c2af
1 changed files with 16 additions and 2 deletions

View File

@ -7,8 +7,9 @@
*/ */
import {ConstantPool, Expression, Statement, WrappedNodeExpr, WritePropExpr} from '@angular/compiler'; 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 {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 MagicString from 'magic-string';
import {dirname} from 'path'; import {basename, dirname} from 'path';
import {SourceMapConsumer, SourceMapGenerator, RawSourceMap} from 'source-map'; import {SourceMapConsumer, SourceMapGenerator, RawSourceMap} from 'source-map';
import * as ts from 'typescript'; import * as ts from 'typescript';
@ -137,7 +138,20 @@ export abstract class Renderer {
try { try {
externalSourceMap = fromMapFileSource(file.text, dirname(file.fileName)); externalSourceMap = fromMapFileSource(file.text, dirname(file.fileName));
} catch (e) { } 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 { return {
source: removeMapFileComments(file.text).replace(/\n\n$/, '\n'), source: removeMapFileComments(file.text).replace(/\n\n$/, '\n'),