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

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

Open
brianjenkins94 opened this issue Oct 26, 2024 · 5 comments
Open

Comments

@brianjenkins94
Copy link

No description provided.

@jehna
Copy link
Owner

jehna commented 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

@j4k0xb
Copy link
Contributor

j4k0xb commented 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

@0xdevalias
Copy link

0xdevalias commented 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:

@pionxzh
Copy link

pionxzh commented 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.

@pionxzh
Copy link

pionxzh commented 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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants