From 6b91d6434d2682ef6b0bf4ff5c9540b8ddbcd7f9 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Tue, 22 May 2018 23:38:16 -0700 Subject: [PATCH] Add a try/catch on inline data-uri sourcemaps too, and add debug logging. --- .../src/transformation/normalize-file.js | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/babel-core/src/transformation/normalize-file.js b/packages/babel-core/src/transformation/normalize-file.js index ec0cf59874..fa4d03505d 100644 --- a/packages/babel-core/src/transformation/normalize-file.js +++ b/packages/babel-core/src/transformation/normalize-file.js @@ -1,5 +1,6 @@ // @flow +import buildDebug from "debug"; import * as t from "@babel/types"; import type { PluginPasses } from "../config"; import convertSourceMap, { typeof Converter } from "convert-source-map"; @@ -8,6 +9,8 @@ import { codeFrameColumns } from "@babel/code-frame"; import File from "./file/file"; import generateMissingPluginMessage from "./util/missing-plugin-helper"; +const debug = buildDebug("babel:transform:file"); + export type NormalizedFile = { code: string, ast: {}, @@ -24,27 +27,30 @@ export default function normalizeFile( let inputMap = null; if (options.inputSourceMap !== false) { - // Check if sourceMap is inline - inputMap = convertSourceMap.fromSource(code); + try { + inputMap = convertSourceMap.fromSource(code); - // Remove inline sourceMap from code - if (inputMap) { + if (inputMap) { + code = convertSourceMap.removeComments(code); + } + } catch (err) { + debug("discarding unknown inline input sourcemap", err); code = convertSourceMap.removeComments(code); } - // if source map is not inline check if it's an external file if (!inputMap) { try { inputMap = convertSourceMap.fromMapFileSource(code); - } catch (err) {} - // remove external sourceMappingUrl from code - if (inputMap) { + if (inputMap) { + code = convertSourceMap.removeMapFileComments(code); + } + } catch (err) { + debug("discarding unknown file input sourcemap", err); code = convertSourceMap.removeMapFileComments(code); } } - // If no sourceMap is found and a user provides it use that if (!inputMap && typeof options.inputSourceMap === "object") { inputMap = convertSourceMap.fromObject(options.inputSourceMap); }