npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-samurai

v3.0.0

Published

A blank schematics

Downloads

4,839

Readme

Logo

Improve tree shaking of your Angular library - more details on this blog post

Overview

Nowadays, thanks to the Angular CLI, libraries are easy to create. They are a great way to share code across multiple applications. Since they can be used in many places, performance is a critical aspect. A library that doesn’t perform can slow down multiple applications!

This blogpost offers a detailed explenation how wrongly packaged libraries can increase the main bundle size and slow down applications initial load.

Ng-packagr offers a great feature called subentries to improve tree shaking. There are a lot of things to be aware of when trying to convert your library to take advantage of subentries.

Ng-samurai is an Angular schematic which automatically updates your library to take advantage of subentries and improve tree shaking. Furthermore, it helps you to quickly generate new subentries.

Installation

npm i -D ng-samurai

Getting started

Once ng-samurai is installed we have two different schematics commands available - one for spliting an existing library into multiple chunks (subentries) and another one for creating a new subentry.

#Available schematics ng-samurai provides two schematics: split-lib and generate-subentries

Split

Spliting your libary automatically into multiple chunks our library project needs to fullfill a couple of cirterias:

  • Nesting of modules: Modules used by other modules can only be siblings and never children. There should always be one only one module per subentry.

Go ahead and run the following command in the root of your project:

ng g ng-samurai:split-lib

This will do the following things:

  • Will convert each folder where it encounters a module to a subentry - it will add a (index.ts, public-api.ts, package.json)
  • Will export all the necessary Typescript files from the public-api. Necessary files are (components, services or other Typescript files expect .spec files)
  • Will update the public-api in the root level and export all subentries
  • Will adjust the paths of your tsconfig.json so that your IDEA understands subentris

Prerequisit for a successfull split

For ng-samurai to function appropriately, there are certain requirements your library needs to fulfill. In some cases, you might need to refactor your application before using ng-samurai.

Folder structure

Converting your library to subentries may also require a change of the folder structure. Each module will result in a subentry and needs its folder. Subentries can not have multiple modules.

Valid file structure Logo

Invalid file structure Logo

Circular dependencies

A subentry can use another subentry. But subentries can not work with circular dependencies. Logo

Each file needs to belong to a module

Entry points can contain all sorts of files. ng-samurai needs a module-file to be present to perform the migration. The .module file indicates to ng-samurai that this code will be split into a subentry. Logo

Generate subentry

Once your library is converted to subentries, it's likely that you want to add new subentries. To do so, you can run the following command:

ng g ng-samurai:generate-subentry

This will do the following things:

  • Will create a new folder with the provided name
  • Will create a (module, component, index.ts, public-api.ts, package.json)
  • Will export the module and the component from the public-api.ts

Further resources

If the topic of subentries is new to you. The following resources explain subentries in more detail.