Translating Dust templates to JSX center associated with the ten years (affected by the endless JavaScript fram

Translating Dust templates to JSX center associated with the ten years (affected by the endless JavaScript fram

Hello Habr! i am Milos from Badoo, and also this is my very first Habr post, initially posted inside our technology web log. Hope you enjoy it, and please share and remark for those who have any queries

So… React, amirite.

It starred in the center of the ten years (suffering from the endless framework that is javaScript), embraced the DOM, surprised everyone else by blending HTML with JavaScript and changed the net development landscape beyond recognition.

Dozens of accomplishments, without even being fully a framework.

Think it’s great or hate it, React does one task very well, and that’s HTML templating. Along with a healthier ecosystem, it is maybe maybe maybe not difficult to realise why it became probably one of the most popular and influential JavaScript libraries, or even the preferred certainly one of all.

yeah, he said he *hates* javascript frameworks…can you think that?

Right right Here when you look at the Cellphone internet team, we don’t follow any strict JS frameworks – or at the least, any popular people – so we make use of a mix of legacy and technologies that are modern. Although that actually works well for people, manipulating DOM is generally difficult, therefore we desired to relieve this by decreasing the wide range of «manual» updates, increasing our rule reuse and stressing less about memory leakages.

After some research, respond ended up being considered the choice that is best and we also chose to opt for it.

We joined up with Badoo in the exact middle of this procedure. Having bootstrapped and labored on React projects previously, I happened to be alert to its benefits and drawbacks in training, but migrating an adult application with vast sums of users is a totally various challenge|challenge that is totally different.

Respond mixes HTML with JavaScript in a structure called JSX. If you will, for React calls, very similar-looking to HTML although it looks like a template language, JSX is actually just a syntax, or syntactic sugar.

Our own HTML files had been well organised, and most of our rendering ended up being done because merely as template.render() . Exactly how could we retain this purchase and ease of use while going to respond? If you ask me, technical problems apart, one idea had been apparent: change our current telephone calls with JSX rule.

After some planning that is initial offered it and wrapped up a command-line tool that executes two easy things:

  1. Reads templates referenced in UI (JavaScript) file
  2. Substitute template.render() calls using the HTML content

Needless to say, this will just go us halfway, because we might nevertheless have to change the html page manually. Taking into consideration the amount and quantity of our templates, we knew that the approach that is best will be one thing automatic. The initial concept sounded not difficult — and if it could be explained, it may be implemented.

After demoing the first device to teammates, the most readily useful feedback i acquired had been there is a parser readily available for the templating language we used. This means than we could with regular expressions, for example that we could parse and translate code much easier. That’s whenever knew that this could work!

Lo and behold, after a few times something had become to transform Dust.js HTML-like templates to JSX React rule. We utilized Dust, however with an extensive option of parsers, must be comparable for translating any kind of popular templating language.

To get more technical details, skip towards the Open-source part below. We utilized tools like Esprima to parse JS rule, and a PEG.js parser generator to parse Dust templates. into the really easiest of terms, it is about translating this particular template rule:

to its JSX rule equivalent:

See side-by-side comparison right here.

following this, our procedure ended up being pretty straightforward that is much. We immediately converted our templates in one structure , and every thing worked not surprisingly ( many thanks, automatic screening). To start with, we preserved our old render( this is certainly template API changes isolated.

Needless to say, with this specific approach you nevertheless get templates rather than “proper” React components. The genuine advantage is into the proven fact that it is much easier, if maybe not trivial, to respond from templates being currently JSX, more often than not by merely wrapping a template rule in a function call.

It might seem: have you thought to compose templates that are new scratch rather? The quick response is that there clearly was absolutely nothing incorrect with this old templates — we merely had plenty of them. in terms of rewriting them and dealing towards real componentisation, that’s a various story.

Some might argue that the component model is merely another trend which might pass, why invest in it? It’s hard to anticipate, but one feasible response is which you don’t need to. Until you find the format that works best for your team if you iterate quickly, you can try out different options, without spending too much time on any of them. That’s certainly one of the core concepts for people at Badoo.

Because of the rise of ES7/8/Next, Elm and factor, not forgetting TypeScript and comparable solutions, rule that was once *.js is becoming increasingly more indistinguishable from JavaScript, and that trend appears like it is set . In the place of being overrun by it, you will want to utilize that to the benefit?

Start supply

When you look at the nature of accomplishing something well, we’ve built these interior tools in a few components:

  1. dust2jsx — package accountable for real Dust to JSX interpretation
  2. ratt (React All the plain things) — command line device for reading/writing files on disk. In charge of including referenced templates, and makes use of dust2jsx internally to transform rule

We’ve even open-sourced these tools — make sure to always check them down, and also other open-source materials on our GitHub web page. Please add or just keep us a remark them useful if you find.