<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gabriele Milan on Medium]]></title>
        <description><![CDATA[Stories by Gabriele Milan on Medium]]></description>
        <link>https://medium.com/@gabriele.milan?source=rss-f99b29b421d2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*B2Teg9OeKF6dwvrSdnrJNA.jpeg</url>
            <title>Stories by Gabriele Milan on Medium</title>
            <link>https://medium.com/@gabriele.milan?source=rss-f99b29b421d2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 13:56:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gabriele.milan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Micro Frontends in Angular using Nx and Module Federation]]></title>
            <link>https://medium.com/@gabriele.milan/micro-frontends-in-angular-using-nx-and-module-federation-a30c5a2c1354?source=rss-f99b29b421d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/a30c5a2c1354</guid>
            <dc:creator><![CDATA[Gabriele Milan]]></dc:creator>
            <pubDate>Fri, 09 Jul 2021 14:47:39 GMT</pubDate>
            <atom:updated>2021-07-21T13:52:54.896Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3XFBFjXOrmbKI6XSyhvNMQ.png" /></figure><p>In this article, I’ll share the way to create Micro Frontends in Angular using Nx and webpack 5 module federation.</p><h3>Create workspace using Nx</h3><p>First of all, create Nx workspace, nx-micro-frontend is the name, feel free to change it.</p><pre>npx create-nx-workspace@latest nx-micro-frontend</pre><p>When the cli prompts you to choose the type of project, select Angular and the style engine that you prefer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UalmQpZYbB9ne52xt7a5LQ.png" /></figure><p>Set the name of the new application, I called it shell (this will be the main application, the host).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*uP_4rhBILI92HSft3vvlZA.png" /></figure><p>Then let’s create the first microfrontend (mfe), in my case will be “<strong><em>menu</em></strong>”. Because in this case we will have a mfe for the menu, and two mfe for the content.</p><p>Create of new application using nx:</p><pre>nx generate <a href="http://twitter.com/nrwl/angular">@nrwl/angular</a>:app menu</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*w7INTKjxvw2XjpC4wQlnZg.png" /></figure><p>After that, we will have two applications, <strong><em>shell </em></strong>and <strong><em>menu</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HRDrU3g0_gUDNE2AmhbK-A.png" /></figure><blockquote>In these projects, I’m using bootstrap to be the pages more user friendly</blockquote><h3>Setup Module Federation</h3><p>To setup module federation we will use the library built by Manfred Steyer (<a href="https://medium.com/@ManfredSteyer">https://medium.com/@ManfredSteyer</a>), <a href="http://twitter.com/angular"><strong><em>@angular</em></strong></a><strong><em>-architects/module-federation:</em></strong></p><pre>ng add <a href="http://twitter.com/angular">@angular</a>-architects/module-federation --project shell --port 4200</pre><p>This command adds module-federation lib and creates the webpack config file to setup remotes or hosts.</p><p>Then let’s setup the menu application using port 4201</p><pre>ng add <a href="http://twitter.com/angular">@angular</a>-architects/module-federation --project menu --port 4201</pre><p>That command change angular.json to allow the menu to be accessible to port 4201 and creates webpack.config.js for us.</p><p>Now let’s setup webpack config in shell application. We configure the menu mfe as remote application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TJHb8Q_RKZR-GlPldO2mMg.png" /></figure><p>Then setup the menu application as remote application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E-8TX7wM-VlKG_Crl1Vnvw.png" /></figure><p>For this demo I create two routers outlets, one for the menu and one for the contents:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wd2jHhg6wFX_VeuZwNGagg.png" /></figure><p>Now in the router configuration, you can setup the menu to be rendered in outlet named “menu”. This configuration allow to get the module from remote module setup before:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZbqNj7YNrcm33PDWWLXTPQ.png" /></figure><p>In menu application, create new menu in app.component.html with two menu items:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23GmqRR_oLqGIL0BDSJEjg.png" /></figure><p>Let’s start the projects using “run:all” script created by module federation plugin.</p><pre>npm run run:all</pre><p>All projects will start on related ports:</p><ul><li>4200 for shell</li><li>4201 for menu</li></ul><p>The result will be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BnOQ2aiNE7LN5Kpw1qHfgQ.png" /></figure><p>As you can see the menu has been downloaded from remote url (port 4201)!!! 💪</p><p>Now let’s create two other applications, “mtg-singles” and “mtg-boosters” (sorry I’m Magic the Gathering fans 😂 )</p><pre>nx generate <a href="http://twitter.com/nrwl/angular">@nrwl/angular</a>:app mtg-singles<br>nx generate <a href="http://twitter.com/nrwl/angular">@nrwl/angular</a>:app mtg-boosters</pre><p>And setup module federation as well.</p><pre>ng add <a href="http://twitter.com/angular">@angular</a>-architects/module-federation --project mtg-singles --port 4202<br>ng add <a href="http://twitter.com/angular">@angular</a>-architects/module-federation --project mtg-boosters --port 4203</pre><p>Change properly the webpack.config.json for “<strong>mtg-singles</strong>”:</p><pre>name: &#39;mtgSingles&#39;,<br>      filename: &#39;remoteEntry.js&#39;,<br>      exposes: {<br>        &#39;./Module&#39;: &#39;./apps/mtg-singles/src/app/app.module.ts&#39;,<br>      },</pre><p>and for “<strong>mtg-boosters</strong>”:</p><pre>name: &#39;mtgBoosters&#39;,<br>      filename: &#39;remoteEntry.js&#39;,<br>      exposes: {<br>        &#39;./Module&#39;: &#39;./apps/mtg-boosters/src/app/app.module.ts&#39;,<br>      },</pre><h3><strong>Communication between MFEs</strong></h3><p>To communicate between MFEs we can use different strategies, please read this beautiful article to chose the best for you:</p><p><a href="https://dev.to/luistak/cross-micro-frontends-communication-30m3">⚔️ Cross micro frontends communication 📦</a></p><p>In this demo I use <strong><em>windowed-observable </em></strong>strategies. So let’s add the npm packages:</p><pre>npm install windowed-observable</pre><p>The idea is to publish message when the user clicks on menu item, configure shell application to be subscribed to this type of message, so the shell navigates the user to proper route.</p><p>So let’s change the router in shell application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KTSKa-5nINS3xLCqD44hWg.png" /></figure><p>In app.component.html of menu app, when the user clicks on menu item we call a function that publish the route using windowed-observable library:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/695/1*KaIUESBy7HmbnBShgyZAbQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*S77cSHuZk5GMhlbQrUeSkg.png" /></figure><p>In the shell application we are subscribed to this type of message:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8r9YmHdyvFhNm7DAY0cncg.png" /></figure><p>This is the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1yPYRJz6ok-sAHZaz522Ww.gif" /></figure><p>Thanks for reading! 😊</p><p><a href="https://github.com/gabrielemilan/nx-micro-frontend">gabrielemilan/nx-micro-frontend</a></p><figure><a href="https://www.buymeacoffee.com/gabrielemilan"><img alt="" src="https://cdn-images-1.medium.com/max/170/1*9vg3-OY14aZN1UpKwIxxZg.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a30c5a2c1354" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy Angular application to Netlify using GitHub and CI/CD]]></title>
            <link>https://medium.com/@gabriele.milan/deploy-angular-application-to-netlify-using-github-and-ci-cd-9faba2eefbe0?source=rss-f99b29b421d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/9faba2eefbe0</guid>
            <category><![CDATA[deployment]]></category>
            <category><![CDATA[netlify]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Gabriele Milan]]></dc:creator>
            <pubDate>Fri, 26 Jun 2020 14:21:22 GMT</pubDate>
            <atom:updated>2020-06-29T07:18:48.090Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_tcsdVM6ZVF83196.jpg" /></figure><p>Netlify is a very cool platform to build, release and host modern web projects.</p><p>In this article I want to share my experience on delivering angular web application to Netlify using GitHub repository.</p><p>First of all, we need a new repository on GitHub, so let’s create one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9eVW9Xy2BirP-YrVKhV-xw.png" /></figure><p>After that, we need an Angular application, so let’s create it using Angular CLI:</p><pre>ng new netlify-demo</pre><p>and push it to repository</p><pre>git remote add origin <a href="https://github.com/gabrielemilan/netlify-demo.git">https://github.com/gabrielemilan/netlify-demo.git</a><br>git push -u origin master</pre><p>Then if you reload github page, you will see the code in the repository.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1oSlhXsbUlr3LGx3mIAjAg.png" /></figure><p>I changed the app-component, just to avoid to publish default page of Angular CLI.</p><p>Now let’s go to Netlify site, register or login, and create “New site from git”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8QYzC4TG887-UHRry5vyyA.png" /></figure><p>Select GitHub</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eWJehJwnCisCRDHnKVTJ4w.png" /></figure><p>Now you have to authorize Netlify to access to your repositories, then pick proper one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sHOR9oRmZtclLbNXk7DYbA.png" /></figure><p>Netlify ask us the build command and the direcotory to publish. We use default angular cli build in production mode. So set “build command” and “publish directory” as follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MFijb_VR7kXCN2UC1O9_-A.png" /></figure><p>Then click “Deploy site”.</p><p>The build process will take a few minutes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uuNDe_KACkAOQ9CpiY-HWw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q5erD0e0RSY_5WDq9sWZgg.png" /></figure><p>Meanwhile you can check logs of building process clicking on building deploys list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Skd1WzXCsBXQH19VIaqbWw.png" /></figure><p>When the build is done, Netlify publishes our “Publish directory” (dist/netlify-demo). Then you can click on autogenerated link to see angular application up and running</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_2Rgiw6q8ROU-nn_FKiRlA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X8aQagG9OzF2B5yDAlMYRQ.png" /></figure><p>And voilà! That’s it!!! 😃</p><p>The Continuous Deployment is enabled by default, so when you push changes to master, a new build is taken over by Netlify:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_h-8eXdFw_dNIsuMc6QJfw.png" /></figure><p>Now let’s wait about 1 minute (the first time take longer, the next ones are faster because of cache), and refresh the site:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fCfzMZzafgBlMo4PUgIWFw.png" /></figure><p>At the end of the day, Netlify allows to deploy sites very very quickly. It offers also the possibility to set custom domain and https for FREE!</p><h4>URL rewrite</h4><p>Now let’s create two routes: products and orders:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9395dc87a4587faf3851a2e63ee553b4/href">https://medium.com/media/9395dc87a4587faf3851a2e63ee553b4/href</a></iframe><p>The routing will work as expected, but when we hit refresh we will get 404! 😢</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*250ijhA3T-sK0lsvD6Db2g.gif" /></figure><p>To fix this problem we have to put inside the root a file named <strong>_redirect </strong>as<strong> </strong>specified in documentation</p><p><a href="https://docs.netlify.com/routing/redirects/">Redirects and rewrites</a></p><p>So let’s create a new file under <strong>src</strong> folder and put this content on it:</p><pre>/* /index.html 200</pre><p>so all routes will be redirect to index.html and will allow angular to manage the routing.</p><p>To publish this file, we have to modify angular.json to consider the new file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iSI6xLbgsAnm7KCDSAMHvw.png" /></figure><p>Now when we build the app we will have this new file on root folder, so let’s commit and push the code to GitHub and wait few minutes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*HvWDs9escRjtgUdc2UrduA.gif" /></figure><p>And now also the url rewrite is working properly. 😁</p><p>Thanks for reading! 😊</p><p><a href="https://github.com/gabrielemilan/netlify-demo">gabrielemilan/netlify-demo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9faba2eefbe0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Publish same Docker image with Angular application to any environments]]></title>
            <link>https://medium.com/@gabriele.milan/publish-same-docker-image-with-angular-application-to-any-environments-edbfc454c6bb?source=rss-f99b29b421d2------2</link>
            <guid isPermaLink="false">https://medium.com/p/edbfc454c6bb</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[nginx]]></category>
            <dc:creator><![CDATA[Gabriele Milan]]></dc:creator>
            <pubDate>Thu, 25 Jun 2020 15:41:57 GMT</pubDate>
            <atom:updated>2020-06-25T15:41:57.326Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/785/0*f7EKk83KpzDaZdvi" /></figure><p>In this story I would share how to deliver same docker image with angular application inside to any environments.</p><p>First of all, we have to create a new application or get an existing one.</p><p>By default angular use environment.ts to store configurations for different environment, such as url of backend api. But if we use this approach we have to do different build for each environment (like testing, staging, prod, etc). To avoid multiple build we can use the approach explained in this article.</p><h4><strong>STEP 1 — Environment variables</strong></h4><p>As mention before, we can’t use environment.ts to store different values per environments, so let’s create a new file under assets folder called config.json (you can call it whatever you want, but must be a json):</p><pre>{<br>  &quot;api&quot;: &quot;https://local-api.com&quot;<br>}</pre><p>Then angular have to get this configuration, to do that I used ReplaySubject in a service. So create new service called config.service.ts and a new model for config property:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1c528b4d7351cb8b7a08e4fc6237bef/href">https://medium.com/media/e1c528b4d7351cb8b7a08e4fc6237bef/href</a></iframe><p>Now let’s call the loadConfig() method on app component and show the model on view:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2083578213a39d7678fcbf048298a85f/href">https://medium.com/media/2083578213a39d7678fcbf048298a85f/href</a></iframe><p>Now if we run the application we will see the api url on page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*ltt-k_JMtbF7eBHIoDLyhw.png" /><figcaption>app component</figcaption></figure><h4>STEP 2 — Create multiple configurations</h4><p>Now we have to create multiple configurations, one for each environment that you want to manage. For example I created under assets a folder named <strong>_environments</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*GRObJwDgS6fXEKdfdGv73A.png" /></figure><p>config.json files must have the proper configuration of corresponding environment.</p><p>Example for <strong>Staging</strong>:</p><pre>{<br>  &quot;api&quot;: &quot;https://staging-api.com&quot;<br>}</pre><p>Example for <strong>Production</strong>:</p><pre>{<br>  &quot;api&quot;: &quot;https://production-api.com&quot;<br>}</pre><h4><strong>STEP 3 — Dockerfile</strong></h4><p>First of all we have to create a new bash file that replace the config file under assets with proper one, based on environment variable (called ENVIRONMENT).</p><p>So create new file called <em>entrypoint.sh</em></p><pre>cp /usr/share/nginx/html/assets/_environments/<em>${ENVIRONMENT}</em>/config.json /usr/share/nginx/html/assets/</pre><pre>/usr/sbin/nginx -g &quot;daemon off;&quot;</pre><blockquote>If you are running on windows machine, pay attention of format of file on bottom right of vscode editor:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GfJKpu-PLIeIaFDjzw6-9g.png" /></figure><p>After that, we can create new <em>Dockerfile</em> file on the root as follow:</p><pre><em>### STAGE 1: Build ###<br>FROM</em> node:lts-stretch-slim <em>as</em> builder<br><em>COPY</em> package.json package-lock.json ./</pre><pre><em>## Storing node modules on a separate layer will prevent unnecessary npm installs at each build<br>RUN</em> npm ci &amp;&amp; mkdir /app &amp;&amp; cp -R ./node_modules ./app<br><em>WORKDIR</em> /app<br><em>COPY</em> . .</pre><pre><em>## Build the angular app in production mode and store the artifacts in dist folder<br>RUN</em> $(npm bin)/ng build --prod</pre><pre><em>### STAGE 2: Setup ###<br>FROM</em> nginx:stable</pre><pre><em>## Copy our default nginx config<br>COPY</em> nginx.conf /etc/nginx/conf.d/default.conf</pre><pre><em>## Remove default nginx website<br>RUN</em> rm -rf /usr/share/nginx/html/*</pre><pre><em>## From &#39;builder&#39; stage copy over the artifacts in dist folder to default nginx public folder<br>COPY</em> --from=builder /app/dist/angular-docker /usr/share/nginx/html</pre><pre><em>COPY</em> [&quot;entrypoint.sh&quot;, &quot;/entrypoint.sh&quot;]<br><em>CMD</em> [&quot;sh&quot;, &quot;/entrypoint.sh&quot;]</pre><p>Let me explain that lines:</p><ol><li><strong>STAGE 1</strong>: get image of node and name it as builder;</li><li>Copy package.json package-lock.json on the root of docker image;</li><li>Run npm ci to install dependencies using package-lock.json file;</li><li>Copy all source files to app folder inside docker;</li><li>Run angular command to build the application in production mode;</li><li><strong>STAGE 2</strong>: get image from dockerhub nginx:stable;</li><li>Copy nginx configuration in default configuration inside nginx server folder;</li><li>Copy from previous image (alias builder) the compiled files and put it in root folder of nginx server;</li><li>Copy entrypoint.sh to root folder inside docker;</li><li>Tell docker to run entrypoint.sh during startup.</li></ol><h4>STEP 4 — Build the image</h4><p>In terminal run this command:</p><pre>docker build -t angular-docker .</pre><p>to build the image and name it as angular-docker.</p><p>Finally, you can run this command to test the application:</p><pre>docker run -e ENVIRONMENT=Staging -p 5555:80 -it angular-docker:latest</pre><p>and if you now go to <a href="http://localhost:5555/">http://localhost:5555</a> you will see the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*Pd2jJTDEVf18sFjw-lNfdA.png" /></figure><p>Now if try another command:</p><pre>docker run -e ENVIRONMENT=Production -p 5556:80 -it angular-docker:latest</pre><p>you will see correct result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*HMiZ-TZh1WaG8ddkFtn2lA.png" /></figure><p>So now you are able to deploy your docker image in any environment, you just have to set the environment variable <strong>ENVIRONMENT </strong>and the docker image take care of replacing the proper file of configuration.</p><p>If you want to check this example, this is the link of repository:</p><p><a href="https://github.com/gabrielemilan/angular-docker">gabrielemilan/angular-docker</a></p><p>Thanks for reading! 😃</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=edbfc454c6bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>