From 3c90baaf6c87aa401323a7813aa32735ba811706 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Mon, 18 Jun 2018 21:49:23 -0700 Subject: [PATCH 1/2] Prefer object sourcemaps over file-inlined sourcemaps. --- .../src/transformation/normalize-file.js | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/babel-core/src/transformation/normalize-file.js b/packages/babel-core/src/transformation/normalize-file.js index fa4d03505d..22ea4ef470 100644 --- a/packages/babel-core/src/transformation/normalize-file.js +++ b/packages/babel-core/src/transformation/normalize-file.js @@ -27,15 +27,23 @@ export default function normalizeFile( let inputMap = null; if (options.inputSourceMap !== false) { - try { - inputMap = convertSourceMap.fromSource(code); + // If an explicit object is passed in, it overrides the processing of + // source maps that may be in the file itself. + if (typeof options.inputSourceMap === "object") { + inputMap = convertSourceMap.fromObject(options.inputSourceMap); + } - if (inputMap) { + if (!inputMap) { + try { + inputMap = convertSourceMap.fromSource(code); + + if (inputMap) { + code = convertSourceMap.removeComments(code); + } + } catch (err) { + debug("discarding unknown inline input sourcemap", err); code = convertSourceMap.removeComments(code); } - } catch (err) { - debug("discarding unknown inline input sourcemap", err); - code = convertSourceMap.removeComments(code); } if (!inputMap) { @@ -50,10 +58,6 @@ export default function normalizeFile( code = convertSourceMap.removeMapFileComments(code); } } - - if (!inputMap && typeof options.inputSourceMap === "object") { - inputMap = convertSourceMap.fromObject(options.inputSourceMap); - } } if (ast) { From 532c25c8db5b2f95271f5cd0d70e762f891cfde4 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Mon, 18 Jun 2018 22:06:08 -0700 Subject: [PATCH 2/2] Ensure that file-path sourcemaps load relative to the file containing the comment. --- lib/third-party-libs.js.flow | 2 +- .../src/transformation/normalize-file.js | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/third-party-libs.js.flow b/lib/third-party-libs.js.flow index 1b6ef25940..cd048815a4 100644 --- a/lib/third-party-libs.js.flow +++ b/lib/third-party-libs.js.flow @@ -160,7 +160,7 @@ declare module "convert-source-map" { fromComment(str: string): Converter, fromMapFileComment(str: string): Converter, fromSource(str: string): Converter, - fromMapFileSource(str: string): Converter, + fromMapFileSource(str: string, dir: string): Converter, removeComments(str: string): string, removeMapFileComments(str: string): string, generateMapFileComment(path: string, options?: ?{ multiline: boolean }): string, diff --git a/packages/babel-core/src/transformation/normalize-file.js b/packages/babel-core/src/transformation/normalize-file.js index 22ea4ef470..203488acd6 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 path from "path"; import buildDebug from "debug"; import * as t from "@babel/types"; import type { PluginPasses } from "../config"; @@ -47,14 +48,22 @@ export default function normalizeFile( } if (!inputMap) { - try { - inputMap = convertSourceMap.fromMapFileSource(code); + if (typeof options.filename === "string") { + try { + inputMap = convertSourceMap.fromMapFileSource( + code, + path.dirname(options.filename), + ); - if (inputMap) { + if (inputMap) { + code = convertSourceMap.removeMapFileComments(code); + } + } catch (err) { + debug("discarding unknown file input sourcemap", err); code = convertSourceMap.removeMapFileComments(code); } - } catch (err) { - debug("discarding unknown file input sourcemap", err); + } else { + debug("discarding un-loadable file input sourcemap"); code = convertSourceMap.removeMapFileComments(code); } }