Translating Dust templates to JSX center regarding the ten years (affected by the javaScript fram that is endless

Translating Dust templates to JSX center regarding the ten years (affected by the javaScript fram that is endless

Hello Habr! I am MiloЕЎ from Badoo, and also this is my very first Habr post, initially posted within our technology web log. Hope you enjoy it, and please share and remark when you yourself have any concerns

So… React, amirite.

It starred in the middle of the ten years (affected by the endless JavaScript framework wars), embraced the DOM, surprised everyone else by combining HTML with JavaScript and changed the net development landscape beyond recognition.

Dozens of accomplishments, without even being truly a framework.

Like it or hate it, React does one task very well, which is HTML templating. As well as a healthier ecosystem, it is maybe maybe maybe perhaps not difficult to realise why it became the most popular and influential JavaScript libraries, if you don’t typically the most popular certainly one of all.

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

Right right right Here within the Cellphone internet group, we don’t follow any strict JS frameworks – or at the least, any popular people – so we work with a mix of legacy and technologies that are modern. Although that really works well we wanted to alleviate this by reducing the number of В«manualВ» updates, increasing our code reuse and worrying less about memory leaks for us, manipulating DOM is usually hard, and.

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

We joined up with Badoo in the center of this method. Having bootstrapped and labored on React projects previously, I happened to be conscious of its advantages and disadvantages in training, but migrating an adult application with billions of users is a very different challenge|challenge that is wholly various.

Respond mixes HTML with JavaScript in a structure known as JSX. Though it seems like a template language, JSX is in fact simply a syntax, or syntactic sugar in the event that you will, for React calls, really similar-looking to HTML.

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

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

  1. Reads templates referenced in UI (JavaScript) file
  2. Substitute render( this is certainly template calls because of the HTML content

Needless to say, this could just go us halfway, because we might still need to alter the html page manually. Taking into consideration the amount and wide range of our templates, we knew that the most useful approach could be one thing automatic. The first concept sounded not difficult — and it can be implemented if it can be explained.

After demoing the first device to teammates, the feedback that is best had been there is certainly a parser 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 work!

Lo and behold, after a few times an instrument came into existence to transform Dust.js HTML-like templates to JSX React rule. We used Dust, however with a broad option of parsers, the method ought to be comparable for translating any kind of popular templating language.

For lots more details that are technical 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. within the really easiest of terms, it is about translating this kind of template rule:

to its JSX rule equivalent:

See side-by-side comparison right here.

following this, our procedure had been pretty straightforward that is much. We immediately converted our templates from 1 structure to some other, and every thing worked as you expected ( many thanks, automatic evaluation). To start with, we preserved our old render( this is certainly template API modifications separated.

needless to say, using this approach you nevertheless end up getting templates rather than “proper” React components. The benefit that is real when you look at the reality that it is much easier, if you don’t trivial, to respond from templates which are currently JSX, generally by merely wrapping a template rule in a function call.

It might seem: you will want to compose templates that are new scratch rather? The brief response is that absolutely nothing incorrect old templates — we merely had many of them. In terms of rewriting them and dealing towards real componentisation, that’s a story that is different.

Some might argue that the component model is merely another trend that might pass, why agree to it? It’s hard to anticipate, but one possible response is that you don’t need certainly 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 at Badoo.

Aided by the rise of ES7/8/Next, Elm and factor, and undoubtedly TypeScript and comparable solutions, rule that was once *.js is becoming increasingly more indistinguishable from JavaScript, and therefore trend seems like it is set . Rather than being overrun by it, have you thought to make use of that benefit?

Start supply

When you look at the character of doing a very important factor well, we’ve built these tools that are internal 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 simply just keep us a remark them useful if you find.

Publicada el: junio 25, 2021, por:

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *