<?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: ArunachalamM</title>
    <description>The latest articles on DEV Community by ArunachalamM (@arunachalamm).</description>
    <link>https://dev.to/arunachalamm</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%2F342768%2F3383f172-6efc-41a8-b872-2f17c581fbd9.jpg</url>
      <title>DEV Community: ArunachalamM</title>
      <link>https://dev.to/arunachalamm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arunachalamm"/>
    <language>en</language>
    <item>
      <title>Run on-demand apps in angular projects in local development</title>
      <dc:creator>ArunachalamM</dc:creator>
      <pubDate>Sat, 26 Feb 2022 10:25:30 +0000</pubDate>
      <link>https://dev.to/arunachalamm/run-on-demand-apps-in-angular-projects-in-local-development-18ej</link>
      <guid>https://dev.to/arunachalamm/run-on-demand-apps-in-angular-projects-in-local-development-18ej</guid>
      <description>&lt;p&gt;I was working on a large-scale project that was developed in the Angular framework.  There are multiple library projects created (almost 100+) for every module.  These libraries are imported as lazy-loaded modules in route.module.ts.&lt;/p&gt;

&lt;p&gt;Below is a sample route module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const routes: Routes = [
  { path: 'login', component: LoginComponent },
  { path: 'home', component: HomeComponent },
  { path: 'applications/app1', loadChildren: () =&amp;gt; 
    import('./applications/app1/app.module').then(m =&amp;gt; m.AppModule)
  },
  { path: 'applications/app2', loadChildren: () =&amp;gt; 
    import('./applications/app2/app.module').then(m =&amp;gt; m.AppModule)
  },
  { path: 'applications/app3', loadChildren: () =&amp;gt; 
    import('./applications/app3/app.module').then(m =&amp;gt; m.AppModule)
  },

  ….
  ….

  { path: 'applications/app50', loadChildren: () =&amp;gt; 
    import('./applications/app1/app.module').then(m =&amp;gt; m.AppModule)
  }
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I run “ng serve’’, it took more time (almost it took 20-30 mins) to serve the application.&lt;/p&gt;

&lt;p&gt;As a developer, I am going to work on a few applications (mostly one or two apps at a time) during my development. Here serving the entire application is a tedious process and even doing the little code changes in the library project would take more time to compile and reflect the changes.&lt;/p&gt;

&lt;p&gt;To avoid this waiting time to serve, I have created a separate routing module for development and configuration (in angular.json). The dev routing module only contains the required apps/libraries that I am going to work on. By doing this we can reduce the considerable amount of time in serving the angular application&lt;/p&gt;

&lt;p&gt;Let’s see what the configurations I have created are:&lt;/p&gt;

&lt;h2&gt;
  
  
  Development version of Routing Module
&lt;/h2&gt;

&lt;p&gt;Create a new routing module (you could name it as your wish) in the same folder where you have route.module.ts. I named it route.dev.module.ts.&lt;/p&gt;

&lt;p&gt;Copy the required imports and routing paths from the main route module and paste them in the dev.routing.module.&lt;/p&gt;

&lt;p&gt;This file is the same as the main routing module but it contains only the required (on-demand) routing paths that I am going to work on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Development version of tsconfig.json
&lt;/h2&gt;

&lt;p&gt;Create a development version of tsconfig.json. I named this file tsconfig.dev.json.  This new file would extend the existing tsconfig.json. But here we add include properly that contains the required project folders to be compiled.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
   "extends": "./tsconfig.json",
   "include": [
      "src/main.ts",
      "src/polyfills.ts",
      "src/app/applications/app1",
      "src/app/applications/app2"
   ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: We need to add main.ts and polyfills.ts files to the include array. If we don’t add it, we will be getting the below error when you do ng serve.&lt;/p&gt;

&lt;p&gt;Error: ./src/main.ts&lt;br&gt;
Module build failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js):&lt;br&gt;
Error:  ......\src\main.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.&lt;br&gt;
at &lt;br&gt;
Error: ./src/polyfills.ts&lt;br&gt;
Module build failed (from ./node_modules/@ngtools/webpack/src/ivy/index.js):&lt;br&gt;
Error: ......\src\polyfills.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating a new configuration in angular.json
&lt;/h2&gt;

&lt;p&gt;Now we have to create a new configuration that will make use of the development version of both typescript configuration (tsconfig.dev.json) and routing module (route.dev.module).&lt;/p&gt;

&lt;p&gt;In angular.json add a new configuration under &lt;strong&gt;architect -&amp;gt; build -&amp;gt; configurations&lt;/strong&gt;. I named this configuration &lt;strong&gt;my-dev-apps&lt;/strong&gt;. Below is a sample configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"my-dev-apps": {
   "tsConfig": "src/tsconfig.dev.json",
   "fileReplacements": [
      {
         "replace": "src/environments/environment.ts",
         "with": "src/environments/environment.dev.ts"
      },
      {
         "replace": "src/app/route.module.ts",
         "with": "src/app/route.dev.module.ts"
      }
   ],
   "optimization": false,
   "sourceMap": true,
   "namedChunks": true,
   "aot": false,
   "extractLicenses": false,
   "vendorChunk": false,
   "buildOptimizer": false,
   "scripts": []
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main things to be noted in this configuration are &lt;strong&gt;tsConfig&lt;/strong&gt; and &lt;strong&gt;fileReplacements&lt;/strong&gt; properties. &lt;/p&gt;

&lt;p&gt;The value of the &lt;strong&gt;tsConfig&lt;/strong&gt; property is the path of &lt;strong&gt;tsconfig.dev.json&lt;/strong&gt;. &lt;br&gt;
In the &lt;strong&gt;fileReplacements&lt;/strong&gt; array, I replace route.module.ts with &lt;strong&gt;route.dev.module.ts&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then we have to add configuration under &lt;strong&gt;architect -&amp;gt; serve&lt;/strong&gt;. The new configuration will be used as browseTarget.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"dev-my-apps": {
   "browserTarget": "main:build:dev-my-apps"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we have to use this new configuration while serving the application. I created an npm script to use this configuration. Below is the sample npm script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"my-dev-apps": "ng serve -c my-dev-apps"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can call this script in your terminal as npm run my-dev-apps.&lt;/p&gt;

&lt;p&gt;After running this script, you can see the ng serve is finished quickly based on the apps that you included in the configuration. This will reduce a lot of waiting time to finish the serve command.&lt;/p&gt;

&lt;p&gt;We can change the required apps in the routing.dev.module and tsconfig.dev.json based on your development activities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;These configurations are only for local development.  Don't use this for production.&lt;/li&gt;
&lt;li&gt;These new configurations may not be required for smaller applications.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>angular</category>
      <category>angularrouting</category>
      <category>runondemandapps</category>
    </item>
  </channel>
</rss>
