<?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: Rahul Ranjan</title>
    <description>The latest articles on DEV Community by Rahul Ranjan (@rahulranjanme).</description>
    <link>https://dev.to/rahulranjanme</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%2F904776%2F720dcf98-0432-4d16-90c0-51436216022a.png</url>
      <title>DEV Community: Rahul Ranjan</title>
      <link>https://dev.to/rahulranjanme</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rahulranjanme"/>
    <language>en</language>
    <item>
      <title>Bootstrap your micro front-end in 5 minutes</title>
      <dc:creator>Rahul Ranjan</dc:creator>
      <pubDate>Sun, 07 Aug 2022 12:33:00 +0000</pubDate>
      <link>https://dev.to/rahulranjanme/bootstrap-your-micro-front-end-in-5-minutes-f78</link>
      <guid>https://dev.to/rahulranjanme/bootstrap-your-micro-front-end-in-5-minutes-f78</guid>
      <description>&lt;h2&gt;
  
  
  Create MFA App
&lt;/h2&gt;

&lt;p&gt;Bootstrap your micro front-end (MFE) using the starter App. The starter app comes with a shared micro-front end and an example of navigation.&lt;/p&gt;

&lt;p&gt;The starter app is not opinionated and is the minimum scaffolding to run an application using the Webpack module federation approach.&lt;/p&gt;

&lt;p&gt;It will create and initialize your micro front-end startup application into the specified folders, user-ready and available for further customization.&lt;/p&gt;

&lt;p&gt;There are two main options on the CLI, which let the software about the type of application you want to create.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Bootstrap MFE platform. Includes 
    1. Shell App 
    2. Shared components app 
    3. MFE app

&amp;gt; Add new MFE to platform
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Table of contents &lt;a id="top"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Running the CLI&lt;/li&gt;
&lt;li&gt;Available scripts&lt;/li&gt;
&lt;li&gt;Settings&lt;/li&gt;
&lt;li&gt;Data sharing between micro-frontends(MFEs) and shell app&lt;/li&gt;
&lt;li&gt;Exposing widgets components&lt;/li&gt;
&lt;li&gt;Useful features&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Bootstrap MFE platform
&lt;/h4&gt;

&lt;p&gt;This option will create three separate applications, as described above, and integrate them into the shell application. It can be used during the creation of a new project.&lt;/p&gt;

&lt;h4&gt;
  
  
  Add new MFE to the platform
&lt;/h4&gt;

&lt;p&gt;This option will build a new micro-front-end application that can be added to a shell already generated in the previous step.&lt;/p&gt;

&lt;h4&gt;
  
  
  Template source repos:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Shell container: &lt;a href="https://github.com/rahul-ranjan-me/mfe-shell-app"&gt;git@github.com:rahul-ranjan-me/mfe-shell-app.git&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Shared components: &lt;a href="https://github.com/rahul-ranjan-me/mfe-shared-components"&gt;git@github.com:rahul-ranjan-me/mfe-shared-components.git&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Micro front-end application: &lt;a href="https://github.com/rahul-ranjan-me/mfe-app"&gt;git@github.com:rahul-ranjan-me/mfe-app.git&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Go to top&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a id="runningCli"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the CLI (Command line interface)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; npx create-mfa-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Executing the command will create three directories (shell-container, shared-components, micro-frontend-app) in the 'workspace' folder (the names would be based on your entries). It will generate the initial project structure and install the transitive dependencies within those directories.&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-app
|-- node_modules
|-- public
  |-- index.html
|-- src
  |-- components
      |-- Header.jsx
      |-- header.scss
  |-- const
      |-- routes.jsx
  |-- core
      |-- App.jsx
      |-- AppRoutes.jsx
      |-- bootstrap.jsx
      |-- index.scss
      |-- setupApp.jsx
  |-- pages
      |-- home
          |-- index.jsx
  |-- appRoutesComponentConfig.js
  |-- exposedComponentsList.js
  |-- index.js
|-- .babelrc
|-- .browserlistrc
|-- .eslintrc.js
|-- .gitignore
|-- package-lock.json
|-- package.json
|-- webpack.config.js

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

&lt;/div&gt;



&lt;p&gt;No complicated configuration or folder structure, only the files you need to construct your application. Once the installation is complete, you may open your project files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; cd workspace/micro-frontend-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Go to top&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a id="scriptsAvailable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Available scripts
&lt;/h2&gt;

&lt;p&gt;Within the newly created project, you can execute a few embedded commands:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;npm start&lt;/code&gt; or &lt;code&gt;yarn start&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Runs the app in development mode&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shell container: &lt;a href="http://localhost:6001"&gt;http://localhost:6001&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Micro front-end app: &lt;a href="http://localhost:6003"&gt;http://localhost:6003&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;npm lint&lt;/code&gt; or &lt;code&gt;yarn lint&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Run the eslinting to static type checking&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;npm build&lt;/code&gt; or &lt;code&gt;yarn build&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Build the application for production into the 'dist' folder. It bundles React in production mode and optimizes the build for the best performance.&lt;/p&gt;

&lt;p&gt;The build is minified, and the filenames include the hashes which ensure files are not cached.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Go to top&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a id="scriptsAvailable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Settings
&lt;/h2&gt;

&lt;p&gt;Consider updating the following configuration in accordance with your application requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) Free Port&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A free port is necessary to execute the newly added micro front-end. You can specify this when adding a new microfront-end application through the Command Line Interface (CLI). In the event that you want to modify the port, you can configure the new port by specifying in the package.json file of the respective micro-front-end application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) Application routes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{micro front-end app}/src/const/routes.jsx&lt;/code&gt; is the configuration file for sharing micro front-end application routes with the shell to register them as platform routes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Adding a new route&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add a new entry to &lt;code&gt;defaultRoutes&lt;/code&gt; array in &lt;code&gt;{micro front-end app}/src/const/routes.jsx&lt;/code&gt; like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ path: "payments/about", element: &amp;lt;About /&amp;gt; },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3) Config service&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The shell-container application makes an XHR call to retrieve information on the micro-front-end applications to be recorded, such as remoteName and the URL.&lt;/p&gt;

&lt;p&gt;Micro front-end addition/deletion can be done through the configuration service. This doesn't require shell container or any micro front-end app re-deployment.&lt;/p&gt;

&lt;p&gt;You can refer sample configuration service response in &lt;code&gt;{shell-container}/public/config.json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To display the file is served via the public folder of the shell-container application. You can create a separate endpoint API for the same and update the URL in line no. 12 of &lt;code&gt;{shell-container}/src/core/setupApp.jsx&lt;/code&gt; file.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Go to top&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a id="dataSharing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Data sharing
&lt;/h2&gt;

&lt;p&gt;The Scaffold application uses the React Context API to share data across micro front-end and shell-container applications.&lt;/p&gt;

&lt;p&gt;The context is common and is created through the shared component of the micro front-end. You can refer &lt;code&gt;{shared-component}/src/components/contextProvider.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Any new data can be added to the context and shared across the platform by adding in the shared component global context file.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Go to top&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a id="widgetComponents"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Exposing widgets components
&lt;/h2&gt;

&lt;p&gt;Sometimes, instead of exposing the whole app as a micro front-end, you also want to expose a particular widget/component that can be built and exposed by one micro front-end app and re-used by other micro front-ends easily.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exposing a widget as a module federated component
&lt;/h3&gt;

&lt;p&gt;Create your component as a React component. To expose this component, we just need to add this component information in two files on the respective micro front-end application.&lt;/p&gt;

&lt;p&gt;1) &lt;code&gt;{micro front-end app}/src/manifest.js&lt;/code&gt; &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Add an entry just like ` "./header": "./src/components/Header",` component to `exposedComponents` object is added on the line no. 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Consuming the component
&lt;/h3&gt;

&lt;p&gt;Now that we have exposed a component from our micro front-end app, we want to consume the same in other micro front-end apps.&lt;/p&gt;

&lt;p&gt;To consume a module federated widget, we have created a special component &lt;code&gt;MFAComponentLoader&lt;/code&gt; that you can export from the shared component micro front-end like below.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { MFAComponentLoader } from "shared/Components";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and consume the exposed component anywhere in your application by calling the component below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;MFAComponentLoader componentName="./header" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is an optional &lt;code&gt;moduleName&lt;/code&gt; props you can pass, if you don't want dynamic discovery of the micro front-end that is exposing the target component.&lt;br&gt;
Module name is the remote name of the micro front-app that is exposing the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;MFAComponentLoader componentName="./header" moduleName="micro_frontend_app" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An example of the same is created for you and placed under &lt;code&gt;{shell-container app}/src/pages/Home.jsx&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Go to top&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a id="usefulFeatures"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Useful features
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Less to research&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Create-mfa-app CLI will come with abilities to help you start on day 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using opensource, no lock in&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Under the hood, we use Webpack, Babel, ESlint, and other amazing projects to fuel your application. This will allow you to expand and customize the setup according to your project needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shared component micro-frontend&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The scaffolding is delivered with a micro front-end shared component (MFE). The MFE shared component lets you consume reusable components from a single source through different micro front-ends. So when you need to update a component, all you need to do is update and deploy shared micro-front-end components. Other micro front-end would always be guaranteed to get the latest version in contrast to consuming as a node package.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data sharing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Common mechanisms to share data between micro front-ends and shell container app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Testing Support&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ESLint pre-configured with the generated application.&lt;/p&gt;

&lt;h2&gt;
  
  
  ToDos
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Add Jest, Cypress, ESLint as an option while configuring the project&lt;/li&gt;
&lt;li&gt;Consuming shared components URL in apps through the configuration service&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Credit
&lt;/h2&gt;

&lt;p&gt;Zack Jackson and the team for coming up with Webpack 5 module federation solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contact me
&lt;/h2&gt;

&lt;p&gt;I would love to get your feedback. Small or big any feedback is welcome. Please drop me a note to me on my email id: &lt;a href="mailto:rahul.smile@gmail.com"&gt;rahul.smile@gmail.com&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Thank you
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Rahul Ranjan&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>microfrontend</category>
      <category>webpack</category>
      <category>javascript</category>
      <category>modulefederation</category>
    </item>
  </channel>
</rss>
