The current Angular 11 build seems to lack in producing sufficient Lazy chunk files

Currently, I am working on implementing lazy loading for modules from different libraries in my project. This involves utilizing two libraries located in the node_modules directory, which are then lazily loaded by the main application. Below is a snippet of my app-routing-module:

const routes: Routes = [
  {
    path: 'admin/b',
    loadChildren: () => import('lib2-web').then(m => m.BModule)
  }, 
  // Other paths and lazy loading configurations
  ];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {
}

Upon building the project, the output reveals certain chunk files:

Initial Chunk Files | Names         |      Size
// Initial chunk file information

Lazy Chunk Files    | Names         |      Size
// Lazy chunk file information

However, I have encountered an issue where instead of having individual lazy chunk files for each module, only a few files are generated. This situation arises when importing modules from one library into another library or the main application.

In order to optimize this setup, one potential solution could involve importing utility modules directly using a more specific syntax, such as

import {DataService} from 'lib1-web/utility'
. By doing so, we can potentially streamline the lazy loading process and ensure that utility modules remain in the main bundle while other modules are lazily loaded.

If further refinement is necessary to achieve the desired outcome of having distinct lazy chunk files for each module, additional adjustments may need to be made within the codebase to segregate functionalities effectively.

Answer №1

Lazy loading an Angular library can be achieved by encapsulating it within a module:

An efficient approach is to create minimal wrapper modules locally within the consumer application codebase. These wrapper modules can import the desired library module using

import { SomeLibModule } from '@my-org/some-lib'
, and then include this module in the @NgModule decorators imports array.

https://i.sstatic.net/NQHkc.png

The final step: Once the wrapper module is prepared, it can be referenced in the route using a standard relative path since the wrapped module is part of the application project itself.

https://i.sstatic.net/G64Nb.png

If you are using a newer version of Angular, the setup would look like this:

const routes: Routes = [
  {
    path: 'admin/b',
    loadChildren: () => import('./some-lib-wrapper-module').then(m => m.SomeLibWrapperModule)
  }
  ];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {
}

For further details, refer to this article and this one.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Getting Session from Next-Auth in API Route: A Step-by-Step Guide

When printing my session from Next Auth in a component like this, I can easily see all of its data. const session = useSession(); // ... <p>{JSON.stringify(session)}</p> I am facing an issue where I need to access the content of the session i ...

I am searching for the RowEnter and RowLeave events in a kendo-grid for Angular 2. Where can I find them

When using an ODATA bound Kendo UI Angular 2 table, I am facing the challenge of needing to save the data that a user edits inline on a per row basis instead of per cell. If only there were RowEnter and RowLeave events available for me to achieve this. Do ...

Whenever the return condition is false, make sure to subscribe to the Angular CanActivate Guard

In my UserAccessGuard class, I have a method that captures the current path and compares it to the user's available paths. However, I am facing asynchronous problems because the condition inside the subscribe block causes my Hasaccess variable to rema ...

The specified property is not recognized by the type in TypeScript

I have set up a basic form with validation in Ionic 2. The form functioned properly when I used 'ionic serve' but encountered issues when running 'ionic run'. My suspicion is that the problem lies within my TypeScript code, specifically ...

Navigating an array using ngFor and encountering an error message saying, "Identifier not specified"

While using ngFor to iterate through an array, I encountered the following error message: "Identifier 'expenseitem' is not defined. The component declaration, template variable declarations, and element references do not contain such a memb ...

How can the value of a number in Angular be changed without altering its original value?

Imagine having the initial number 100. If I enter 50 in another input, it should add 50 to 100. However, if I then change the value from 50 to 80, the total should be 180 and not 230. The goal is always to add numbers to the original sum, not the new valu ...

Obtaining child elements of the DOM within an Angular Component that has already been instantiated: A step-by-step guide

Imagine I have a component called <app-parent></app-parent>. The corresponding file is named parent.component.ts. Within app.component.ts, I create an instance of it with the following child element: <app-parent> <h1>Hello</h1& ...

Issue: ChildrenOutletContexts provider not found

I am encountering difficulties using angular/material with Angular 5. Despite following a basic Tutorial, the webpage goes blank whenever I include any material tags in the component HTML. When running ng serve with Angular CLI, no issues are reported. D ...

Is it normal for the Array of Observables to be empty upon initial page load, only to contain content later

Currently, I am working on integrating ngx-infinite-scroll functionality. My goal is to extract the initial 5 posts from my "posts" array and populate them into the "shownPosts" array at the beginning. Subsequently, as the user continues scrolling down, I ...

Error message: "Issue occurs when sending keys to protractor element's text value and

Having trouble running this code in Protractor as I keep encountering errors, and I'm unable to retrieve the value of the anpr_box_input text. Error message: ManagedPromise::871 {[[PromiseStatus]]: "pending"} failed - should have a valid license ...

Error message: The value of ngIf has been altered after it has been checked

I am encountering the ngIf value changed error in my code and I'm unsure of the correct solution. Below is the HTML snippet causing the issue: <div *ngIf="currentVlaue"> <div class="section-body"> <div cla ...

Setting the date of the NgbDatePicker through code

I have implemented two NgbDatePicker components in my project: input( type="text", ngbDatepicker, #d="ngbDatepicker", [readonly]="true", formControlName='startDate', (dateSelect)='setNew ...

Creating an Angular interface that includes static values

How can we define an interface in Angular that includes constant values? For example, creating a 'State' interface that lists all the states in Australia. One way to do this is by defining the State interface like so: interface State { name: ...

AGM MAP | Placing marker within a custom polygon created on the map

Query Description I am utilizing the AGM_MAP library for an Angular website, which includes a map where users can select an address for their orders. Current Issue The problem I am facing is that when I add a polygon to the map, the marker cannot be p ...

Ensuring the presence of TypeScript variables

Take a look at this code snippet: let str: string | null; function print(msg: string) { console.log(msg); } print(str); When running this code, the typescript compiler correctly identifies the error, stating that Argument of type 'string | nu ...

Encountering the issue of receiving "undefined" in node.js after submitting data from an Angular form

I am facing an issue where I am getting 'undefined' in the backend (node.js) when I submit data from angular forms, even though I have used body-parser to parse the incoming data. server.js const express= require("express"); const app= ...

Prevent updating components when modifying state variables

Introduction I've developed a React component that consists of two nested components. One is a chart (created with react-charts) and the other is a basic input field. Initially, I have set the input field to be hidden, but it becomes visible when the ...

Unable to Anticipate User Input in Angular Using Scanner or Keyboard

Currently grappling with Angular 6 and facing an issue. I have a text box and a submit button, with a requirement for functionality to allow input through either manual keyboard typing or a Barcode scanner. The desired behavior is for the submit button to ...

Issues with date clicking on FullCalendar in Angular

I'm currently using version 4.4.0 of the FullCalendar plugin, but I am facing an issue where the dayClick event is not being triggered in my code. Below is a snippet of my code: calendar.component.html <full-calendar defaultView="dayGridMonth ...

Heroku: deployment unsuccessful

Currently, I am encountering a problem with Heroku: my build is failing without any explanation provided. Previously, I had no issues deploying. Even on my local machine, deployment was smooth. However, on my Heroku application, the build process halts a ...