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.
Dozens of accomplishments, without even being fully a framework.
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.
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:
- 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.
When you look at the nature of accomplishing something well, we’ve built these interior 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 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.