This project demonstrates how to create a versatile Angular library with multiple entry points, allowing for better organization, improved tree shaking, and enhanced code splitting.
This guide walks you through creating an Angular library with multiple entry points to improve your project's modularity, tree shaking, and code splitting. By the end of this guide, you'll have a well-organized library with separate entry points for different components, enabling better performance and maintainability.
npm install -g @angular/cli
).Clone the repository:
git clone https://github.com/simplycodesmart/stellar-ui-workspace.git
cd stellar-ui-workspace
Install dependencies:
npm install
Create a new Angular workspace without an initial application:
ng new stellar-ui-workspace --create-application false
cd stellar-ui-workspace
Install necessary dependencies using npm:
npm install
Generate the library named @stellar-ui/st-sdk
:
ng generate library @stellar-ui/st-sdk --prefix=st
Create subentry points for Button
, Card
, and Accordion
components:
cd projects/stellar-ui/st-sdk
ng generate component button/button --flat=true --inlineTemplate=true --inlineStyle=true
ng generate component card/card --flat=true --inlineTemplate=true --inlineStyle=true
ng generate component accordion/accordion --flat=true --inlineTemplate=true --inlineStyle=true
Create public-api.ts
, index.ts
, and ng-package.json
in each subentry point folder.
Update tsconfig.json
for flexible dependency handling. For example, if Button
and Card
are used in Accordion
, they should be imported accordingly.
Adjust the angular.json
configuration to include tests for subentry points:
{
"projects": {
"@mycomp/ui-sdk": {
"sourceRoot": "projects/stellar-ui/st-sdk"
}
}
}
Create a test app to demonstrate the library usage:
ng generate app demo-app
Update tsconfig.app.json
to include the library paths:
"paths": {
"@stellar-ui/st-sdk/*": ["./../../dist/stellar-ui/st-sdk/*"],
"@stellar-ui/st-sdk": ["./../../dist/stellar-ui/st-sdk"]
}
Update app.component.ts
to use the AccordionComponent
from the library.
By following this guide, you have successfully created an Angular library with multiple entry points, enhancing organization, performance, and dependency management. This approach will help you craft robust and versatile Angular libraries that empower developers and improve application quality.
The source code for the @stellar-ui/st-sdk
library and the demo app is available on GitHub:
Stellar UI Workspace
For a detailed explanation, check out the accompanying Medium article: Crafting a Versatile Angular Library: A Guide to Multiple Entry Points
This project is licensed under the MIT License - see the LICENSE file for details.
Now it includes the Medium article link as well. Let me know if you need further adjustments!