未加星标

Rawact compiles React.js components to pure JavaScript

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

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 undertakensomething 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

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:Rawact compiles React.js components to pure JavaScript
本站链接:https://www.codesec.net/view/611807.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(11)