rmorse / babel-plugin-jsx-template-vars

A Babel transform for rendering a template version of your React / Preact app. Useful for generating a pre-render for SSR.
MIT License
10 stars 0 forks source link

Auto detect variable type from context #8

Open rmorse opened 2 years ago

rmorse commented 2 years ago

It would make life a lot easier if we could detect the type automatically, so all we have to do is supply the name of the variable we want to expose...

For replace type variables, we just need to check for them being used in the output.

For list type variables, we can check for the existance of a .map or some other type of loop

And for control type variables we can check their usage in JSX expression, and if they are used in the left side of the expression...

It probably won't be 100% reliable but feels like it could be quite effective...

An additional feature worth investigating would be to have variables that have multiple "types"...

Eg, a replace variable could also be a control variable, which is not currently supported (this is a rather contrived example):

<>
    { name === '' && 'Unknown user' }
    { name !== '' && name }
</>