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

[cli] Avoid SourceMapGenerator for simple map concatenation #14479

Merged
merged 2 commits into from Apr 21, 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
5 changes: 2 additions & 3 deletions packages/babel-cli/package.json
Expand Up @@ -24,14 +24,13 @@
"compiler"
],
"dependencies": {
"@jridgewell/trace-mapping": "^0.3.4",
"@jridgewell/trace-mapping": "^0.3.8",
"commander": "^4.0.1",
"convert-source-map": "^1.1.0",
"fs-readdir-recursive": "^1.1.0",
"glob": "^7.0.0",
"make-dir": "condition:BABEL_8_BREAKING ? : ^2.1.0",
"slash": "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0",
"source-map": "^0.5.0"
"slash": "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0"
},
"optionalDependencies": {
"@nicolo-ribaudo/chokidar-2": "condition:BABEL_8_BREAKING ? : 2.1.8-no-fsevents.3",
Expand Down
79 changes: 39 additions & 40 deletions packages/babel-cli/src/babel/file.ts
@@ -1,6 +1,5 @@
import convertSourceMap from "convert-source-map";
import { TraceMap, eachMapping } from "@jridgewell/trace-mapping";
import sourceMap from "source-map";
import { AnyMap, encodedMap } from "@jridgewell/trace-mapping";
import slash from "slash";
import path from "path";
import fs from "fs";
Expand All @@ -19,66 +18,66 @@ export default async function ({
babelOptions,
}: CmdOptions): Promise<void> {
function buildResult(fileResults: Array<any>): CompilationOutput {
const map = new sourceMap.SourceMapGenerator({
file:
cliOptions.sourceMapTarget ||
path.basename(cliOptions.outFile || "") ||
"stdout",
sourceRoot: babelOptions.sourceRoot,
});
const mapSections = [];

let code = "";
let offset = 0;

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

code += result.code + "\n";

if (result.map) {
const consumer = new TraceMap(result.map);

eachMapping(consumer, mapping => {
map.addMapping({
generated: {
line: mapping.generatedLine + offset,
column: mapping.generatedColumn,
},
source: mapping.source,
original:
mapping.source == null
? null
: {
line: mapping.originalLine,
column: mapping.originalColumn,
},
});
});

const { resolvedSources, sourcesContent } = consumer;
sourcesContent?.forEach((content, i) => {
if (content === null) return;
map.setSourceContent(resolvedSources[i], content);
});
mapSections.push({
offset: { line: offset, column: 0 },
map: result.map || emptyMap(),
});

offset = code.split("\n").length - 1;
}
code += result.code + "\n";
offset += countNewlines(result.code) + 1;
}

const map = new AnyMap({
version: 3,
file:
cliOptions.sourceMapTarget ||
path.basename(cliOptions.outFile || "") ||
"stdout",
sections: mapSections,
});
// For some reason, the spec doesn't allow sourceRoot when constructing a
// sectioned sorucemap. But AllMap returns a regular sourcemap, we can
// freely add to with a sourceRoot.
map.sourceRoot = babelOptions.sourceRoot;

// add the inline sourcemap comment if we've either explicitly asked for inline source
// maps, or we've requested them without any output file
if (
babelOptions.sourceMaps === "inline" ||
(!cliOptions.outFile && babelOptions.sourceMaps)
) {
code += "\n" + convertSourceMap.fromObject(map).toComment();
code += "\n" + convertSourceMap.fromObject(encodedMap(map)).toComment();
}

return {
map: map,
code: code,
};
}
function countNewlines(code: string): number {
let count = 0;
let index = -1;
while ((index = code.indexOf("\n", index + 1)) !== -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to handle all the possible newlines. Even if we always print \n, multiline template literals may still contain different line terminators.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a faster version of the code.split("\n").length that was there before. We can add more newline support, could we wait until a followup?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok sure, I didn't realize it was already like this 👍 (and no one ever complained, so it's super low priority).

count++;
}
return count;
}
function emptyMap() {
return {
version: 3,
names: [],
sources: [],
mappings: [],
};
}

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

fs.writeFileSync(cliOptions.outFile, result.code);
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.

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.

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

Expand Up @@ -5,4 +5,4 @@ arr.map(function (x) {
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0ZGluIl0sIm5hbWVzIjpbImFyciIsIm1hcCIsIngiXSwibWFwcGluZ3MiOiI7O0FBQUFBLEdBQUcsQ0FBQ0MsR0FBSixDQUFRLFVBQUFDLENBQUM7QUFBQSxTQUFJQSxDQUFDLEdBQUdBLENBQVI7QUFBQSxDQUFUIiwic291cmNlc0NvbnRlbnQiOlsiYXJyLm1hcCh4ID0+IHggKiB4KTsiXX0=

//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsImZpbGUiOiJzdGRvdXQifQ==
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3Rkb3V0IiwibmFtZXMiOltdLCJzb3VyY2VzIjpbXSwic291cmNlc0NvbnRlbnQiOltdLCJtYXBwaW5ncyI6IiJ9
2 changes: 1 addition & 1 deletion packages/babel-core/package.json
Expand Up @@ -67,7 +67,7 @@
"devDependencies": {
"@babel/helper-transform-fixture-test-runner": "workspace:^",
"@babel/plugin-transform-modules-commonjs": "workspace:^",
"@jridgewell/trace-mapping": "^0.3.4",
"@jridgewell/trace-mapping": "^0.3.8",
"@types/convert-source-map": "^1.5.1",
"@types/debug": "^4.1.0",
"@types/resolve": "^1.3.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-generator/package.json
Expand Up @@ -26,7 +26,7 @@
"devDependencies": {
"@babel/helper-fixtures": "workspace:^",
"@babel/parser": "workspace:^",
"@jridgewell/trace-mapping": "^0.3.4",
"@jridgewell/trace-mapping": "^0.3.8",
"@types/jsesc": "^2.5.0",
"@types/source-map": "^0.5.0",
"charcodes": "^0.2.0"
Expand Down
Expand Up @@ -19,7 +19,7 @@
"@babel/core": "workspace:^",
"@babel/helper-check-duplicate-nodes": "workspace:^",
"@babel/helper-fixtures": "workspace:^",
"@jridgewell/trace-mapping": "^0.3.4",
"@jridgewell/trace-mapping": "^0.3.8",
"quick-lru": "5.1.0",
"regenerator-runtime": "^0.13.7"
},
Expand Down
17 changes: 8 additions & 9 deletions yarn.lock
Expand Up @@ -202,7 +202,7 @@ __metadata:
dependencies:
"@babel/core": "workspace:^"
"@babel/helper-fixtures": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.4
"@jridgewell/trace-mapping": ^0.3.8
"@nicolo-ribaudo/chokidar-2": "condition:BABEL_8_BREAKING ? : 2.1.8-no-fsevents.3"
chokidar: ^3.4.0
commander: ^4.0.1
Expand All @@ -212,7 +212,6 @@ __metadata:
make-dir: "condition:BABEL_8_BREAKING ? : ^2.1.0"
rimraf: ^3.0.0
slash: "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0"
source-map: ^0.5.0
peerDependencies:
"@babel/core": ^7.0.0-0
dependenciesMeta:
Expand Down Expand Up @@ -335,7 +334,7 @@ __metadata:
"@babel/template": "workspace:^"
"@babel/traverse": "workspace:^"
"@babel/types": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.4
"@jridgewell/trace-mapping": ^0.3.8
"@types/convert-source-map": ^1.5.1
"@types/debug": ^4.1.0
"@types/resolve": ^1.3.2
Expand Down Expand Up @@ -457,7 +456,7 @@ __metadata:
"@babel/helper-fixtures": "workspace:^"
"@babel/parser": "workspace:^"
"@babel/types": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.4
"@jridgewell/trace-mapping": ^0.3.8
"@types/jsesc": ^2.5.0
"@types/source-map": ^0.5.0
charcodes: ^0.2.0
Expand Down Expand Up @@ -957,7 +956,7 @@ __metadata:
"@babel/core": "workspace:^"
"@babel/helper-check-duplicate-nodes": "workspace:^"
"@babel/helper-fixtures": "workspace:^"
"@jridgewell/trace-mapping": ^0.3.4
"@jridgewell/trace-mapping": ^0.3.8
"@types/jest": ^27.4.1
quick-lru: 5.1.0
regenerator-runtime: ^0.13.7
Expand Down Expand Up @@ -4085,13 +4084,13 @@ __metadata:
languageName: node
linkType: hard

"@jridgewell/trace-mapping@npm:^0.3.0, @jridgewell/trace-mapping@npm:^0.3.4":
version: 0.3.4
resolution: "@jridgewell/trace-mapping@npm:0.3.4"
"@jridgewell/trace-mapping@npm:^0.3.0, @jridgewell/trace-mapping@npm:^0.3.8":
version: 0.3.8
resolution: "@jridgewell/trace-mapping@npm:0.3.8"
dependencies:
"@jridgewell/resolve-uri": ^3.0.3
"@jridgewell/sourcemap-codec": ^1.4.10
checksum: ab8bce84bbbc8c34f3ba8325ed926f8f2d3098983c10442a80c55764c4eb6e47d5b92d8ff20a0dd868c3e76a3535651fd8a0138182c290dbfc8396195685c37b
checksum: 4b46f28c785133773de521677983186dfe7ed80872bee57b73624527e345954139ea45dfb7551a5f04de1773dc9501b896554a8f1c9e1c44ab2146a3c66b6fda
languageName: node
linkType: hard

Expand Down