From 532c25c8db5b2f95271f5cd0d70e762f891cfde4 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Mon, 18 Jun 2018 22:06:08 -0700 Subject: [PATCH] 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); } }