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
Conversation
This removes the last use of `source-map` package from babel-cli. Instead, we use a [SectionedSourceMap](https://github.com/jridgewell/trace-mapping/blob/8db5df070b15e2a669277b710a9d69ad0676524d/src/types.ts#L28-L32), which is purpose built for simple file concatenation like this. This should be considerably faster to generate sourcemaps now. And, if a sourcemaps are not needed, it's essentially free. As an added benefit, this gives us proper `names` support: ```diff --- a 2022-04-20 11:01:49.000000000 -0400 +++ b 2022-04-20 11:01:55.000000000 -0400 @@ -1,12 +1,17 @@ { "version": 3, "file": "script2.js", - "names": [], + "names": [ "arr", "map", "x", "MULTIPLIER" ], "sources": [ "script.js" ], "sourcesContent": [ "arr.map(x => x * MULTIPLIER);" ], - "mappings": ";;AAAA,GAAG,CAAC,GAAJ,CAAQ,UAAA,CAAC;AAAA,SAAI,CAAC,GAAG,UAAR;AAAA,CAAT" + "mappings": ";;AAAAA,GAAG,CAACC,GAAJ,CAAQ,UAAAC,CAAC;AAAA,SAAIA,CAAC,GAAGC,UAAR;AAAA,CAAT" } ```
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/51708/ |
function countNewlines(code: string): number { | ||
let count = 0; | ||
let index = -1; | ||
while ((index = code.indexOf("\n", index + 1)) !== -1) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
SourceMapGenerator
for simple map concatenation
@jridgewell/trace-mapping
and removes ancientsource-map
This removes the last use of
source-map
package from babel-cli. Instead, we use a SectionedSourceMap, which is purpose built for simple file concatenation like this.This should be considerably faster to generate sourcemaps now. If sourcemaps are not needed, it's essentially free. And as an added benefit, this gives us proper
names
support:(That fifth letter that appears in some mappings segments is the
names
pointer)