How to parse and modify Js using Rust?

I'm making proxy and I have to parse and modify Js response.
But I can't find the best crate for my purpose.

It depends on the level of modifications you have to do to the JS file. If you just need to replace some parts of it (e.g., replacing a placeholder __VERSION__ with a real version number) you can just go with string substitutions or regexps.

If you need to do extensive modifications, i.e., rewrite part of the program then you the best approach is to use a parser to generate an AST, do the changes to the AST and then serialize it back to JS code. In this case a good (but complex) package to use is tree-sitter. It is written in C, but crates.io has the Rust bindings. See also the documentation.

has a lot of tooling for parsing JS and TypeScript.

There's also:

1 Like