TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Support other JSX factories

Open tinganho opened this issue 11 years ago • 36 comments

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.

tinganho avatar Jul 09 '15 00:07 tinganho

@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 avatar Dec 09 '15 18:12 RyanCavanaugh

@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 avatar Dec 09 '15 22:12 axefrog

@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 :)

rwyborn avatar Dec 09 '15 22:12 rwyborn

@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 avatar Dec 09 '15 22:12 RyanCavanaugh

@RyanCavanaugh thanks, you've satisfied my argumentative urges :)

axefrog avatar Dec 12 '15 00:12 axefrog

@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.

unional avatar Dec 17 '15 00:12 unional

Not all jsx compose to a DOM/VirtualDOM element.

Very good point.

mindplay-dk avatar Dec 17 '15 19:12 mindplay-dk

A comment from #6146 I'd like people to weigh in on


How does this look?

let x = <Foo />;
  • --jsx preserve
    • let x = <Foo />;
  • --jsx react
    • let x = React.createElement(Foo);
  • --jsx react --reactNamespace rt
    • let x = rt.createElement(Foo);
  • --jsx make
    • let x = make(Foo);

RyanCavanaugh avatar Dec 18 '15 17:12 RyanCavanaugh

A comment from #6146:

I would suggest to "pollute the global" with a meaningful namespace, such as JSX.make() or JSX.compose(). But simple make() 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 as JSX to hold states, when needed).

Apparently I don't know where should I add my comment to. :p

unional avatar Dec 18 '15 18:12 unional

Maybe make it more customizable such as in: https://github.com/alexmingoia/jsx-transform

unional avatar Jan 02 '16 07:01 unional

:+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.

Dynalon avatar Jan 07 '16 21:01 Dynalon

thanks @rwyborn!

mhegazy avatar Jan 07 '16 23:01 mhegazy

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

wisercoder avatar Mar 07 '16 00:03 wisercoder

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" 
                  }
               }
            ]
         }
      ]
   }

gilboa23 avatar Jun 15 '16 14:06 gilboa23

It appears this feature was eventually added as compiler option --jsxFactory

See https://www.typescriptlang.org/docs/handbook/compiler-options.html

mrcrowl avatar May 24 '17 10:05 mrcrowl

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

ravihugo avatar Apr 01 '18 02:04 ravihugo