Support other JSX factories
Seems like Babel supports other JSX factories other than React.createElement(...):
Here is docs from deku another JSX framework from Segment.io
Using .babelrc:
{
"jsxPragma": "element"
}
Source: https://github.com/dekujs/deku/blob/master/docs/guides/jsx.md#babelrc
I propose we add something similar to the TS compiler options. Letting people to do a post build of preserved JSX element is not the nicest solution.
@axefrog what specific thing are you addressing there? We already have --jsx preserve, which is the most agnostic thing we could possibly do. React emit exists as a convenience for those who don't want to chain in another tool.
@RyanCavanaugh "React emit exists as a convenience for those who don't want to chain in another tool." ... what you mean to say is "React emit exists as a convenience that only people who specifically use React.js can use if they don't want to use another tool". I still cannot for the life of me figure out why it's so hard to output a function call that doesn't assume that React.js is the only game in town, or why you'd couple yourself to a single third-party rendering library when there's no good reason to do so. What are your thoughts on the fact that Babel will spit out any function call name that you want to use when transforming JSX?
@axefrog just to chime in, I am currently working on a PR that will let you specify the emit option in exactly the same way as the "pragma" jsx option in Babel (see https://babeljs.io/docs/plugins/transform-react-jsx/ ). This would make Typescript JSX support equivalent to Babel.
eta at some point, currently lower priority than my day job :)
@axefrog When we implemented JSX support, we decided that React emit + Preserve emit was the right trade-off in terms of convenience vs the engineering effort of adding more configurability. That was informed by the number of non-React JSX consumers in the world at the time (which is honestly still pretty low) and a general desire to reduce complexity where it makes sense. Non-React consumers do seem to be on the rise and that's why we're accepting PRs to add this functionality.
Again, this issue is marked Accepting PRs. If someone wants to implement this, they can. We are not opposed to allowing other factory names and we don't intend to strongly couple the compiler to any particular framework. Send us a pull request :wink:
@RyanCavanaugh thanks, you've satisfied my argumentative urges :)
@rwyborn are you giving this a try? I would suggest instead of JSX.createElement(...), it is better named as JSX.compose(...) so it would be more generic. Not all jsx compose to a DOM/VirtualDOM element.
They simply convert to a compilation target that can be understand by the respective framework.
Not all jsx compose to a DOM/VirtualDOM element.
Very good point.
A comment from #6146 I'd like people to weigh in on
How does this look?
let x = <Foo />;
--jsx preservelet x = <Foo />;
--jsx reactlet x = React.createElement(Foo);
--jsx react --reactNamespace rtlet x = rt.createElement(Foo);
--jsx makelet x = make(Foo);
A comment from #6146:
I would suggest to "pollute the global" with a meaningful namespace, such as
JSX.make()orJSX.compose(). But simplemake()might work too, just that it is a bit less convenience when I customize it in code (i.e. couldn't have a proper singleton such asJSXto hold states, when needed).
Apparently I don't know where should I add my comment to. :p
Maybe make it more customizable such as in: https://github.com/alexmingoia/jsx-transform
:+1:. I support @blesh's comment: Babel uses the /** @jsx: factoryFn */ syntax which is quite handy. A compiler flag would also be great. If support for this lands in TS (as in Babel), I can see a whole new way of creating templates from .tsx files instead of mustache/handlebars etc:
I setup a basic - but working - JSX to HTMLElement factory inspired by plain-jsx in TypeScript here: https://gist.github.com/Dynalon/a8790a1fa66bfd2c26e1 though I had to create a React shim so that it compiles with TS 1.7.5.
thanks @rwyborn!
A library for creating templates from .tsx files instead of mustache/handlebars is available here: https://github.com/wisercoder/uibuilder This takes advantage of the new reactNamespace flag of TypeScript 1.8
I know i'm a bit late into this discussion, but I would like to propose a simple and generic alternative. Add a new literal emit mode which transpiles the JSX expressions into object literals. This would make the output highly reusable in any custom JSX framework without dependencies on any predefined factory methods.
For example,
<MyComponent attr1="value1">
<div class="style1">
<SubComponent attr2="value2"/>
</div>
</MyComponent>
would get transpiled into:
{
"type": MyComponent,
"attributes": {
"attr1": "value1"
},
"children": [
{
"type": "div",
"attributes": {
"class": "style1"
},
"children": [
{
"type": SubComponent,
"attributes": {
"attr2": "value2"
}
}
]
}
]
}
It appears this feature was eventually added as compiler option --jsxFactory
See https://www.typescriptlang.org/docs/handbook/compiler-options.html
For those finding this in Google - Typescript 2.8 now supports the per-file jsxFactory feature mentioned a couple of times in this issue:
/** @jsx dom */
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html