Hello Habr! I am Milos from Badoo, and also this is my very first Habr post, initially posted within our technology weblog. Hope you enjoy it, and please share and remark when you yourself have any queries
So… React, amirite.
It starred in the middle of the ten years (affected by the endless JavaScript framework wars), embraced the DOM, surprised every person by blending HTML with JavaScript and changed the internet development landscape beyond recognition.
Dozens of accomplishments, without also being 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 perhaps perhaps not difficult to understand why it became perhaps one of the most popular and influential JavaScript libraries, or even the most used certainly one of all.
yeah, he said he *hates* javascript frameworks…can you think that?
Right right right right Here within the mobile phone online group, we don’t follow any strict JS frameworks – or at the least, any popular people – and we also make use of mix of legacy and modern technologies. Although that actually works well for all of us, manipulating DOM is generally difficult, therefore we wished to relieve this by reducing 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 best option therefore we chose to opt for it.
We joined up with Badoo in the center of this procedure. Having bootstrapped and labored on React projects previously, I happened to be conscious of 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 various.
Respond mixes HTML with JavaScript in a structure known as 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 y our rendering ended up being done since just as template.render() . Exactly how could we retain this simplicity and order while going to respond? In my opinion, technical problems apart, one concept had been apparent: change our current phone 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:
- Reads templates referenced in UI (JavaScript) file
- Substitute template.render() calls using the HTML content
Of course, this might just go us halfway, because we would still need to change the rule manually. Considering the amount and wide range of our templates, we knew that the approach that is best could be one thing automatic. concept sounded not difficult — and if it could be explained, it could be implemented.
After demoing device to teammates, the best feedback i acquired ended up being there is a parser readily available for the templating language we used. This means that people could parse and convert rule easier than we’re able to with regular expressions, as an example. That’s whenever knew that this could work!
Lo and behold, after a few times an instrument had become to transform Dust.js HTML-like templates to JSX React rule. We used Dust, however with a broad accessibility to parsers, must be comparable for translating some other popular templating language.
For lots 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 was pretty much simple. We immediately converted our templates from 1 structure to a different, and every thing worked not surprisingly (many thanks, automatic assessment). To start with, we preserved our old template.render() API to help keep changes separated.
needless to say, applying this approach you nevertheless end up getting templates rather than “proper” React components. The genuine advantage is when you look at the reality that it is much easier, or even trivial, to respond from templates being currently JSX, more often than not by merely wrapping a template rule in a function call.
You may think: then write templates that are new scratch alternatively? The brief response is that there is absolutely nothing incorrect old templates — we just had most of them. in terms of rewriting them and working towards real componentisation, that’s a story that is different.
Some might argue that the component model is simply another trend which could pass, why agree to it? It’s hard to anticipate, but one feasible response is that 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.
Utilizing the rise of ES7/8/Next, Elm and factor, not forgetting TypeScript and comparable solutions, rule that has been once *.js is becoming progressively indistinguishable from JavaScript, and that trend seems like it is set to carry on. As opposed to being overrun by it, have you thought to make use of that to your benefit?
Start supply
Into the character of performing a very important factor well, we’ve built these interior tools in a few components:
- dust2jsx — package accountable for real Dust to JSX interpretation
- ratt (respond all the stuff) — command line device for reading/writing files on disk. Accountable for including referenced templates, and utilizes 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.

Deja una respuesta