Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge multi-source output sourcemaps #14246

Merged
merged 1 commit into from Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/babel-core/src/transformation/file/generate.ts
Expand Up @@ -14,18 +14,14 @@ export default function generateCode(
outputMap: SourceMap | null;
} {
const { opts, ast, code, inputMap } = file;
const { generatorOpts } = opts;

const results = [];
for (const plugins of pluginPasses) {
for (const plugin of plugins) {
const { generatorOverride } = plugin;
if (generatorOverride) {
const result = generatorOverride(
ast,
opts.generatorOpts,
code,
generate,
);
const result = generatorOverride(ast, generatorOpts, code, generate);

if (result !== undefined) results.push(result);
}
Expand All @@ -34,7 +30,7 @@ export default function generateCode(

let result;
if (results.length === 0) {
result = generate(ast, opts.generatorOpts, code);
result = generate(ast, generatorOpts, code);
} else if (results.length === 1) {
result = results[0];

Expand All @@ -53,7 +49,11 @@ export default function generateCode(
let { code: outputCode, map: outputMap } = result;

if (outputMap && inputMap) {
outputMap = mergeSourceMap(inputMap.toObject(), outputMap);
outputMap = mergeSourceMap(
inputMap.toObject(),
outputMap,
generatorOpts.sourceFileName,
);
}

if (opts.sourceMaps === "inline" || opts.sourceMaps === "both") {
Expand Down
66 changes: 65 additions & 1 deletion packages/babel-core/src/transformation/file/merge-map.ts
Expand Up @@ -4,15 +4,79 @@ import remapping from "@ampproject/remapping";
export default function mergeSourceMap(
inputMap: SourceMap,
map: SourceMap,
source: string,
): SourceMap {
const result = remapping([rootless(map), rootless(inputMap)], () => null);
const outputSources = map.sources;

let result;
if (outputSources.length > 1) {
// When there are multiple output sources, we can't always be certain which
// source represents the file we just transformed.
const index = outputSources.indexOf(source);

// If we can't find the source, we fall back to the legacy behavior of
// outputting an empty sourcemap.
if (index === -1) {
result = emptyMap(inputMap);
} else {
result = mergeMultiSource(inputMap, map, index);
}
} else {
result = mergeSingleSource(inputMap, map);
}

if (typeof inputMap.sourceRoot === "string") {
result.sourceRoot = inputMap.sourceRoot;
}
return result;
}

// A single source transformation is the default, and easiest to handle.
function mergeSingleSource(inputMap: SourceMap, map: SourceMap): SourceMap {
return remapping([rootless(map), rootless(inputMap)], () => null);
}

// Transformation generated an output from multiple source files. When this
// happens, it's ambiguous which source was the transformed file, and which
// source is from the transformation process. We use remapping's multisource
// behavior, returning the input map when we encounter the transformed file.
function mergeMultiSource(inputMap: SourceMap, map: SourceMap, index: number) {
// We empty the source index, which will prevent the sourcemap from becoming
// relative the the input's location. Eg, if we're transforming a file
// 'foo/bar.js', and it is a transformation of a `baz.js` file in the same
// directory, the expected output is just `baz.js`. Without this step, it
// would become `foo/baz.js`.
map.sources[index] = "";

let count = 0;
return remapping(rootless(map), () => {
if (count++ === index) return rootless(inputMap);
return null;
});
}

// Legacy behavior of the old merger was to output a sourcemap without any
// mappings but with copied sourcesContent. This only happens if there are
// multiple output files and it's ambiguous which one is the transformed file.
function emptyMap(inputMap: SourceMap) {
const inputSources = inputMap.sources;

const sources = [];
const sourcesContent = inputMap.sourcesContent?.filter((content, i) => {
if (typeof content !== "string") return false;

sources.push(inputSources[i]);
return true;
});

return {
...inputMap,
sources,
sourcesContent,
mappings: "",
};
}

function rootless(map: SourceMap): SourceMap {
return {
...map,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,4 @@
{
"inputSourceMap": true,
"plugins": ["./plugin.js"]
}
@@ -0,0 +1,5 @@
"bar";

function foo(bar) {
throw new Error('Intentional.');
}
@@ -0,0 +1,32 @@
module.exports = function (babel) {
const { types: t } = babel;

return {
visitor: {
CallExpression(path) {
const { file } = this;
const { sourceFileName } = file.opts.generatorOpts;
const callee = path.node;
const { loc } = callee;

// This filename will cause a second source file to be generated in the
// output sourcemap.
loc.filename = "test.js";
loc.start.column = 1;
loc.end.column = 4;

const node = t.stringLiteral('bar');
node.loc = loc;
path.replaceWith(node);

// This injects the sourcesContent, though I don't imagine anyone's
// doing it.
file.code = {
[sourceFileName]: file.code,
'test.js': '<bar />',
};
path.stop();
},
},
};
};
@@ -0,0 +1,17 @@
{
"mappings": "AAAC;;ACCD,SAASA,GAAT,CAAaC,GAAb,EAAwB;AACpB,QAAM,IAAIC,KAAJ,CAAU,cAAV,CAAN;AACH",
"names": [
"foo",
"bar",
"Error"
],
"sources": [
"test.js",
"input.tsx"
],
"sourcesContent": [
"<bar />",
"foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}"
],
"version": 3
}