diff --git a/packages/babel-core/src/transformation/file/merge-map.ts b/packages/babel-core/src/transformation/file/merge-map.ts index fb12d6f2d839..2deefa638a8f 100644 --- a/packages/babel-core/src/transformation/file/merge-map.ts +++ b/packages/babel-core/src/transformation/file/merge-map.ts @@ -38,7 +38,13 @@ export default function mergeSourceMap( if (typeof inputMap.sourceRoot === "string") { result.sourceRoot = inputMap.sourceRoot; } - return result; + + // remapping returns a SourceMap class type, but this breaks code downstream in + // @babel/traverse and @babel/types that relies on data being plain objects. + // When it encounters the sourcemap type it outputs a "don't know how to turn + // this value into a node" error. As a result, we are converting the merged + // sourcemap to a plain js object. + return { ...result }; } function rootless(map: SourceMap): SourceMap { diff --git a/packages/babel-core/test/merge-map.js b/packages/babel-core/test/merge-map.js new file mode 100644 index 000000000000..bc13bb35a99a --- /dev/null +++ b/packages/babel-core/test/merge-map.js @@ -0,0 +1,27 @@ +import _mergeSourceMap from "../lib/transformation/file/merge-map.js"; +const mergeSourceMap = _mergeSourceMap.default; + +describe("merge-map", () => { + it("returns a plain js object", () => { + const inputMap = { + file: "file.js", + mappings: [], + names: [], + sources: ["file.ts"], + version: 3, + }; + + const outputMap = { + file: "file.transpiled.js", + mappings: [], + names: [], + sources: ["file.js"], + version: 3, + }; + + const map = mergeSourceMap(inputMap, outputMap, "file.transpiled.js"); + expect(typeof map).toBe("object"); + expect(Object.prototype.toString.call(map)).toBe("[object Object]"); + expect(Object.getPrototypeOf(map)).toBe(Object.prototype); + }); +});