[GH-ISSUE #182] Instead of rewriting the source, what about creating a sourcemap? #52

Open
opened 2026-03-03 13:52:37 +03:00 by kerem · 5 comments
Owner

Originally created by @brianjenkins94 on GitHub (Oct 26, 2024).
Original GitHub issue: https://github.com/jehna/humanify/issues/182

Originally created by @brianjenkins94 on GitHub (Oct 26, 2024). Original GitHub issue: https://github.com/jehna/humanify/issues/182
Author
Owner

@jehna commented on GitHub (Oct 27, 2024):

A great idea! Do you know anything about creating sourcemaps with Babel or similar tools? I think usually Babel creates sourcemaps that start from the original file and you end up with a sourcemap mapping from the end result (e.g. a minified file) to the original file.

In Humanify's case we'd need to do the reverse: Start with a minified file, apply Prettier, Webcrack and Babel transformations, and end up with a source map mapping from the original (minified) file to the end result (un-minified file).

If anybody has ideas on how to do this with any existing tools, I'd be happy to give it a look

<!-- gh-comment-id:2439925836 --> @jehna commented on GitHub (Oct 27, 2024): A great idea! Do you know anything about creating sourcemaps with Babel or similar tools? I think usually Babel creates sourcemaps that start from the original file and you end up with a sourcemap mapping from the end result (e.g. a minified file) to the original file. In Humanify's case we'd need to do the reverse: Start with a minified file, apply Prettier, Webcrack and Babel transformations, and end up with a source map mapping from the original (minified) file to the end result (un-minified file). If anybody has ideas on how to do this with any existing tools, I'd be happy to give it a look
Author
Owner

@j4k0xb commented on GitHub (Oct 29, 2024):

Instead of rewriting the source, what about creating a sourcemap?

Renaming itself has many edge cases (const { a } = b -> const { a: foo } = b), and there are also other transformations in humanify.
It would be simpler to rewrite the source and make babel generate a source-map additionally.

Here's a snippet to reverse it: https://gist.github.com/j4k0xb/0d76d103db9696b515ce9c51c45b76b8

and you probably need a way to combine multiple source-maps

<!-- gh-comment-id:2444439765 --> @j4k0xb commented on GitHub (Oct 29, 2024): > **Instead of** rewriting the source, what about creating a sourcemap? Renaming itself has many edge cases (`const { a } = b` -> `const { a: foo } = b`), and there are also other transformations in humanify. It would be simpler to rewrite the source and make babel generate a source-map additionally. Here's a snippet to reverse it: https://gist.github.com/j4k0xb/0d76d103db9696b515ce9c51c45b76b8 - prettier: doesn't support source-maps https://github.com/prettier/prettier/issues/11136 - webcrack: also not, but it's possible to expose it - your babel transformations: also possible and you probably need a way to combine multiple source-maps
Author
Owner

@0xdevalias commented on GitHub (Dec 10, 2024):

It would be simpler to rewrite the source and make babel generate a source-map additionally.

^Big agree on this.


I haven't looked deeply into this, but I switched from prettier to biome in my RE project; I wonder if it can generate sourcemaps for it's formatting? (ChatGPT seems to think it can't)

Even if neither of these tools can; I wonder if it would be possible to take the 'before formatting' and 'after formatting' code, and then generate a sourcemap based on the diff between them?


See also:

<!-- gh-comment-id:2533069602 --> @0xdevalias commented on GitHub (Dec 10, 2024): > It would be simpler to rewrite the source and make babel generate a source-map additionally. ^Big agree on this. --- > * prettier: doesn't support source-maps [Source maps prettier/prettier#11136](https://github.com/prettier/prettier/issues/11136) I haven't looked deeply into this, but I switched from `prettier` to `biome` in my RE project; I wonder if it can generate sourcemaps for it's formatting? (ChatGPT seems to think it can't) - https://biomejs.dev/formatter/ Even if neither of these tools can; I wonder if it would be possible to take the 'before formatting' and 'after formatting' code, and then generate a sourcemap based on the diff between them? --- See also: - https://github.com/pionxzh/wakaru/issues/66
Author
Owner

@pionxzh commented on GitHub (Dec 11, 2024):

It will be challenging to compare before and after to generate the sourcemap. I didn't look into humanity's source code detailly, but you should be able to let babel generate the sourcemap since most of the operation is renaming. Prettier does not support sourcemap, but I have no idea what other alternatives provide this functionality.

<!-- gh-comment-id:2534768826 --> @pionxzh commented on GitHub (Dec 11, 2024): It will be challenging to compare before and after to generate the sourcemap. I didn't look into humanity's source code detailly, but you should be able to let babel generate the sourcemap since most of the operation is renaming. Prettier does not support sourcemap, but I have no idea what other alternatives provide this functionality.
Author
Owner

@pionxzh commented on GitHub (Dec 11, 2024):

I have tried using https://github.com/Rich-Harris/magic-string to do some code transformation with source map support, but I'm not sure if it fits into the use case here.

<!-- gh-comment-id:2534826941 --> @pionxzh commented on GitHub (Dec 11, 2024): I have tried using https://github.com/Rich-Harris/magic-string to do some code transformation with source map support, but I'm not sure if it fits into the use case here.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/humanify#52
No description provided.