<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Lokesh Prabakaran</title>
    <description>The latest articles on Forem by Lokesh Prabakaran (@lokesh007).</description>
    <link>https://forem.com/lokesh007</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F86266%2F9e527c6b-4772-4c5a-a639-4d6d51c504ed.jpg</url>
      <title>Forem: Lokesh Prabakaran</title>
      <link>https://forem.com/lokesh007</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lokesh007"/>
    <language>en</language>
    <item>
      <title>WebAssembly with Rust and React (Using create-react-app)</title>
      <dc:creator>Lokesh Prabakaran</dc:creator>
      <pubDate>Tue, 30 Jun 2020 18:57:30 +0000</pubDate>
      <link>https://forem.com/lokesh007/webassembly-with-rust-and-react-using-create-react-app-67</link>
      <guid>https://forem.com/lokesh007/webassembly-with-rust-and-react-using-create-react-app-67</guid>
      <description>&lt;p&gt;WebAssembly looks quite promising in bringing high performance, near-native speed to frontend applications, all from within the browser.&lt;/p&gt;

&lt;p&gt;This opens up entirely new fronts/avenues for frontend application which was previously inaccessible. There are numerous articles and writings on webassembly out there, and this article's scope is not about the workings of WebAssembly but instead about setting it up with Rust and React, so one can use this boilerplate code to build useful applications.&lt;/p&gt;

&lt;p&gt;The reason I chose Rust is that Rust as a language is really good and is well suited for systems programming and also I have been exploring Rust for close to a month for another side project of mine but that is for another day.&lt;/p&gt;

&lt;p&gt;Create a new app using create-react-app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app my-app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;create-react-app is the defacto tool to create a react app which takes care of setting up all the initial configuration and scripts, so developers can focus more on creating applications rather than configuring them&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo npm i -g wasm-pack --unsafe-perm=true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We will be using this to create a native rust package that is configured to compile to wasm. One can think of it as a create-react-app equivalent for the Rust module.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wasm-pack new &amp;lt;your-native-module-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This will create a native rust module&lt;/p&gt;

&lt;p&gt;Now, you can restructure the app the way you want, just make a note of the relative path of the native module from the project's root.&lt;/p&gt;

&lt;p&gt;Create-react-app takes care of all the configuration, but it does come at a price. We can't modify configurations to suit our needs, because that is the job of create-react-app. But for adding our rust module and WebAssembly into our build, we need to update webpack config.&lt;/p&gt;

&lt;p&gt;We don't need to eject create-react-app, there is a way we can do this without ejecting create-react-app; using ‘react-app-rewired’&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-app-rewired @wasm-tool/wasm-pack-plugin --save-dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;add config-overrides.js&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Rewrite your scripts config in your package.json file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
   "start": "react-app-rewired start",
   "build": "react-app-rewired build",
   "test": "react-app-rewired test",
   "eject": "react-scripts eject"
 },
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Done, you are all set for creating something useful with react and rust.&lt;/p&gt;

&lt;p&gt;Please refer to the github repo below for reference,&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/lokesh-007"&gt;
        lokesh-007
      &lt;/a&gt; / &lt;a href="https://github.com/lokesh-007/wasm-react-rust"&gt;
        wasm-react-rust
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      WebAssembly with Rust and React (Using create-react-app)
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>webassembly</category>
      <category>react</category>
      <category>rust</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
