DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Low-Code Development: Leverage low and no code to streamline your workflow so that you can focus on higher priorities.

DZone Security Research: Tell us your top security strategies in 2024, influence our research, and enter for a chance to win $!

Launch your software development career: Dive head first into the SDLC and learn how to build high-quality software and teams.

Open Source Migration Practices and Patterns: Explore key traits of migrating open-source software and its impact on software development.

Related

  • The Power of @ngrx/signalstore: A Deep Dive Into Task Management
  • React, Angular, and Vue.js: What’s the Technical Difference?
  • Building Angular Library and Publishing in npmjs Registry
  • The Most Popular Angular UI Libraries To Try in 2021

Trending

  • Linting Excellence: How Black, isort, and Ruff Elevate Python Code Quality
  • Open-Source Dapr for Spring Boot Developers
  • Explore the Complete Guide to Various Internet of Things (IoT) Protocols
  • Unlocking Potential With Mobile App Performance Testing
  1. DZone
  2. Coding
  3. Frameworks
  4. Multiple Entry Points for the NgxSimpleCharts Angular Library

Multiple Entry Points for the NgxSimpleCharts Angular Library

Angular Libraries created with 'ng generate library ngx-simple-charts' are a single piece of code. Learn how to make the Angular Compiler split the code of a Library into Modules.

By 
Sven Loesekann user avatar
Sven Loesekann
·
Jan. 10, 22 · Tutorial
Like (2)
Save
Tweet
Share
5.6K Views

Join the DZone community and get the full member experience.

Join For Free

Angular Libraries can grow to a significant size. An Angular Library that is created with 'ng generate library ngx-simple-charts' is a single piece of code that will be included in the first module that includes/uses it. That module probably will impact the first initial useful paint.

To solve that problem multiple entry points can be used. These entry points provide the feature to include only the code of the entry point in the module. Lazy loaded modules can include the code of other entry points. That makes it possible to spread out the code to the modules that use it and lowers the size of the modules to the necessary size.

The Ngx-Simple-Charts library is the example used in this article. The project that uses it is the AngularPortfolioMgr. The article about creating the first iteration of the ngx-simple-charts can be found here.

Simple-Charts With Multiple Entry Points

The library has a new structure:

 
ngx-simple-charts/projects/ngx-simple-charts
   /bar
   /line
   /src


  • The ‘bar’ directory has the entry point for the bar charts feature.
  • The ‘line’ directory has the entry point for the line charts feature.
  • The ‘src’ directory has the entry point for the common features, currently a placeholder service.

The package.json looks like this:

JSON
 
{
  "name": "ngx-simple-charts",
  "version": "13.1.0",
  "license": "Apache License Version 2.0",
  "peerDependencies": {
    "@angular/common": "^13.0.0",
    "@angular/core": "^13.0.0",
    "rxjs": "^7.4.0"
  },
  "dependencies": {
    "d3-array": "^3.0.0",
    "d3-axis": "^3.0.0",
    "d3-brush": "^3.0.0",
    "d3-color": "^3.0.0",
    "d3-format": "^3.0.0",
    "d3-interpolate": "^3.0.0",
    "d3-scale": "^4.0.0",
    "d3-selection": "^3.0.0",
    "d3-shape": "^3.0.0",
    "d3-time-format": "^4.0.0",
    "d3-transition": "^3.0.0",
    "tslib": "^2.3.1"
  }
}


The ‘peerDependencies’ define the required libraries of the application including the library. 

The ‘dependencies’ define the libraries the whole library uses. 

In the bar directory is the ng-package.json: 

JSON
 
{
  "$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
  "lib": {
    "entryFile": "./public-api.ts"
  }
}


The ng-package.json tells the compiler that this is an entry point and registers the public api of the entry point.

In the bar directory is the public-api.ts: 

TypeScript
 
export * from './src/lib/sc-bar-chart/sc-bar-chart.component';
export * from './src/lib/ngx-bar-charts.module';
export * from './src/lib/sc-bar-chart/model/chart-bars';

This file declares the public api of the entry point. In this case the component, the module and the input interface to the data. The Angular Cli build will include the needed parts of libraries in the dependencies in the entry point. 

Similar files can be found in the line directory of the library.

Using the Library in AngularPortfolioMgr Project

In this package.json is the Ngx-Simple-Charts library included in a project:

JSON
 
  "dependencies": {
    ...
    "@angular/router": "~13.0.0",
    "ngx-simple-charts": "^13.1.0",
    "material-design-icons": "3.0.1",
    ...
  },

That makes the library available in the project.

Using the Bar Entry Point

The bar entry point is imported in the portfolios.module.ts: 

TypeScript
 
import { NgxBarChartsModule } from 'ngx-simple-charts/bar';

@NgModule({
	declarations: [OverviewComponent, NewPortfolioComponent, 
                   AddSymbolComponent, PortfolioTableComponent, 
                   PortfolioChartsComponent, ProdConfigComponent, 
                   DevConfigComponent],
	imports: [
		...
		MatCheckboxModule,
		NgxBarChartsModule,
		PortfoliosRoutingModule
	],
	entryComponents: [NewPortfolioComponent],
	providers: [DevAppInfoService, ProdAppInfoService, 
                { provide: HTTP_INTERCEPTORS, 
                 useClass: TokenInterceptor, multi: true }],
})
export class PortfoliosModule { }

The import declares the ‘NgxBarChartsModule’ of the ‘ngx-simple-charts/bar’ entry point.

In the ‘@NgModule’ the ‘NgxBarChartsModule’ is imported in the ‘PortfoliosModule’.

The ‘<sc-bar-chart>’ component of the library is then used in the portfolio-charts component of the module.

Using the Line Entry Point

The line entry point is imported in the portfolio-detail.module.ts:

TypeScript
 
import { NgxLineChartsModule } from 'ngx-simple-charts/line';

@NgModule({
	declarations: [PortfolioComponent, SymbolComponent],
	imports: [
                ...
		BaseModule,
		NgxLineChartsModule,
		MatCheckboxModule,
		...
	],
})
export class PortfolioDetailModule { }


The import declares the ‘NgxLineChartsModule’ of the ‘ngx-simple-charts/line' entry point.

In the ‘@NgModule’ the ‘NgxLineChartsModule’ is imported in the ‘PortfolioDetailModule’.

The ‘<sc-line-chart>’ component of the library is then used in the symbol component of the module. 

Having a look at the result

The AngularPortfolioMgr project has these commands in the package.json:

JSON
 
 "scripts": {
    ...
    "build-stats": "ng build --localize 
        --configuration production --stats-json",
    "analyse": "webpack-bundle-analyzer dist/manager/de/stats.json",
    ...
}


With the command ‘npm run build-stats’ a build is run that creates a ‘stats.json’ file.

With the command ‘npm run analyze’ the ‘webpack-bundle-analyzer’ is started and shows in the browser the content of the ‘stats.json’ file in a visual format. 

The result looks like this:

Result of stats.json file in a visual format


In the portfolios.module the ‘d3-scale’ code for the bar-charts can be seen and in the portfolio-detail.module the ‘ngx-simple-charts-line’ code is shown.

Conclusion

Angular enables the creation of libraries with multiple entry points. That enables Angular to put only the code for the required entry point in its modules. That shrinks the modules to the minimum size they need to run and can provide a fast initial load with lazy-loaded modules. Creating an Angular library with multiple entry points is made easy by the Angular Cli.

Library AngularJS

Published at DZone with permission of Sven Loesekann. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Power of @ngrx/signalstore: A Deep Dive Into Task Management
  • React, Angular, and Vue.js: What’s the Technical Difference?
  • Building Angular Library and Publishing in npmjs Registry
  • The Most Popular Angular UI Libraries To Try in 2021

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: