React, etc. Tech Stack

React, Flux, GraphQL, Hack, HHVM...? All of this and more!

Rawact compiles React.js components to pure JavaScript

React is now everywhere from JavaScript applications running in the browser to mobile devices leveraging the React Native toolkit on Android and iOS. In the browser React remains a view library, which means you need to ship the runtime in your payload for web apps written in React.js.

The web browser is already a runtime platform itself, so essentially what the React library itself does is add another layer on top of the native web platform. Web Components leverage the native capabilities of browsers directly.

Alternatively to shipping a runtime like with React of native Web Components, the trend of compiling frameworks in JavaScript has been a growing trend for some time. Frameworks like Svelte process the written code during a buildstep and the end result is a raw JavaScript bundle that does not require additional code.

Another approach is used by Ionic in their Stencil framework, which allows writing applications with the JSX and TypeScript syntaxes. These components are then compiled to standards compliant web components, negating the need for shipping a complete runtime for each user.

Compiling React.js to raw JavaScript instructions

So approaches mentioned above have not been available for React.js developers. However now, Tobias Koppers of Webpack fame has undertaken something that allows just this. He has written an experimental Babel plugin that compiles React components into native DOM instructions.

This allows leaner JavaScript builds as the library is not required to execute React based applications. While the work is very early, it shows great promise with an example of a React build of 126 KiB coming down to just 18.3 KiB with Rawact.

The implementation of Rawact is also relatively feature complete and has feature parity with the React API. This includes alpha level features such as React Hooks. For a quick example of what Rawact does in the background Tobias offers the example of a simple JSX element:

return <div className="test" />;

The above React code would render to this (annotated) example that runs without the overhead of sending the complete React.js view library to the client:

// module scope
const instructions = {};

return context => {
    // Check if rendering instructions are "similar"
    if (context._ !== instructions) {
        // Unmount old rendering instructions
        if (context.$) context.$();
        // Set own unmount functions and instructions marker
        context.$ = null;
        context._ = instructions;

        // Create DOM element
        context.a = createElement("div");

        // Set properties
        context.a.className = "test";
    return context.a;

Those interested in learning more about Rawac should head over to the project page on Github and view the project description for more on it's features, capabilities and future: babel-plugin-rawact

Written by Jorgé on Friday November 9, 2018

Permalink -

« TypeScript in create-react-app - What is HTTP/3? »