Skip to content

Commit b6d8434

Browse files
authored
Switch to @jridgewell/gen-mapping for sourcemap generation (#14497)
1 parent f05b877 commit b6d8434

14 files changed

Lines changed: 141 additions & 135 deletions

File tree

lib/third-party-libs.js.flow

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,6 @@ declare module "semver" {
5151
}
5252
}
5353

54-
declare module "source-map" {
55-
declare export type SourceMap = {
56-
version: 3,
57-
file: ?string,
58-
sourceRoot: ?string,
59-
sources: [?string],
60-
sourcesContent: [?string],
61-
names: [?string],
62-
mappings: string,
63-
};
64-
65-
declare module.exports: {
66-
SourceMapGenerator: typeof SourceMapGenerator,
67-
}
68-
69-
declare class SourceMapGenerator {
70-
constructor(?{
71-
file?: string | null,
72-
sourceRoot?: string | null,
73-
skipValidation?: boolean | null,
74-
}): this;
75-
76-
addMapping({
77-
generated: {
78-
line: number,
79-
column: number,
80-
}
81-
}): void;
82-
83-
setSourceContent(string, string): void;
84-
85-
toJSON(): SourceMap;
86-
}
87-
}
88-
8954
declare module "convert-source-map" {
9055
import type { SourceMap, SourceMapGenerator } from "source-map";
9156

packages/babel-cli/test/fixtures/babel/dir --out-dir --source-maps inline/out-files/lib/bar/bar.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/dir --out-dir --source-maps inline/out-files/lib/foo.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/dir --out-dir --source-maps/out-files/lib/bar/bar.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/dir --out-dir --source-maps/out-files/lib/foo.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
@@ -3,6 +3,6 @@
33
arr.map(function (x) {
44
return x * x;
55
});
6-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZGluIl0sIm5hbWVzIjpbImFyciIsIm1hcCIsIngiXSwibWFwcGluZ3MiOiI7O0FBQUFBLEdBQUcsQ0FBQ0MsR0FBSixDQUFRLFVBQUFDLENBQUM7QUFBQSxTQUFJQSxDQUFDLEdBQUdBLENBQVI7QUFBQSxDQUFUIiwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiB4KTsiXX0=
6+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJhcnIiLCJtYXAiLCJ4Il0sInNvdXJjZXMiOlsic3RkaW4iXSwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiB4KTsiXSwibWFwcGluZ3MiOiI7O0FBQUFBLEdBQUcsQ0FBQ0MsR0FBSixDQUFRLFVBQUFDLENBQUM7QUFBQSxTQUFJQSxDQUFDLEdBQUdBLENBQVI7QUFBQSxDQUFUIn0=
77

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

packages/babel-cli/test/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ const assertTest = function (stdout, stderr, opts, cwd) {
121121
expect(actual).toBe(expected || "");
122122
}
123123
} catch (e) {
124-
e.message += "\n at " + filename;
125-
throw e;
124+
if (!process.env.OVERWRITE) {
125+
e.message += "\n at " + filename;
126+
throw e;
127+
}
128+
const expectedLoc = path.join(opts.testLoc, "out-files", filename);
129+
console.log(`Updated test file: ${expectedLoc}`);
130+
fs.writeFileSync(expectedLoc, actualFiles[filename]);
126131
}
127132
});
128133

@@ -267,6 +272,7 @@ fs.readdirSync(fixtureLoc).forEach(function (binName) {
267272
}
268273
});
269274

275+
opts.testLoc = testLoc;
270276
opts.outFiles = readDir(path.join(testLoc, "out-files"), fileFilter);
271277
opts.inFiles = readDir(path.join(testLoc, "in-files"), fileFilter);
272278

packages/babel-core/src/transformation/file/generate.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,24 @@ export default function generateCode(
4646
throw new Error("More than one plugin attempted to override codegen.");
4747
}
4848

49-
let { code: outputCode, map: outputMap } = result;
49+
// Decoded maps are faster to merge, so we attempt to get use the decodedMap
50+
// first. But to preserve backwards compat with older Generator, we'll fall
51+
// back to the encoded map.
52+
let { code: outputCode, decodedMap: outputMap = result.map } = result;
5053

51-
if (outputMap && inputMap) {
52-
outputMap = mergeSourceMap(
53-
inputMap.toObject(),
54-
outputMap,
55-
generatorOpts.sourceFileName,
56-
);
54+
if (outputMap) {
55+
if (inputMap) {
56+
// mergeSourceMap returns an encoded map
57+
outputMap = mergeSourceMap(
58+
inputMap.toObject(),
59+
outputMap,
60+
generatorOpts.sourceFileName,
61+
);
62+
} else {
63+
// We cannot output a decoded map, so retrieve the encoded form. Because
64+
// the decoded form is free, it's fine to prioritize decoded first.
65+
outputMap = result.map;
66+
}
5767
}
5868

5969
if (opts.sourceMaps === "inline" || opts.sourceMaps === "both") {

packages/babel-core/test/fixtures/transformation/source-maps/inline/output.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-generator/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
],
2121
"dependencies": {
2222
"@babel/types": "workspace:^",
23-
"jsesc": "condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1",
24-
"source-map": "^0.5.0"
23+
"@jridgewell/gen-mapping": "^0.1.0",
24+
"jsesc": "condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1"
2525
},
2626
"devDependencies": {
2727
"@babel/helper-fixtures": "workspace:^",
2828
"@babel/parser": "workspace:^",
2929
"@jridgewell/trace-mapping": "^0.3.8",
3030
"@types/jsesc": "^2.5.0",
31-
"@types/source-map": "^0.5.0",
3231
"charcodes": "^0.2.0"
3332
},
3433
"engines": {

0 commit comments

Comments
 (0)