Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2660
Abstract
src/MicroFrontend.js"><code>MicroFrontend</code>.js</a> to handle chunks:</p>
<figure id="785e">
<div>
<div>
<iframe class="gist-iframe" src="/gist/JenniferFuBook/0cd7d34548c76df1837988692880b54c.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="905f">Lines 13 to 38 load small chunked js files. When all chunks are loaded, it invokes <code>this.renderMicroFrontend()</code> at line 31. This is how micro-frontends can preserve optimization.</p><p id="8239">Now we can see micro-frontend’s three chunked files are loaded:</p><figure id="85d6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1WA-rkNfEFwRRaeuuSi4UA.png"><figcaption></figcaption></figure><p id="67e8">They are embedded in the app container’s HTML files:</p><figure id="5861"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*j7Rbaz33A7Ejzt4wKO-DbA.png"><figcaption></figcaption></figure><h2 id="6db3">Hook version of MicroFrontend.js</h2><p id="c746">As a bonus, we convert MicroFrontend.js to use Hook.</p>
<figure id="eb39">
<div>
<div>
<iframe class="gist-iframe" src="/gist/JenniferFuBook/94bd3eaaa26fb8097f100164c6bc1c0c.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="f02c"><a href="https://gist.github.com/JenniferFuBook/0cd7d34548c76df1837988692880b54c#gistcomment-3350826">As natansevero pointed out</a>, for anybody that using this algorithm and do not want to load all micro front-end JavaScript on the <code><head></code> tag, you can replace the code from line 33 to <code>document.body.after(script)</code>. It works as expected :)</p><h1 id="e1b2">The Code Repositories</h1><p id="3798">There are two code repositories for this example:</p><ul><li><b>The app container:</b> The three-step converted application container is located <a href="https://github.com/JenniferFuBook/app-container">here</a>.</li></ul><div id="6ac2"><pre>git <span class="hljs-keyword">clone</span> <span class="hljs-title">--single-branch</span> --branch chunkOptimization https://github.com/JenniferFuBook/app-container.git
npm i
npm <span class="hljs-literal">start</span></pre></div><ul><li><b>The micro-frontend app:</b> The five-step converted micro-frontend application is located <a href="https://github.com/JenniferFuBook/micro-frontend">here</a>.</li></ul><div id="b70a"><pre>git
Options
<span class="hljs-keyword">clone</span> <span class="hljs-title">--single-branch</span> --branch chunkOptimization https://github.com/JenniferFuBook/micro-frontend.git
npm i
npm <span class="hljs-literal">start</span></pre></div><blockquote id="bc9f"><p><i>As matter of this improved version of MicroFrontend.js, you don’t have to use <code>react-app-rewired</code> in step 1, and step 2 can be completely skipped. 5 steps are reduced to 3.5.</i></p></blockquote><blockquote id="f14f"><p><i>This saving is captured in the </i>chunkOptimization <i>branch.</i></p></blockquote><h1 id="edb9">The Launch Command</h1><p id="f29c">Assume these two repositories are in your <code>home</code> directory. Set the following alias in your Bash profile:</p><div id="39af"><pre><span class="hljs-keyword">alias</span> runBoth='<span class="hljs-keyword">cd</span> ~<span class="hljs-string">/app-container</span>; concurrently <span class="hljs-string">"npm start --prefix ~/micro-frontend"</span> <span class="hljs-string">"npm start"</span>'</pre></div><p id="493c"><code>runBoth</code> uses <a href="http://micro%20front-end/"><code>concurren</code>tly</a> command to start both the app container and the micro-frontend app. For this branch of work, we retain optimization for micro-frontends.</p><h1 id="96a3">Conclusion</h1><p id="2abd">You don’t have to lose optimization for micro-frontends.</p><p id="913a">Part of this work is contributed by Jonathan Ma.</p><p id="5733">Thanks for reading. I hope this was helpful.</p><p id="ecec">This is a series about micro-frontend. The following is a list of other articles:</p><ul><li>“<a href="https://readmedium.com/5-steps-to-turn-a-random-react-application-into-a-micro-frontend-946718c147e7">5 Steps to Turn a Random React Application Into a Micro Front-End</a>”</li><li>“<a href="https://readmedium.com/3-steps-to-turn-a-random-react-application-into-a-micro-frontend-container-a80e33b6a066">3 Steps to Turn a Random React Application Into a Micro-Frontend Container</a>”</li><li>“<a href="https://readmedium.com/build-your-own-micro-frontend-ecosystem-a05128c74f99">Build Your Own Micro-Frontend Ecosystem</a>”</li><li>“<a href="https://readmedium.com/10-decision-points-for-micro-frontends-approach-4ebb4b59f40">10 Decision Points for a Micro-Frontend Approach</a>”</li><li>“<a href="https://readmedium.com/6-steps-to-create-a-multi-version-react-application-1c3e5b5df7e9">6 Steps to Create a Multi-Version React Application</a>”</li><li>“<a href="https://readmedium.com/micro-frontends-using-webpack-5-module-federation-3b97ffb22a0d">Micro Frontends Using Webpack 5 Module Federation</a>”</li></ul></article></body>