Closed fmvilas closed 3 years ago
The most important is to have it as mentioned in the first line, along with Nunjucks, where Nunjucks is default, and with some alpha
flag one can enable React.
It can get really hard to read a complex template I just would like to argue here that it is a good reason for a change. Simply because you can also write an unreadable react component, while you also can write a perfectly structured Nunjucks template like the html-template. Unless you mean that there is a great plugin/tooling support for displaying the content of the react component
with some alpha flag one can enable React
We can instead rely on the template configuration file. If there's a renderer
field with value equal to react
, we proceed as a React template. Otherwise, it would use Nunjucks.
Unless you mean that there is a great plugin/tooling support for displaying the content of the react component
The difference is that anyone can easily create reusable components (partials) or add handy functions right inside the React file. The latter can be done with Nunjucks filters too but requires more effort because functions are not colocated with the code using them, which is great for reusability but a pain for simple one-time stuff. The former is simply not possible. We can't have reusable partials anyone can use in their templates (without having to copy/paste from somewhere).
In general, the more elaborated/handy components we can offer to the template authors, the easier is going to be to create a template and also read it. One common example is the variable indentation. You have to prepend something like {{ indent1 }}
as in this example, making the whole thing hard to read. That's the reason I'm including the <Text indentation={indentSize}>
component as a mandatory thing in this initial version.
We can instead rely on the template configuration file. If there's a renderer field with value equal to react, we proceed as a React template. Otherwise, it would use Nunjucks.
❤️
This issue has been automatically marked as stale because it has not had recent activity :sleeping: It will be closed in 30 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation. Thank you for your contributions :heart:
React as a Generator engine
Make Generator support React as a template engine, along with Nunjucks.
Problem
The current templating system —based on Nunjucks— has some really important drawbacks:
--debug
flag turned on. That's a problem of Nunjucks itself and has been there for quite some time now.typeof variable === 'object'
or running basic JS logic.Solution
In general, the solution goes through adding React as a template engine to the Generator. When the template declares that it's using React as its template engine, the Generator will render files using React instead of Nunjucks.
There a slight change in the "rendering paradigm" though. In the case of React, we don't "render the file" but instead we "import" it, "process" it, and get the result as a string that has to be written to the destination file. This will allow us to have top-level metadata on each file, like in the following example:
In the example above, we have a top-level
<File>
component that allows us to add meta information about the file itself, like its name, permissions, and more. Even more, we can tell Generator not to render this file by simply returningnull
in theSchemaFile
function:In the example above, this file will only be rendered if the type of the schema is
object
. This will allow us to eventually get rid of file name template syntax like$$schema$$
,$$everySchema$$
, and$$objectSchema$$
. Also, the usage of<File name="...">
will eventually allow us to get rid of all the file name templates.Solution elements
<File>
component: the component in charge of defining a file's metadata and content. As metadata, we should havename
and leavepermissions
as a nice to have.<Text>
component: this would be used for rendering plain text, e.g., code. Should offer props to set indentation and its type. For instance,<Text indentation={1} indentationType="tabs">
to indent the whole text with 1 tab.Rabbit holes & challenges
import
syntax may be challenging. Watch out. We may have to introduce a step to precompile ES7 and JSX syntax before the rendererrequire
s the files.Out of bounds