Skip to content

Commit

Permalink
Split sourcemap merging logic into its own file.
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfsmyth committed Apr 25, 2018
1 parent 4da4728 commit e31e907
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
45 changes: 1 addition & 44 deletions packages/babel-core/src/transformation/file/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import type { PluginPasses } from "../../config";
import convertSourceMap, { type SourceMap } from "convert-source-map";
import sourceMap from "source-map";
import generate from "@babel/generator";

import type File from "./file";
import mergeSourceMap from "./merge-map";

export default function generateCode(
pluginPasses: PluginPasses,
Expand Down Expand Up @@ -72,46 +72,3 @@ export default function generateCode(

return { outputCode, outputMap };
}

function mergeSourceMap(inputMap: SourceMap, map: SourceMap): SourceMap {
const inputMapConsumer = new sourceMap.SourceMapConsumer(inputMap);
const outputMapConsumer = new sourceMap.SourceMapConsumer(map);

const mergedGenerator = new sourceMap.SourceMapGenerator({
file: inputMapConsumer.file,
sourceRoot: inputMapConsumer.sourceRoot,
});

// This assumes the output map always has a single source, since Babel always compiles a
// single source file to a single output file.
const source = outputMapConsumer.sources[0];

inputMapConsumer.eachMapping(function(mapping) {
const generatedPosition = outputMapConsumer.generatedPositionFor({
line: mapping.generatedLine,
column: mapping.generatedColumn,
source: source,
});
if (generatedPosition.column != null) {
mergedGenerator.addMapping({
source: mapping.source,

original:
mapping.source == null
? null
: {
line: mapping.originalLine,
column: mapping.originalColumn,
},

generated: generatedPosition,

name: mapping.name,
});
}
});

const mergedMap = mergedGenerator.toJSON();
inputMap.mappings = mergedMap.mappings;
return inputMap;
}
50 changes: 50 additions & 0 deletions packages/babel-core/src/transformation/file/merge-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// @flow

import type { SourceMap } from "convert-source-map";
import sourceMap from "source-map";

export default function mergeSourceMap(
inputMap: SourceMap,
map: SourceMap,
): SourceMap {
const inputMapConsumer = new sourceMap.SourceMapConsumer(inputMap);
const outputMapConsumer = new sourceMap.SourceMapConsumer(map);

const mergedGenerator = new sourceMap.SourceMapGenerator({
file: inputMapConsumer.file,
sourceRoot: inputMapConsumer.sourceRoot,
});

// This assumes the output map always has a single source, since Babel always compiles a
// single source file to a single output file.
const source = outputMapConsumer.sources[0];

inputMapConsumer.eachMapping(function(mapping) {
const generatedPosition = outputMapConsumer.generatedPositionFor({
line: mapping.generatedLine,
column: mapping.generatedColumn,
source: source,
});
if (generatedPosition.column != null) {
mergedGenerator.addMapping({
source: mapping.source,

original:
mapping.source == null
? null
: {
line: mapping.originalLine,
column: mapping.originalColumn,
},

generated: generatedPosition,

name: mapping.name,
});
}
});

const mergedMap = mergedGenerator.toJSON();
inputMap.mappings = mergedMap.mappings;
return inputMap;
}

0 comments on commit e31e907

Please sign in to comment.