From 46eb99b18bf148550afdd8fe1e74d35547abe0d5 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Tue, 20 Sep 2016 10:39:01 -0700 Subject: [PATCH] Use the real sourcemap API and handle input sourcemaps - fixes T7259 (#3456) --- packages/babel-cli/src/babel/file.js | 51 ++++++++++++------- .../out-files/script2.js | 2 +- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 36195ca722ee..9abdb4d12f5e 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -24,31 +24,35 @@ module.exports = function (commander, filenames, opts) { let offset = 0; _.each(results, function (result) { - let filename = result.filename || "stdout"; code += result.code + "\n"; if (result.map) { let consumer = new sourceMap.SourceMapConsumer(result.map); - - let sourceFilename = filename; - if (commander.outFile) { - sourceFilename = path.relative(path.dirname(commander.outFile), sourceFilename); - } - sourceFilename = slash(sourceFilename); - - map._sources.add(sourceFilename); - map.setSourceContent(sourceFilename, result.actual); + let sources = new Set(); consumer.eachMapping(function (mapping) { - map._mappings.add({ - generatedLine: mapping.generatedLine + offset, - generatedColumn: mapping.generatedColumn, - originalLine: mapping.source == null ? null : mapping.originalLine, - originalColumn: mapping.source == null ? null : mapping.originalColumn, - source: mapping.source == null ? null : sourceFilename + if (mapping.source != null) sources.add(mapping.source); + + map.addMapping({ + generated: { + line: mapping.generatedLine + offset, + column: mapping.generatedColumn, + }, + source: mapping.source, + original: mapping.source == null ? null : { + line: mapping.originalLine, + column: mapping.originalColumn, + }, }); }); + sources.forEach((source) => { + let content = consumer.sourceContentFor(source, true); + if (content !== null){ + map.setSourceContent(source, content); + } + }); + offset = code.split("\n").length; } }); @@ -93,7 +97,9 @@ module.exports = function (commander, filenames, opts) { }); process.stdin.on("end", function () { - results.push(util.transform(commander.filename, code)); + results.push(util.transform(commander.filename, code, { + sourceFileName: "stdin", + })); output(); }); }; @@ -120,7 +126,16 @@ module.exports = function (commander, filenames, opts) { _.each(_filenames, function (filename) { if (util.shouldIgnore(filename)) return; - let data = util.compile(filename); + let sourceFilename = filename; + if (commander.outFile) { + sourceFilename = path.relative(path.dirname(commander.outFile), sourceFilename); + } + sourceFilename = slash(sourceFilename); + + let data = util.compile(filename, { + sourceFileName: sourceFilename, + }); + if (data.ignored) return; results.push(data); }); diff --git a/packages/babel-cli/test/fixtures/babel/filename-sourcemap --out-file --source-maps inline/out-files/script2.js b/packages/babel-cli/test/fixtures/babel/filename-sourcemap --out-file --source-maps inline/out-files/script2.js index 17534661c1c8..3269c3156981 100644 --- a/packages/babel-cli/test/fixtures/babel/filename-sourcemap --out-file --source-maps inline/out-files/script2.js +++ b/packages/babel-cli/test/fixtures/babel/filename-sourcemap --out-file --source-maps inline/out-files/script2.js @@ -4,4 +4,4 @@ var foo = function foo() { return 4; }; -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjcmlwdC5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLFVBQVUsZTtTLEFBQU07QUFBQyIsImZpbGUiOiJzY3JpcHQyLmpzIiwic291cmNlc0NvbnRlbnQiOlsidmFyIGZvbyA9IGZ1bmN0aW9uICgpIHtcbiAgcmV0dXJuIDQ7XG59O1xuXG4vLyMgc291cmNlTWFwcGluZ1VSTD1kYXRhOmFwcGxpY2F0aW9uL2pzb247YmFzZTY0LGV5SjJaWEp6YVc5dUlqb3pMQ0p6YjNWeVkyVnpJanBiSW05eWFXZHBibUZzTG1weklsMHNJbTVoYldWeklqcGJYU3dpYldGd2NHbHVaM01pT2lKQlFVRkJMRlZCUVZVc1dUdFRRVUZOTEVNN1EwRkJReUlzSW5OdmRYSmpaWE5EYjI1MFpXNTBJanBiSW5aaGNpQm1iMjhnUFNBb0tTQTlQaUEwT3lKZGZRPT0iXX0= +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm9yaWdpbmFsLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsVUFBVSxlO1MsQUFBTTtBQUFDIiwiZmlsZSI6InNjcmlwdDIuanMiLCJzb3VyY2VzQ29udGVudCI6WyJ2YXIgZm9vID0gKCkgPT4gNDsiXX0=