This context provides a detailed guide on how to standardize Angular UI and layouts by sharing libraries on NPM.
Abstract
The context discusses the importance of maintaining consistency in UI components, services, and styles across a series of websites for a better user experience. It suggests moving common files to a public library to keep consistency, reduce maintenance efforts, and standardize the look of products from the same team or company. The guide covers creating a new Angular library, publishing it to NPM, and introducing the Angular Package Format (APF). It includes steps to create a new library in a project, move existing code to the library and expose them, build, pack, install, and use the library locally, and publish the library to NPM. The context also explains the purpose of ESM (ES2015, ES2020 Module) and the different exported JavaScript module formats in APF.
Bullet points
Maintaining consistency in UI components, services, and styles across websites is important for user experience.
Moving common files to a public library can keep consistency, reduce maintenance efforts, and standardize product look.
The guide covers creating a new Angular library, publishing it to NPM, and introducing APF.
Steps to create a new library in a project include executing a command, updating package.json, and creating a new module for the library.
Moving existing code to the library involves moving components and updating the module and public-api.ts.
Building, packing, installing, and using the library locally involves several commands and updates to app.module.ts.
Publishing the library to NPM involves updating the package.json, logging in to npm, and publishing the package.
APF enables a package to work seamlessly under most common scenarios and supports different module formats.
ESM provides a native way to load JavaScript modules into a browser and has two common formats: ESM and FESM.
APF includes multiple exported JavaScript module formats such as types, esm2020, es2020, es2015, node, and default.
Standardize Angular UI and Layouts by Sharing Libraries on NPM
Introduction
When creating a series of websites, the consistency of UI components, services, and styles between these websites is extremely important for the user experience.
For example, the problem for maintaining a series of website is we don’t always have time to apply all new layouts or UI components to different projects.
Moving the common files to a public library can not only keep the consistency but can also reduce our efforts to maintain multiple applications individually. Additionally, it’ll be helpful to standardize the look of the product from the same team or the same company.
cd projects/documentation-ui/src/lib
ng g m documentation-ui
Move existing code to the library and expose them
1. Move the existing component into the module.
projects/documentation-ui/src/lib/documentation-ui/theme: move the theme.component.ts, theme.service.ts, theme.domain.ts we created from the previous article to this path
projects/documentation-ui/src/styles/theme: move the _index.scss file to this path
2. Update the documentation-ui.module.ts and expose the component
We also have to import the third-party dependencies in the module (e.g., FontAwesomeModule).
3. Update the projects/documentation-ui/public-api.ts
4. Update the projects/documentation-ui/ng-package.json to add the assets
Build, pack, install, and use the library locally
Go back to the root folder of the host application.
1. Build, pack, and install the library.
It will generate the dist/documentation-ui/documentation-ui-0.0.1.tgz.
npm run build-library
npm run pack-library
npm run install-library
2. Update the app.module.ts to import the built package
3. Now we can use exported SCSS files in the project.
4. Now we can use exported services in the project
5. Now we can use exported components in the project
Publish the Library to NPM
After installing and testing the library locally, we could publish our packages to npm publicly.
Update projects/documentation-ui/readme.md: add instructions of the installation, usage, or compatibility table for users.
2. Update projects/documentation-ui/package.json
name, version, license, keywords: the information will be displayed on the npm package page
peerDependencies: packages required by this library but they won’t be packed into this library (npm 7+ will help to install them, but npm 6 or early versions won’t)
dependencies: packages required by this library and they will be packed into this library
3. Login to npm
npm login
4. Publish the package to npm
npm publish dist/documentation-ui/documentation-ui-0.0.1.tgz --access public
5. Check the package page to make sure there are still any confusing information
6. Install the Library from NPM and the adoptions
Even if the testing locally works fine, it’s still something unexpected when installing the published library in applications with different Angular versions.
The common root cause is from the depended third-party libraries. Move them from dependencies to peerDependencies to prevent fixing depended third-party libraries into our library’s package.
Introduction to Angular Package Format (APF)
APF enables a package to work seamlessly under most common scenarios. The APF can be compatible to TypeScript modules, CommonJS modules, ESM, and etc.
The purpose of ESM (ES2015, ES2020 Module) is to provide a native way to load JavaScript modules into a browser. There are two common formats of files.
ESM: modules are separated in different files individually.
FESM: flatten all module into a single file.
After packing the package, check the file structure under the dist/documentation-ui and the dist/documentation-ui/package.json . There are multiple exported JavaScript module formats.