From ea1c44ea7f5bee7681aa4d6074e3aa01bd125749 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Thu, 17 Feb 2022 10:42:07 -0500 Subject: [PATCH] Fix merging sourcemaps on Windows (#14282) --- .../babel-core/src/transformation/file/merge-map.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/babel-core/src/transformation/file/merge-map.ts b/packages/babel-core/src/transformation/file/merge-map.ts index 4b902c27bf82..fb12d6f2d839 100644 --- a/packages/babel-core/src/transformation/file/merge-map.ts +++ b/packages/babel-core/src/transformation/file/merge-map.ts @@ -4,8 +4,16 @@ import remapping from "@ampproject/remapping"; export default function mergeSourceMap( inputMap: SourceMap, map: SourceMap, - source: string, + sourceFileName: string, ): SourceMap { + // On win32 machines, the sourceFileName uses backslash paths like + // `C:\foo\bar.js`. But sourcemaps are always posix paths, so we need to + // normalize to regular slashes before we can merge (else we won't find the + // source associated with our input map). + // This mirrors code done while generating the output map at + // https://github.com/babel/babel/blob/5c2fcadc9ae34fd20dd72b1111d5cf50476d700d/packages/babel-generator/src/source-map.ts#L102 + const source = sourceFileName.replace(/\\/g, "/"); + // Prevent an infinite recursion if one of the input map's sources has the // same resolved path as the input map. In the case, it would keep find the // input map, then get it's sources which will include a path like the input