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

@herodevs/hero-loader

v2.0.1

Published

This component allows you to lazily load your Angular module whenever you want, instead of being restricted to lazy loading on route changes.

Downloads

77

Readme

<hero-loader> for lazy loading in Angular

Every Angular app is different and has different needs. Yet Angular only provides one method for lazily loading code: using the loadChildren piece in the routes for any given module. By using the loadChildren piece of a route, you are telling Angular and the Angular CLI to help you out and lazily load that piece of the app when the associated route is hit. This is a very efficient tool that we should all be using.

HOWEVER!!!

Some of us need more flexibility when lazily loading modules. Some modules need to be triggered to load on events BESIDES route change. Maybe a click or a mouseover. Maybe when the user has admin rights, or when they don't have admin rights. This is why we built <hero-loader>. Using this component, combined with an ngIf, you can trigger lazy loading of a module for just about any scenario that you can think of.

How does it work?

To do this, we utilize the exact same pieces of Angular that loadChildren from routes uses. But we do it in a different way. Let's look at how it works.

Getting Started

Start by installing the right node module:

npm install @herodevs/hero-loader

At this point, we have all that we need to get started. We only need to do some configuring. We need to do the following:

  1. Tell Angular to create a separate bundle for the module that we intend to lazy load.
  2. Import HeroLoaderModule where we intend to use this lazy loading.
  3. Tell <hero-loader> to load that bundle when needed.

Let's do this one at a time.

Create a separate bundle for our module

Open your angular.json file. In that file, look for the nested property projects.<your-project-name>.architect.build.options where <your-project-name> is the name of your project. Once you have the build options property in sight, add the lazyModules property to the options:

 "options": {
    ...
    "lazyModules": [ "src/app/test/test.module" ]
 }

In the above example, you are telling the Angular CLI to prepare a separate bundle for TestModule in the file src/app/test/test.module.ts. You will notice that this looks a lot like the loadChildren syntax for a route. That's because this lazyModules property is doing the same thing that the loadChildren property does in a route. Now the Angular CLI knows to create a separate bundle for the TestModule.

Import HeroLoaderModule

In your app, you need to add HeroLoaderModule to the imports of one of your app's NgModules

@NgModule({
  imports: [HeroLoaderModule],
})
export class AppModule {}

Now your app knows about the HeroLoaderModule and you can use the <hero-loader> component to lazy load the TestModule.

Use <hero-loader> in our app

The following is an example of how to use <hero-loader> to load our TestModule.

<div (mouseover)="load = true">Hover to load TestModule</div>
<hero-loader *ngIf="load" moduleName="src/app/test/test.module#TestModule"></hero-loader>

When you hover the <div> above, the ngIf will turn on the <hero-loader> component which will then load the TestModule and it will use whatever component is listed in the TestModule.bootstrap property and attach that component to the inside of the <hero-loader> component.

Consider that TestModule looks as follows:

@NgModule({
  declarations: [TestComponent],
  bootstrap: [TestComponent],
})
export class TestModule {}

Using <hero-loader> to load the TestModule will the TestComponent inside of the the <hero-loader> component that you added to your template.

Question