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

Conversation

jridgewell
Copy link
Member

@jridgewell jridgewell commented Apr 20, 2022

Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? Yes, updates @jridgewell/trace-mapping and removes ancient source-map
License MIT

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:

--- 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"
 }

(That fifth letter that appears in some mappings segments is the names pointer)

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"
 }
```
@babel-bot
Copy link
Collaborator

babel-bot commented Apr 20, 2022

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/51708/

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories area: sourcemaps labels Apr 20, 2022
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).

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thanks!

@nicolo-ribaudo nicolo-ribaudo changed the title babel-cli: avoid SourceMapGenerator for simple map concatenation [cli] Avoid SourceMapGenerator for simple map concatenation Apr 21, 2022
@nicolo-ribaudo nicolo-ribaudo merged commit f51fc61 into babel:main Apr 21, 2022
@jridgewell jridgewell deleted the cli-anymap branch May 11, 2022 18:38
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 11, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: sourcemaps outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants