<?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 Benjamin Lizarraga on Medium]]></title>
        <description><![CDATA[Stories by Benjamin Lizarraga on Medium]]></description>
        <link>https://medium.com/@lizarraga.jsb?source=rss-6bbe4a921ce------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*tGkZhzXuMQRTwxbMD5pd_w.jpeg</url>
            <title>Stories by Benjamin Lizarraga on Medium</title>
            <link>https://medium.com/@lizarraga.jsb?source=rss-6bbe4a921ce------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 03:57:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lizarraga.jsb/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[Ionic one code base multiple flavors]]></title>
            <link>https://medium.com/@lizarraga.jsb/ionic-one-code-base-multiple-flavors-9001fdab9264?source=rss-6bbe4a921ce------2</link>
            <guid isPermaLink="false">https://medium.com/p/9001fdab9264</guid>
            <category><![CDATA[cordova]]></category>
            <category><![CDATA[ionic]]></category>
            <category><![CDATA[codebase]]></category>
            <dc:creator><![CDATA[Benjamin Lizarraga]]></dc:creator>
            <pubDate>Mon, 11 May 2020 16:10:22 GMT</pubDate>
            <atom:updated>2022-05-03T17:02:38.055Z</atom:updated>
            <content:encoded><![CDATA[<p>I’d like to write about a challenge that I had to face a few weeks ago. Making a ionic app multi client. It was a very interesting challenge. So, let get started.</p><p>This is just an approach that I take, it always can be improve it. Feel free to put some comments about it.</p><h3>Initial situation</h3><p>So, to put us in context, I will talk about the initial situation of the project. There is the thing, we have two different repositories with the same code base, and each one has specific features per client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*2K0UDcw3RWUY4lcJozLYSA.png" /></figure><p><strong>Cons</strong></p><ul><li>if the code base change we have to made the same change in two different repositories. That means the maintenance cost is very high.</li><li>Testing the code base twice.</li><li>Difficult to track changes between each project.</li></ul><p>What will happen if the business needs to add a new client? We have to create a new repo, replicating all the code and make the specific changes for that client. In summary, the maintenance cost increment and we want to keep our maintenance cost as lower as possible.</p><p>With that in mind we need to move our project structure in something like the following image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*kRkEJgC5uOpqauv86bYeHw.png" /></figure><h3>Show me the code</h3><p>Let’s create a new ionic project to illustrate that structure and how it works, the challenge was using ionic cordova, so let&#39;s create a new blank ionic project:</p><pre>$: ionic start ionic-multiclient</pre><pre><strong>Pick a framework! 😁</strong></pre><pre>Please select the JavaScript framework to use for your new app. To bypass this prompt next time, supply a</pre><pre>value for the — type option.</pre><pre>? <strong>Framework:</strong> Angular</pre><pre><strong>Let’s pick the perfect starter template! 💪</strong></pre><pre>Starter templates are ready-to-go Ionic apps that come packed with everything you need to build your app.</pre><pre>To bypass this prompt next time, supply template, the second argument to ionic start.</pre><pre>? <strong>Starter template:</strong> blank</pre><pre>✔ Preparing directory ./ionic-multiclient — done!</pre><pre>✔ Downloading and extracting blank starter — done!</pre><pre>? <strong>Integrate your new app with Capacitor to target native iOS and Android?</strong> No</pre><pre>Installing dependencies may take several minutes.</pre><pre>──────────────────────────────────────────────────────────────────</pre><p>After the creation we will a structure like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*9lBVpnbdpR34fKK8VfUJdQ.png" /></figure><p>We want to maintain the same code base allowing deliver multiple binaries. In order to do it we need to adapt our project structure for in this case two applications, but could be more so we need to be flexible, our next steps are the following:</p><ol><li>Create a <strong>clients</strong> folder at the root of the project: The purpose of this folder is keep all assets and static resources for each client.</li><li>Generate for each client the respective resources (icons, splash and config.xml file)</li><li>Copy each client resources into its respective client folder under <strong>resources</strong> folder.</li><li>Copy each config.xml file into its respective client folder under its client folder.</li><li>Ignore <strong>/resources</strong> folder and /<strong>config.xml</strong> file at the root of the project, because we want to copy those dynamically by client, so we do not need it anymore.</li></ol><pre>/clients<br>- /cat<br>-- /config.xml<br>-- /resources/<br>---- /android/<br>---- /ios/<br>---- /icon.png<br>---- /splash.png<br>- /dog<br>-- /config.xml<br>-- /resources/<br>---- /android/<br>---- /ios/<br>---- /icon.png<br>---- /splash.png<br></pre><p>The final result is like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ra0mdwOY84Hc4o06Un6ugA.gif" /></figure><p>With this we have the initial structure, now we need to solve how to provide our clients assets to the ionic application at the build time, to make this real we will to create a js script to replace the resources for a particular client into the ionic project structure. There are the following steps:</p><ol><li>Create <strong>scripts</strong> folder at the root.</li><li>Create a js file called provide-client.js into the previous folder.</li><li>Write a script that copy the resources of a specific client into the original resources folder in order to build the application for that specific client.</li><li>Update package.json file with our script.</li></ol><p>Lets do it.</p><p>So, after create <strong>scripts/provide-client.js,</strong> we will install a npm package to help us with the copy task and stay focus on the important things.</p><pre>npm install --save-dev fs-extra</pre><p>And the script is the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07ebf9a9cd0e56886c7f2574b086c3d7/href">https://medium.com/media/07ebf9a9cd0e56886c7f2574b086c3d7/href</a></iframe><p>Let’s to shed some light about it:</p><p>line 1: importing <em>fs-extra</em> package. Not much to add here.</p><p>line 4–9: Well, we need a way to tell to the script ‘Hey I need the resources for <strong>this</strong> specific client’, so that way is passing a parameter, in this case we will use <em>— client clientName</em>, then we will search by that parameter to get its value.</p><p>line 10–17: Basically this method replace resources folder with the resources folder of a specific client, and print an output.</p><p>line 19–26: The same as above but over config.xml file.</p><p>line 28–33: This is the main method, this method call to <em>getClient</em> method, retrieve the client and use it on providers method.</p><p>line 35: Finally we call the main method and pass the context process as parameter.</p><p>After that, we need to update the package.json file with our new script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab3b0ccc4ca0f0eb9c5f511b37ab13fc/href">https://medium.com/media/ab3b0ccc4ca0f0eb9c5f511b37ab13fc/href</a></iframe><p>Copy lines 9 to 14 and paste in your package.json file. In summary scripts from lines 9 and 10 provide our application with a specific client, scripts from 11 and 12 provide the client and serve the application. Scripts from lines 123 and 14 provide a specific client and run the android application.</p><p>So, if we run <strong>npm run run:apk:cat</strong> in the command line, <strong>cat</strong> client will be provided and finally we will have an apk running:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*RV4eJt9MIw-uH2F2sZxx4g.gif" /><figcaption>cat apk running first time.</figcaption></figure><p>Awesome!, now if we run <strong>npm run run:apk:dog, dog</strong> client will be provided and running, and if we can see at the final of this video we have two different apps installed on our phone:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*9hgSlf5OXnsokIuHyhQ9SA.gif" /><figcaption>dog apk running first time.</figcaption></figure><p>So, what we have done at this point?</p><ul><li>Decrease the maintenance cost, unifying two similar code base project in only one repo.</li><li>Set up our ionic project to support multiple flavor with the same code base, changing the provided client according to need.</li></ul><blockquote>With this improvement, in the future, we can have a only one CI/CD pipeline to build and deliver to multiple clients.</blockquote><h3>Client Specific Features</h3><p>Well, what about client specific features because just switching static resources sounds beautiful but we need more power here.</p><p>Fortunately using Ionic with Angular, we can take advantage over Angular structure, so the thing is use client specific components with different internal functionality but with the same <strong>tag</strong> and creating an alias to import it in our module. The way to do that thing is using environments.</p><p>So, let’s create two new environment files one per client, under environments folder:</p><pre>environments<br>- environment.cat.ts<br>- environment.dog.ts</pre><p>what we are supposed to do with this files. All step are the following:</p><ul><li>Create <strong>environment.cat.ts</strong> and <strong>environment.dog.ts</strong> under environments folder.</li><li>Create specific Hello World component for each client, with the same tag, that could be <strong>app-hello</strong> for example.</li><li>In each client specific environment file, export its component putting the same alias in both files.</li><li>Importing that environment specific component into our module.</li><li>Using the component in <strong>app.compoment.html</strong> file.</li></ul><p>Let’s do that.</p><p>Under <strong>app</strong> folder, create components folder, and under it create a one folder per client. After that create a new component under each client folder, in this case hello-cat component and hello-dog component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*n8-iotx8-DLgCWSzI9bhkQ.png" /><figcaption>folder structure after creating client specific component folders</figcaption></figure><p>Just change each component with some different text.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*4XMG006DFPZFGjsyq0Yxmw.png" /><figcaption>hello-cat.component.html</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*qRSZhVX0B2qScaF08KPVoQ.png" /><figcaption>hello-dog.component.html</figcaption></figure><p>In <strong>environment.ts</strong> file we need to <strong>export</strong> a component as default, in this case I choose to export hello-dog.component as default.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ec2a34e6d9d6d2d8845112aad2a14a5/href">https://medium.com/media/4ec2a34e6d9d6d2d8845112aad2a14a5/href</a></iframe><p><strong>environment.dog.ts</strong> file will be the same as our default environment file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/310c46491ec30a8691136ee2bc84952d/href">https://medium.com/media/310c46491ec30a8691136ee2bc84952d/href</a></iframe><p>And here the last one, <strong>environment.cat.ts</strong> exporting its specific component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60e4e051beab965260c83f8d9c83c262/href">https://medium.com/media/60e4e051beab965260c83f8d9c83c262/href</a></iframe><p>The thing here is exporting those different components using the same alias.</p><p>After that we are able to import our client specific component in <strong>home.module.ts.</strong></p><p>The import is made it over <strong>environment.ts</strong>, that is because we will replace environment specific file in that file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7c15581f428ac8c25e9910308d2355f/href">https://medium.com/media/f7c15581f428ac8c25e9910308d2355f/href</a></iframe><p>And here we are using that component, on <strong>home.page.html</strong> file under <strong>app-hello</strong> tag.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c55814d6de6a70ecca21f6e33674ce49/href">https://medium.com/media/c55814d6de6a70ecca21f6e33674ce49/href</a></iframe><p>Now we need a way to tell it to ionic “Hey ionic please build the application using this specific environment”. We need something like the following:</p><pre>ionic cordova build android --client cat --configuration cat</pre><p>That last parameter is what we need.</p><p><em>— configuration </em>is used in <strong>angular.json</strong> file. “Hey Ionic we will using this specific configuration to build the application”. Angular Ionic use <strong>angular.json</strong> file to looking for the right configuration to make the build.</p><p>So, open <strong>angular.json</strong> file, under architect -&gt; build -&gt; configurations, we will add two news configurations that says “Hey, please replace this environment specific file into <strong>environment.ts</strong> file”. That we are doing in lines 69 to 84. But that is not enough, we are just telling what we need need, now Ionic needs to know how to reach those directives, So under <em>ionic-cordova-build -&gt; configurations, </em>we will to add two builds directives, look at lines 174 to 180.</p><p>Those lines says “Hey, when I pass <em>— configuration cat </em>to you. You need to look at configuration in <em>app:build:cat</em>, that is the path to our previous configuration that tell to replace that environment file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eec98150fa2b49caeb2723618e44f1d8/href">https://medium.com/media/eec98150fa2b49caeb2723618e44f1d8/href</a></iframe><p>So, after all of those changes, we are able to build each client specific applications with different static resources and specific functionality.</p><p>Adding the following scripts to <strong>package.json</strong> file we can run the applications</p><pre>&quot;run:apk:cat&quot;: &quot;npm run provide:client:cat &amp;&amp; ionic cordova run android --configuration cat&quot;,</pre><pre>&quot;run:apk:dog&quot;: &quot;npm run provide:client:dog &amp;&amp; ionic cordova run android --connfiguration dog&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*4fobQ9SMg7D-8pdIYR16Bg.png" /><figcaption>running cat application with its specific Hello Component</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/472/1*YTqeFJhYet-4kijTXNf4PA.png" /><figcaption>running dog application with its specific Hello Component</figcaption></figure><h3>Last Words</h3><p>This is my first post on Medium, I have write it just to share my experience and what approach I take to face out this challenge.</p><p>This example is just a base, could be extensible, for example using the environment file approach, create a module to import all client specific components and then import that module in our main module. We can also override routing files and another static resources if we need. Another useful thing is the possibility to create a only one DevOps pipeline to build and deploy all of our applications.</p><p>Thank you for read me.</p><p>Feel free to comment, make suggestions, ideas are more powerful when are shared.</p><p>Here is the repo.</p><p><a href="https://github.com/benja-lizarrraga/ionic-multiclient">GitHub - benja-lizarrraga/ionic-multiclient: This repository is made it for illustrate a multi client approach with ionic-cordova. This is just an approach.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9001fdab9264" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>