From dd776da1e525b15b98eb716e78d2b348a871cac5 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Tue, 21 Feb 2023 06:34:42 +0800 Subject: [PATCH] fix: Handling source maps without `sourcesContent` (#15445) * fix * improve --- packages/babel-generator/src/source-map.ts | 2 +- packages/babel-generator/test/index.js | 34 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/babel-generator/src/source-map.ts b/packages/babel-generator/src/source-map.ts index 634b41fb60e0..fa27a87cfa58 100644 --- a/packages/babel-generator/src/source-map.ts +++ b/packages/babel-generator/src/source-map.ts @@ -58,7 +58,7 @@ export default class SourceMap { setSourceContent( map, resolvedSources[i], - this._inputMap.sourcesContent[i], + this._inputMap.sourcesContent?.[i], ); } } diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index ab84ce99ed86..b5816f24b710 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -880,6 +880,40 @@ describe("generation", function () { });" `); }); + + it("inputSourceMap without sourcesContent", () => { + const ast = parse("var t = x => x * x;"); + + expect( + generate(ast, { + sourceMaps: true, + inputSourceMap: { + version: 3, + names: ["t", "x"], + sources: ["source-maps/arrow-function/input.js"], + mappings: + "AAAA,IAAIA,CAAC,GAAG,SAAJA,CAACA,CAAGC,CAAC;EAAA,OAAIA,CAAC,GAAGA,CAAC;AAAA", + }, + }).map, + ).toMatchInlineSnapshot(` + Object { + "file": undefined, + "mappings": "AAAA,IAAIA,CAAC,GAAGC,CAAA,IAAAA,CAAA,GAAJA,CAAC", + "names": Array [ + "t", + "x", + ], + "sourceRoot": undefined, + "sources": Array [ + "source-maps/arrow-function/input.js", + ], + "sourcesContent": Array [ + undefined, + ], + "version": 3, + } + `); + }); }); describe("programmatic generation", function () {