Skip to content

Commit f51fc61

Browse files
authored
[cli] Avoid SourceMapGenerator for simple map concatenation (#14479)
1 parent f24a72d commit f51fc61

12 files changed

Lines changed: 58 additions & 61 deletions

File tree

packages/babel-cli/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@
2424
"compiler"
2525
],
2626
"dependencies": {
27-
"@jridgewell/trace-mapping": "^0.3.4",
27+
"@jridgewell/trace-mapping": "^0.3.8",
2828
"commander": "^4.0.1",
2929
"convert-source-map": "^1.1.0",
3030
"fs-readdir-recursive": "^1.1.0",
3131
"glob": "^7.0.0",
3232
"make-dir": "condition:BABEL_8_BREAKING ? : ^2.1.0",
33-
"slash": "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0",
34-
"source-map": "^0.5.0"
33+
"slash": "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0"
3534
},
3635
"optionalDependencies": {
3736
"@nicolo-ribaudo/chokidar-2": "condition:BABEL_8_BREAKING ? : 2.1.8-no-fsevents.3",

packages/babel-cli/src/babel/file.ts

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import convertSourceMap from "convert-source-map";
2-
import { TraceMap, eachMapping } from "@jridgewell/trace-mapping";
3-
import sourceMap from "source-map";
2+
import { AnyMap, encodedMap } from "@jridgewell/trace-mapping";
43
import slash from "slash";
54
import path from "path";
65
import fs from "fs";
@@ -19,66 +18,66 @@ export default async function ({
1918
babelOptions,
2019
}: CmdOptions): Promise<void> {
2120
function buildResult(fileResults: Array<any>): CompilationOutput {
22-
const map = new sourceMap.SourceMapGenerator({
23-
file:
24-
cliOptions.sourceMapTarget ||
25-
path.basename(cliOptions.outFile || "") ||
26-
"stdout",
27-
sourceRoot: babelOptions.sourceRoot,
28-
});
21+
const mapSections = [];
2922

3023
let code = "";
3124
let offset = 0;
3225

3326
for (const result of fileResults) {
3427
if (!result) continue;
3528

36-
code += result.code + "\n";
37-
38-
if (result.map) {
39-
const consumer = new TraceMap(result.map);
40-
41-
eachMapping(consumer, mapping => {
42-
map.addMapping({
43-
generated: {
44-
line: mapping.generatedLine + offset,
45-
column: mapping.generatedColumn,
46-
},
47-
source: mapping.source,
48-
original:
49-
mapping.source == null
50-
? null
51-
: {
52-
line: mapping.originalLine,
53-
column: mapping.originalColumn,
54-
},
55-
});
56-
});
57-
58-
const { resolvedSources, sourcesContent } = consumer;
59-
sourcesContent?.forEach((content, i) => {
60-
if (content === null) return;
61-
map.setSourceContent(resolvedSources[i], content);
62-
});
29+
mapSections.push({
30+
offset: { line: offset, column: 0 },
31+
map: result.map || emptyMap(),
32+
});
6333

64-
offset = code.split("\n").length - 1;
65-
}
34+
code += result.code + "\n";
35+
offset += countNewlines(result.code) + 1;
6636
}
6737

38+
const map = new AnyMap({
39+
version: 3,
40+
file:
41+
cliOptions.sourceMapTarget ||
42+
path.basename(cliOptions.outFile || "") ||
43+
"stdout",
44+
sections: mapSections,
45+
});
46+
// For some reason, the spec doesn't allow sourceRoot when constructing a
47+
// sectioned sorucemap. But AllMap returns a regular sourcemap, we can
48+
// freely add to with a sourceRoot.
49+
map.sourceRoot = babelOptions.sourceRoot;
50+
6851
// add the inline sourcemap comment if we've either explicitly asked for inline source
6952
// maps, or we've requested them without any output file
7053
if (
7154
babelOptions.sourceMaps === "inline" ||
7255
(!cliOptions.outFile && babelOptions.sourceMaps)
7356
) {
74-
code += "\n" + convertSourceMap.fromObject(map).toComment();
57+
code += "\n" + convertSourceMap.fromObject(encodedMap(map)).toComment();
7558
}
7659

7760
return {
7861
map: map,
7962
code: code,
8063
};
8164
}
65+
function countNewlines(code: string): number {
66+
let count = 0;
67+
let index = -1;
68+
while ((index = code.indexOf("\n", index + 1)) !== -1) {
69+
count++;
70+
}
71+
return count;
72+
}
73+
function emptyMap() {
74+
return {
75+
version: 3,
76+
names: [],
77+
sources: [],
78+
mappings: [],
79+
};
80+
}
8281

8382
function output(fileResults: Array<string>): void {
8483
const result = buildResult(fileResults);
@@ -90,7 +89,7 @@ export default async function ({
9089
if (babelOptions.sourceMaps && babelOptions.sourceMaps !== "inline") {
9190
const mapLoc = cliOptions.outFile + ".map";
9291
result.code = util.addSourceMappingUrl(result.code, mapLoc);
93-
fs.writeFileSync(mapLoc, JSON.stringify(result.map));
92+
fs.writeFileSync(mapLoc, JSON.stringify(encodedMap(result.map)));
9493
}
9594

9695
fs.writeFileSync(cliOptions.outFile, result.code);

packages/babel-cli/test/fixtures/babel/filename --out-file --source-maps inline/out-files/script2.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/babel-cli/test/fixtures/babel/filename-sourcemap --out-file --source-maps inline/out-files/script2.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/babel-cli/test/fixtures/babel/filenames --out-file --source-maps inline/out-files/script3.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/babel-cli/test/fixtures/babel/filenames --out-file --source-maps/out-files/script3.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/babel-cli/test/fixtures/babel/stdin --out-file --source-maps/out-files/test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/babel-cli/test/fixtures/babel/stdin --source-maps inline/stdout.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ arr.map(function (x) {
55
});
66
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZGluIl0sIm5hbWVzIjpbImFyciIsIm1hcCIsIngiXSwibWFwcGluZ3MiOiI7O0FBQUFBLEdBQUcsQ0FBQ0MsR0FBSixDQUFRLFVBQUFDLENBQUM7QUFBQSxTQUFJQSxDQUFDLEdBQUdBLENBQVI7QUFBQSxDQUFUIiwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiB4KTsiXX0=
77

8-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsImZpbGUiOiJzdGRvdXQifQ==
8+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3Rkb3V0IiwibmFtZXMiOltdLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJtYXBwaW5ncyI6IiJ9

packages/babel-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"devDependencies": {
6868
"@babel/helper-transform-fixture-test-runner": "workspace:^",
6969
"@babel/plugin-transform-modules-commonjs": "workspace:^",
70-
"@jridgewell/trace-mapping": "^0.3.4",
70+
"@jridgewell/trace-mapping": "^0.3.8",
7171
"@types/convert-source-map": "^1.5.1",
7272
"@types/debug": "^4.1.0",
7373
"@types/resolve": "^1.3.2",

packages/babel-generator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"devDependencies": {
2727
"@babel/helper-fixtures": "workspace:^",
2828
"@babel/parser": "workspace:^",
29-
"@jridgewell/trace-mapping": "^0.3.4",
29+
"@jridgewell/trace-mapping": "^0.3.8",
3030
"@types/jsesc": "^2.5.0",
3131
"@types/source-map": "^0.5.0",
3232
"charcodes": "^0.2.0"

0 commit comments

Comments
 (0)