Hello Habr! i am Milos from Badoo, and also this is my Habr that is first post initially posted within our technology web log. Hope you enjoy it, and please share and remark for those who have any concerns
So… React, amirite.
Dozens of accomplishments, without also being fully a framework.
right right Here within the Cellphone online group, we don’t follow any strict JS frameworks – or at the least, any popular people – and now we work with a mix of legacy and modern technologies. Although that really works well for people, manipulating DOM is generally difficult, so 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 had been considered the choice that is best therefore we made a decision to opt for it.
We joined up with Badoo in the center of this procedure. Having bootstrapped and labored on React projects previously, I became alert to its advantages and disadvantages in training, but migrating an adult application with vast sums of users is an entirely various challenge|challenge that is wholly various.
Our own HTML files had been well organised, & most of y our rendering ended up being done since merely as template.render() . Exactly how could we retain this purchase and simpleness while going to React? In my opinion, technical problems apart, one concept ended up being apparent: change our current phone calls with JSX rule.
After some initial preparation we offered it and wrapped up a command-line tool that executes two easy things:
- Substitute render( that is template calls using the HTML content
Needless to say, this will just go us halfway, because we might still need to change the html page manually. Considering 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 may be explained, it may be implemented.
After demoing the first device to teammates, the feedback that is best i obtained had been that there’s a parser designed 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 i truly knew that this might 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 a broad accessibility to parsers, the procedure should always be comparable for translating some other popular language that is templating.
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 kind of template rule:
to its JSX rule equivalent:
See side-by-side comparison right here.
Following this, our procedure was pretty straightforward that is much. We immediately converted our templates from a single structure , and every thing worked needlessly to say (many thanks, automatic evaluating). To start with, we preserved our old template.render() API to help keep changes isolated.
needless to say, by using this approach you nevertheless end up getting templates rather than “proper” React components. The genuine advantage is when you look at the proven fact that it is much easier, or even trivial, to respond from templates which are currently JSX, generally by just wrapping a template rule in a function call.
It might seem: have you thought to compose brand new templates from scratch alternatively? The quick response is that there clearly was absolutely nothing incorrect with your old templates — we just had many of them. in terms of rewriting them and working towards real componentisation, that is a various story.
Some might argue that the component model is simply another trend that might pass, why commit to 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.
When you look at the nature to do something well, we’ve built these internal tools in a few components:
- dust2jsx — package accountable for real Dust to JSX interpretation
- 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 check always them away, and also other open-source materials on our GitHub web page. Please add or just leave us a remark if you learn them helpful.