You should look into how this is done at scale with something like Babel.
Babel parses JS into a AST (abstract syntax tree) and then lets you react to specific pieces of code.
Here is an example of the power of a tool like Babel: https://github.com/christina-de-martinez/babel-plugin-glowup...