aleclarson / sorcery

Resolve a chain of sourcemaps back to the original source, like magic
3 stars 0 forks source link

Avoid lossy source map merging #1

Open aleclarson opened 6 years ago

aleclarson commented 6 years ago

As described by @eventualbuddha in https://github.com/Rich-Harris/sorcery/issues/67 and @mariusGundersen in https://github.com/mozilla/source-map/issues/216, merging 2+ source maps should never result in lost mappings. The current behavior of sorcery and Mozilla's source-map is to trace the line mappings of the root source map and toss out the rest of the tree, which results in a poor debugging experience. Below is my attempt to solve this issue.

Check out the tests here, and add a test (or file an issue) if you come across an edge case.

Most of the fix is contained in the blend function. It's well-documented, but questions are welcome. This function has one argument; a Node object that represents the "root" source map in a tree of source maps. The sources array of the given node is used to trace the root mappings array to its original source(s). Additionally, segments from any other nodes in the tree are interweaved into the root mappings array if they fit.

Since blend only mutates the given node, we need to start from the leaf nodes and work our way up to the root node so all source maps in the tree are blended together. This is done with the trace function.

I hope this solution works well for anyone who needs it. Please comment if you have any questions or find any bugs. Maybe this solution will even be adopted by Mozilla and @Rich-Harris. :)

Test plan

These are the tests I have so far. Passing tests have a checkmark. ✔︎

Test ideas are welcome. :)

whaaaley commented 6 years ago

How's the progress on this? Is this still something you're working on?

aleclarson commented 6 years ago

@whaaaley I'm taking the "fix it if I need it" approach due to time constraints. If you find any bugs, a repro would be handy. In its current state, it probably works in most situations. I'll be testing it on real applications in the near future.

whaaaley commented 6 years ago

Alrighty. I'll test it out for my css-in-js idea soon.

aleclarson commented 6 years ago

@whaaaley You can use @cush/sorcery to avoid cloning it yourself. 👍