From 1a87a5e9d7737a4538250991845852fcd25a8481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hua=CC=81ng=20Ju=CC=80nlia=CC=80ng?= Date: Sun, 21 Jul 2019 21:35:38 -0400 Subject: [PATCH] fix: normalize sourcemap sources when generating from file name --- packages/babel-generator/src/source-map.js | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/babel-generator/src/source-map.js b/packages/babel-generator/src/source-map.js index d03628a5eb53..106a6bd9df55 100644 --- a/packages/babel-generator/src/source-map.js +++ b/packages/babel-generator/src/source-map.js @@ -7,8 +7,9 @@ import sourceMap from "source-map"; export default class SourceMap { constructor(opts, code) { this._cachedMap = null; - this._code = code; - this._opts = opts; + const { _opts, _code } = normalizeSourceFileName(opts, code); + this._code = _code; + this._opts = _opts; this._rawMappings = []; } @@ -73,6 +74,9 @@ export default class SourceMap { this._lastGenLine = generatedLine; this._lastSourceLine = line; this._lastSourceColumn = column; + if (filename) { + filename = filename.replace(/\\/g, "/"); + } // We are deliberately not using the `source-map` library here to allow // callers to use these mappings without any overhead @@ -94,3 +98,19 @@ export default class SourceMap { }); } } + +function normalizeSourceFileName(opts, code) { + let _opts = opts; + const _code = code; + if (typeof code === "string") { + _opts = { + ...opts, + sourceFileName: _opts.sourceFileName.replace(/\\/g, "/"), + }; + } else if (typeof code === "object") { + Object.keys(code).forEach(sourceFileName => { + _code[sourceFileName.replace(/\\/g, "/")] = code[sourceFileName]; + }); + } + return { _opts, _code }; +}