Split sourcemap merging logic into its own file.
This commit is contained in:
parent
4da4728bf5
commit
e31e907d5f
@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
import type { PluginPasses } from "../../config";
|
import type { PluginPasses } from "../../config";
|
||||||
import convertSourceMap, { type SourceMap } from "convert-source-map";
|
import convertSourceMap, { type SourceMap } from "convert-source-map";
|
||||||
import sourceMap from "source-map";
|
|
||||||
import generate from "@babel/generator";
|
import generate from "@babel/generator";
|
||||||
|
|
||||||
import type File from "./file";
|
import type File from "./file";
|
||||||
|
import mergeSourceMap from "./merge-map";
|
||||||
|
|
||||||
export default function generateCode(
|
export default function generateCode(
|
||||||
pluginPasses: PluginPasses,
|
pluginPasses: PluginPasses,
|
||||||
@ -72,46 +72,3 @@ export default function generateCode(
|
|||||||
|
|
||||||
return { outputCode, outputMap };
|
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
packages/babel-core/src/transformation/file/merge-map.js
Normal file
50
packages/babel-core/src/transformation/file/merge-map.js
Normal file
@ -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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user