wasp-lang / wasp

The fastest way to develop full-stack web apps with React & Node.js.
https://wasp-lang.dev
MIT License
13.53k stars 1.18k forks source link

Consider having a mechanism for patching the generated code / templates #1245

Open Martinsos opened 1 year ago

Martinsos commented 1 year ago

@Zeko369 (what a surprise, since he is known for practicing patching/hacking Wasp templates :D) suggested that we might consider adding a mechanism for patching the generated Wasp code / templates.

So right now, if you want to change how Wasp does some things internally, you can:

  1. Modify the generated code. Doing it manually is not fun because each time something changes in Wasp project, those changes will get lost. So you would probably need to write some script, and run it either manually of automatically (which is also not an easy feat, automating that to happen at the right moment).
  2. Modifying Wasp templates. Wasp uses those to define big parts of the code it generates. To modify these, you need to find them on disk and modify them. This is not documented, as a technique, nor path to them. Modifications to them are permanent, but only per Wasp version -> so new version of Wasp will not carry over the changes you did to the previous version, because it has its own template files. Also, changes are global, so for all Wasp projects you are working on.

Patching mechanism could work in two ways:

  1. Patch the generated code. User would provide some kind of script that could modify generated files. It might be called for each file and given its contents to modify, or maybe it would manually read them from disk and modify them, or maybe it would just be a directory with files to use to overwrite Wasp generated file -> we would have to figure out the best interface. What is important is that Wasp should call it each time generated files change. Maybe it would know which files exactly script is interested in and therefore notify it only when those files change. Just having this hind of hook that would call user's script when specified generated files change could already be useful.
  2. Path Wasp templates. They could provide template files that should replace our template files (some of them). We would then use those instead of ours. They would probably list them in a special directory. This would be per-project, which is big improvement compared to when done manually. It could also work even with version change. Although we might want to warn them if template file they are replacing has changed, so they can check if their template file still makes sense (compare checksums of old and new template).
Martinsos commented 4 months ago

This could also be generalized via "Wasp lifecycle hooks". If we has such hooks, we could define in "pre-start" hook that we want to overwrite certain templates with other templates. Although it would then also be important to somehow reset them hm. Ok, maybe doing it via Wasp lifecycle hooks is not the best solution due to the need to reset them, so the remains don't remain for another project. Or, if we had a hook which is able to modify template files specifically, than that is a straightforward solution.