wasp-lang / wasp

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

Wasp TS SDK (TypeScript-based Wasp AST generation) #551

Open faassen opened 2 years ago

faassen commented 2 years ago

Description

Wasp has its own DSL. This DSL lets you declare how a Wasp app goes together.

The DSL has the benefit that it makes very clear what is being configured, and lets you configure an entire full stack web app as a whole.

The DSL has some drawbacks:

As an alternative we could make available a TypeScript based frontend. This would generate the same appspec, but from TS. Advantages:

Drawbacks:

Technical challenges

While the TS implementation isn't particularly complex mostly, to enable inline functions and natural imports we actually need access to the source code of what's included later on in the generation process. To do this may require parsing the TypeScript and extracting that information into its own module (which can then be used by the generator).

As an intermediary solution to reach parity with existing Wasp we can make the user do this explicitly (and not allow any inline code) through a "jsRef" construct:

jsRef("./components", "MyComponent")

To allow type checking an optional third argument may be supplied that is the JS entity to refer to:

import {MyComponent} from "./components";

jsRef("./components", "MyComponent", MyComponent);

Later on we may introduce a facility to analyze imports and allow jsRefs to be generated automatically.

Martinsos commented 2 years ago

A quick go on pros/cons as I see it:

Btw looping, conditionals, no-name declarations -> we can implement all of those in the Wasp lang if/when we will need them. We do get them out of the box in TS, but it is not super hard to do in Wasp.

So with all this, it is a very attractive option to explore this more. With current state of Wasp, it makes sense to keep the Wasp DSL we have and focus on web framework features, but as we progress, it would be wise to do more experimentation with TS SDK and see how far can we get with it, what can we achieve. If we get to the point where we see that TS SDK is a better option than Wasp DSL, and it makes no sense to maintain both, we can even drop Wasp DSL as a compiler frontend.

It would be interesting to first just get TS SDK to the level where it can do exactly the same thing as current Wasp DSL does. So, generate equivalent AppSpec. At that point we can start using it for real, and we can also continue experimenting with it, making sure it follows up development of Wasp DSL, and also try to add additional features to it, improve it to be better than Wasp DSL. Also as we need to implement smth in Wasp DSL, will be able to consider how hard is it to do the same thing in TS SDK. Finally, as I mentioned above, if TS SDK is obviously better than Wasp DSL, we can switch exclusively to it.

faassen commented 2 years ago

If we had a way to import the appspec from JSON we could open up the ground for more experiments. You could then create some custom build command that uses the ingested JSON.

Martinsos commented 2 years ago

If we had a way to import the appspec from JSON we could open up the ground for more experiments. You could then create some custom build command that uses the ingested JSON.

Yes this is an exciting idea! If we make AppSpec serializable to JSON, which shouldn't be super hard, then we can as you said both have other tools generate that JSON, but also other tools consume that JSON. It does limit us a bit in the sense that AppSpec needs to be JSON serializable, but restrictions are also good, at least it is worth giving them a try.

Martinsos commented 1 month ago

This initiative is getting started now with #2047 !