<?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>DEV Community: Blesson Abraham</title>
    <description>The latest articles on DEV Community by Blesson Abraham (@blessonabraham).</description>
    <link>https://dev.to/blessonabraham</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%2F830542%2Fb6fbca03-8aa0-47da-8a6c-7f6161cf4d89.jpg</url>
      <title>DEV Community: Blesson Abraham</title>
      <link>https://dev.to/blessonabraham</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/blessonabraham"/>
    <language>en</language>
    <item>
      <title>Multithreading with Web Worker in React</title>
      <dc:creator>Blesson Abraham</dc:creator>
      <pubDate>Tue, 03 May 2022 08:41:08 +0000</pubDate>
      <link>https://dev.to/blessonabraham/multithreading-with-web-worker-in-react-3nic</link>
      <guid>https://dev.to/blessonabraham/multithreading-with-web-worker-in-react-3nic</guid>
      <description>&lt;p&gt;Javascript is a single-threaded language, No matter what async operations you use like promises, async, await, settimeout, etc.. it all ends up in the exact call stack now or later.&lt;/p&gt;

&lt;p&gt;If you have ever operated on enterprise projects, you know the main challenges are optimization and performance. So we try to make the UI as lightweight as attainable. As I've mentioned above micro tasks (async, await..) and macro tasks (settimeout, setinterval..) both are not executed on separate threads, It's just deferred and executed later on the same UI thread.&lt;/p&gt;

&lt;p&gt;Here in this writing, we will leverage the API that's available in browsers and NodeJs to achieve multithreading in Javascript apps.&lt;/p&gt;

&lt;p&gt;So let's get started by setting up a React app with Typescript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; npx create-react-app demo-app --template typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What are web workers?
&lt;/h2&gt;

&lt;p&gt;Web-workers are APIs provided by browser/nodeJS which will take your intensive tasks into a separate thread and will do the execution. So the communication between the main thread and the worker thread is done via postMessages(). Web Workers don’t have access to some very crucial JavaScript features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The DOM (it’s not thread-safe).&lt;/li&gt;
&lt;li&gt;The window object.&lt;/li&gt;
&lt;li&gt;The document object.&lt;/li&gt;
&lt;li&gt;The parent object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's create a file in the src folder where we keep all the intensive long-running code.&lt;/p&gt;

&lt;p&gt;If you see below we are using postMessage() to communicate back to the main thread and // eslint-disable-next-line no-restricted-globals because self cannot be used in react without context objects.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;intensiveTasks.ts&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const workercode = () =&amp;gt; {

  // eslint-disable-next-line no-restricted-globals
  self.onmessage = function (e) {
    const data = e.data
    // eslint-disable-next-line no-restricted-globals
    self.postMessage(data);
  }

  setTimeout(() =&amp;gt; {
    // eslint-disable-next-line no-restricted-globals
    self.postMessage("Worker Thread: Hi");
  }, 2000);


};

let code = workercode.toString();
code = code.substring(code.indexOf("{") + 1, code.lastIndexOf("}"));

const blob = new Blob([code], { type: "application/javascript" });
const worker_script = URL.createObjectURL(blob);

export default worker_script;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here we use the same postMessage() to communicate with the worker thread and myWorker.onmessage is used to receive the message back from the worker thread.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;App.tsx&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useRef, useState } from 'react';
import './App.css';
import IntensiveTask from './intensiveTask';

function App() {

  const [currentMessage, setCurrentMessage] = useState&amp;lt;string&amp;gt;('')
  const [allMessages, setAllMessages] = useState&amp;lt;string[]&amp;gt;([])

  const initiateWebWorker = () =&amp;gt; {
    var myWorker = new Worker(IntensiveTask);

    // When you want to get messages from Worker Thread
    myWorker.onmessage = (message: any) =&amp;gt; {
      setCurrentMessage(message.data)
    };

    // When you want to send messages to worker thread  
    myWorker.postMessage('Main Thread: Hello');
  }

  useEffect(() =&amp;gt; initiateWebWorker(), [])

  useEffect(() =&amp;gt; setAllMessages([...allMessages, currentMessage]), [currentMessage])

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;div className="App-header"&amp;gt;
        &amp;lt;b&amp;gt;Web Worker Example&amp;lt;/b&amp;gt;
        &amp;lt;p&amp;gt;{allMessages.map((data) =&amp;gt; &amp;lt;div&amp;gt;{data}&amp;lt;/div&amp;gt;)}&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Now you can kickstart our react app using npm start command.&lt;/p&gt;

&lt;p&gt;GitHub: &lt;a href="https://github.com/blessonabraham/webworkers-react"&gt;https://github.com/blessonabraham/webworkers-react&lt;/a&gt;&lt;br&gt;
Codesandbox: &lt;a href="https://codesandbox.io/s/webworkers-react-8gvljs"&gt;https://codesandbox.io/s/webworkers-react-8gvljs&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/webworkers-react-8gvljs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Building Micro Frontend with React &amp; Module Federation</title>
      <dc:creator>Blesson Abraham</dc:creator>
      <pubDate>Sat, 30 Apr 2022 09:18:24 +0000</pubDate>
      <link>https://dev.to/blessonabraham/building-micro-frontend-with-react-module-federation-3j46</link>
      <guid>https://dev.to/blessonabraham/building-micro-frontend-with-react-module-federation-3j46</guid>
      <description>&lt;p&gt;We will be creating a production-ready micro-front end app using React, Redux, Typescript, Tailwind CSS, React Router, and Webpack but the scope of the article will be too broad and will be split up into a series&lt;/p&gt;

&lt;p&gt;Here in this unit, we will be setting up a simple micro front end app with React, Typescript, and Tailwind CSS, and yes to simplify things, we will be using Lerna to set up a mono repo.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's a Micro FrontEnd?
&lt;/h2&gt;

&lt;p&gt;Micro frontends are similar to the microservices concept. Here each part of the WebApp you see, for instance, the Header can be in react and the sidebar can be in Angular, Vue, or any other framework. So we will have a Host/Container app that will fetch bundled codes from different URLs on load. It opens up the possibility of independent teamwork without any interdependency.&lt;/p&gt;

&lt;p&gt;Without boring you much with the details, let's get started and will tell you the details later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Folder Formation
&lt;/h2&gt;

&lt;p&gt;Create the folders in a similar way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- micro-frontend
   - packages
      - header
      - host
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, we will be just having a header as a micro front end for now, and the host will be calling the header on load. If you are wondering why we have created these folders under packages, it's because we are using Lerna, and it's the recommended practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Header
&lt;/h2&gt;

&lt;p&gt;Lets initialize npm in the folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now install the main dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Module federation is still not yet implemented in Create-React-App(CRA). So will be using webpack 5 to build the project. In CRA, under the hood, It's using Webpack but with CRA, we are completely freed up from the hustle of setting up webpack. It's not that complex to set it up if we understand what it's doing.&lt;/p&gt;

&lt;p&gt;let's install the dev-dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D @babel/core @babel/preset-react @babel/preset-typescript autoprefixer babel-loader css-loader file-loader html-webpack-plugin mini-css-extract-plugin postcss postcss-loader style-loader tailwindcss webpack webpack-cli webpack-dev-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we are using typescript to write this project, let's install the required type definitions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D @types/mini-css-extract-plugin @types/react @types/react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, your package.json would look like the below.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;package.json&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "header",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^18.0.0",
    "react-dom": "^18.0.0"
  },
  "devDependencies": {
    "@babel/core": "^7.17.9",
    "@babel/preset-react": "^7.16.7",
    "@babel/preset-typescript": "^7.16.7",
    "@types/mini-css-extract-plugin": "^2.5.1",
    "@types/react": "^18.0.5",
    "@types/react-dom": "^18.0.1",
    "autoprefixer": "^10.4.4",
    "babel-loader": "^8.2.5",
    "css-loader": "^6.7.1",
    "file-loader": "^6.2.0",
    "html-webpack-plugin": "^5.5.0",
    "mini-css-extract-plugin": "^2.6.0",
    "postcss": "^8.4.12",
    "postcss-loader": "^6.2.1",
    "style-loader": "^3.3.1",
    "tailwindcss": "^3.0.24",
    "webpack": "^5.72.0",
    "webpack-cli": "^4.9.2",
    "webpack-dev-server": "^4.8.1"
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned above, We are using webpack, to limit the scope of this article, we are not gonna go into many details but will just give you a high-level overview.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is webpack?&lt;/strong&gt;&lt;br&gt;
Webpack is a module bundler library, Which means, that when we run an npm run/serve command against a webpack project, webpack will kick in and it will read through webpack.config.js then compile and build your project using the dependencies that we mentioned in this config file. In webpack we have plugins and modules, &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Loaders&lt;/strong&gt; work at a file level, If we mention the file extension and the dependency then webpack will use that dependency to compile/transpile the files with mentioned extensions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Plugins&lt;/strong&gt; work at a system level. They can work on the pattern, file system handling (name, path), etc. For instance, we are using CleanWebpackPlugin, which will clean the bundle folder before generating another build.&lt;/p&gt;

&lt;p&gt;HtmlWebpackPlugin: It will generate an HTML5 file for you that includes all your webpack bundles in the body using script tags.&lt;/p&gt;

&lt;p&gt;MiniCssExtractPlugin: It extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS and SourceMaps.&lt;/p&gt;

&lt;p&gt;ModuleFederationPlugin: Module federation allows a JavaScript application to dynamically run code from another bundle/build, on the client and server. And here below, we expose header component.&lt;/p&gt;

&lt;p&gt;and now that you know what is webpack, let's create the config file.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;webpack.config.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
const deps = require("./package.json").dependencies;

module.exports = {
    entry: './src/index.ts',
    output: {
        filename: '[name].[contenthash].js',
        path: path.join(process.cwd(), 'dist')
    },
    plugins: [
        new CleanWebpackPlugin(),
        new MiniCssExtractPlugin({
            filename: '[name].[contenthash].css',
        }),
        new HtmlWebpackPlugin({
            template: './public/index.html',
        }),
        new ModuleFederationPlugin({
            name: 'header',
            filename: 'remoteEntry.js',
            exposes: {
                './header': './src/Header',
            },
            shared: {
                ...deps,
                react: {
                    singleton: true,
                    requiredVersion: deps.react,
                },
                "react-dom": {
                    singleton: true,
                    requiredVersion: deps["react-dom"],
                },
            },
        })
    ],
    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
    },
    module: {
        rules: [
            {
                test: /\.(ts|tsx)?$/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-typescript", "@babel/preset-react"]
                    }
                }],
                exclude: /[\\/]node_modules[\\/]/
            },
            {
                test: /\.(css|s[ac]ss)$/i,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
            },
            {
                test: /\.(png|jpg|gif)$/i,
                type: 'asset/resource'
            }
        ]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's create the react files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index file&lt;/strong&gt;, let's just import the Bootstrap file where we exactly do the stuff that usually gets done in the index file. Its because you might get run into an error like &lt;em&gt;Shared module is not available for eager consumption&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;index.ts&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import('./Bootstrap')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;bootstrap.tsx&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Header from './Header';

ReactDOM.render(&amp;lt;Header /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and one importing thing here for you to notice, We are exposing the header component through module federation and you should be importing necessary CSS in the header component, So the CSS you imported will be exposed for the exposed component and its subcomponents. Parent component CSS wont be exposed.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;header.tsx&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as React from 'react';
import "./header.scss"

const Header = () =&amp;gt; {
  return (
    &amp;lt;nav class="font-sans flex flex-col text-center sm:flex-row sm:text-left sm:justify-between py-4 px-6 bg-white shadow sm:items-baseline w-full"&amp;gt;
      &amp;lt;div class="mb-2 sm:mb-0"&amp;gt;
        &amp;lt;a href="/home" class="text-2xl no-underline text-grey-darkest hover:text-blue-dark"&amp;gt;Simple Header&amp;lt;/a&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;a href="/one" class="text-lg no-underline text-grey-darkest hover:text-blue-dark ml-4"&amp;gt;Link 1&amp;lt;/a&amp;gt;
        &amp;lt;a href="/two" class="text-lg no-underline text-grey-darkest hover:text-blue-dark ml-4"&amp;gt;Link 2&amp;lt;/a&amp;gt;
        &amp;lt;a href="/three" class="text-lg no-underline text-grey-darkest hover:text-blue-dark ml-4"&amp;gt;Link 3&amp;lt;/a&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/nav&amp;gt;
  )
};
export default Header
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;header.css&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@tailwind base;
@tailwind components;
@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that's it, now if you run npm serve in this folder, it will just start running on port 3001&lt;/p&gt;

&lt;h2&gt;
  
  
  Host
&lt;/h2&gt;

&lt;p&gt;Let's create the host app and call the header app into it.&lt;/p&gt;

&lt;p&gt;let's initiate npm&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; npm init  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the main dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react react-dom  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and now the dev-dependencies. if you notice, here we are not installing some libraries like Tailwind CSS, which is not necessary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D @babel/core @babel/preset-react @babel/preset-typescript babel-loader css-loader html-webpack-plugin mini-css-extract-plugin postcss postcss-loader style-loader webpack webpack-cli webpack-dev-server clean-webpack-plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now your package.json file might look similar below, don't miss out to add the script section to yours. It's needed for running the app.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;package.json&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "host",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "serve": "webpack serve --mode development --port 3000  --open",
    "build-dev": "webpack --mode development",
    "build-prod": "webpack --mode production"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "clean-webpack-plugin": "^4.0.0",
    "react": "^18.0.0",
    "react-dom": "^18.0.0"
  },
  "devDependencies": {
    "@babel/core": "^7.17.9",
    "@babel/preset-react": "^7.16.7",
    "@babel/preset-typescript": "^7.16.7",
    "babel-loader": "^8.2.5",
    "css-loader": "^6.7.1",
    "html-webpack-plugin": "^5.5.0",
    "mini-css-extract-plugin": "^2.6.0",
    "postcss": "^8.4.12",
    "postcss-loader": "^6.2.1",
    "style-loader": "^3.3.1",
    "webpack": "^5.72.0",
    "webpack-cli": "^4.9.2",
    "webpack-dev-server": "^4.8.1"
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here below we consume the header component with the module federation plugin.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;webpack.config.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');

module.exports = {
    entry: './src/index.ts',
    output: {
        filename: '[name].[contenthash].js',
        path: path.join(process.cwd(), 'dist')
    },
    plugins: [
        new CleanWebpackPlugin(),
        new MiniCssExtractPlugin({
            filename: '[name].[contenthash].css',
        }),
        new HtmlWebpackPlugin({
            template: './public/index.html',
        }),
        new ModuleFederationPlugin({
            remotes: {
                header: 'header@http://localhost:3001/remoteEntry.js',
            }
        })
    ],
    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
    },
    module: {
        rules: [
            {
                test: /\.(ts|tsx)?$/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-typescript", "@babel/preset-react"]
                    }
                }],
                exclude: /[\\/]node_modules[\\/]/
            }
        ]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and lets create the react files&lt;/p&gt;

&lt;p&gt;&lt;em&gt;index.ts&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import('./Bootstrap')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;bootstrap.tsx&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Host from './host';

ReactDOM.render(&amp;lt;Host /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are importing the header and wrapping it in react suspense because we are lazy loading the header and it will show an indicator till all the children load.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;host.tsx&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as React from 'react';
const Header = React.lazy(() =&amp;gt; import('header/header'));

const Host = () =&amp;gt; (
    &amp;lt;&amp;gt;
        &amp;lt;React.Suspense fallback="Loading..."&amp;gt;
            &amp;lt;div&amp;gt;
                &amp;lt;Header /&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/React.Suspense&amp;gt;
    &amp;lt;/&amp;gt;
);

export default Host;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here, we need the type definition for the header because the actual header is in another project which we are fetching via URL.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;types.d.ts&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;declare module 'header/header' {
    export default Object
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now at this point, if you run npm serve in the host folder, it will just start running and would suggest you run header app before starting this, or else it would be just blank&lt;/p&gt;

&lt;h2&gt;
  
  
  Monorepo - Setting up lerna
&lt;/h2&gt;

&lt;p&gt;Setting up Lerna is just an optional step, which has nothing to do with micro front-end architecture. Mono-repo just helps us to run/serve all projects at once without going into each folder in our local system. So you can skip this section if you don't want to include everything in a single repo.&lt;/p&gt;

&lt;p&gt;copy the below file to your root folder(outside of your package folder) and run an npm install.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;package.json&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "root",
  "private": true,
  "scripts": {
    "serve": "lerna run --parallel serve",
    "kill-ports": "kill-port --port 3000,3001,3002,3003,3004,3005,3006"
  },
  "devDependencies": {
    "kill-port": "^1.6.1",
    "lerna": "^4.0.0"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and create the lerna config file.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;lerna.json&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "packages": [
        "packages/*"
    ],
    "version": "0.0.0"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's enough! Now, if you run an npm serve in the root folder Lerna will start launching each app in parallel.&lt;/p&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/blessonabraham/micro-frontend-react"&gt;https://github.com/blessonabraham/micro-frontend-react&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>microservices</category>
      <category>webpack</category>
      <category>tailwindcss</category>
    </item>
  </channel>
</rss>
