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-scripter

v0.1.4

Published

An Angular library that helps in loading JS script on demand

Downloads

144

Readme

NgScripter - One More Angular Script Loader

ng-scripter is a simple script loader that helps in loading external JS scripts on demand. This utility will help in decreasing the initial page load time since it gives the ability to load any JS script dynamically when required.

Installation

NPM:

npm i ng-scripter

Yarn:

yarn add ng-scripter

Setup

Import ScriptLoaderModule inside the module that requires it (AppModule or any other module)

import { ScriptLoaderModule } from 'ng-scripter';
 
@NgModule({
  ...
  imports: [
    ...
    ScriptLoaderModule
  ]
})

Usage

Inject the ScriptLoaderService inside a component/service

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private scriptLoaderService: ScriptLoaderService) {
  }

  loadScript = () => {
    const script: Script = {
      id: 'faker-id',
      src: 'https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js',
      async: true,
      defer: false,
      crossOrigin: 'anonymous'
    };
    this.scriptLoaderService.loadScript(script).subscribe(
      (data: Script) => console.log('Script Loaded ', data),
      (err) => console.log('Script failed ', err)
    );
  };

}

Options

The Script model has the following attributes.

| Attribute | Required | Type | Description | | ------------- | ------------- | ------------- | ------------- | | id | Yes | string | An Id for the script. (Preferably Unique) | src | Yes | string | The source url | | async | No | boolean | Async load or not (Default : false) | | defer | No | boolean | Defer or not (Default : false) | | crossOrigin | No | string | CORS |

Usage of watch

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy {
  private watcherSubscription: Subscription;
  ......  
  constructor(private scriptLoaderService: ScriptLoaderService) {
  }

  ngOnInit(): void {
      // get notified every time a new script is loaded the first time  
      this.watcherSubscription = this.scriptLoaderService.watch().subscribe(
        script => {
          console.log('Watcher => ', script);
        }
      );
    }
  
    ngOnDestroy(): void {
      if (this.watcherSubscription) {
        this.watcherSubscription.unsubscribe();
      }
    }

}

Other notable functions

  1. isScriptLoaded : Checks if a script is loaded or not using the script Object. Returns true or false
  2. isScriptLoadedViaSrc : Checks if a script is loaded or not using the script's src. Returns true or false
  3. isScriptLoadedViaId : Checks if a script is loaded or not using the script's id. Returns true or false

Advantages over other script loaders

  1. Avoids loading the same script multiple times
  2. Ability to add initial delay before loading a script
  3. Shared Observables used, so if a script is inProgress of loading and at that time another request to load that script comes in then the same instance of load is shared.
  4. Various function to check if a script is loaded
  5. Ability to get notified everytime a new script is loaded