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

lazy-module-loader

v1.0.2

Published

Async module loader with support for lazy loading and preloading

Downloads

7

Readme

lazy-module-loader

Lazy Module Loader is a universal JavaScript module loader allowing to load modules with asynchronous resolution of their dependencies.

Build Status npm version

Intention

Lazy Module Loader had been widely used in Opera Desktop browser as a dedicated solution for modularity and bundling mechanism for Web UI code before the ES modules were fully supported.

Usage

Once the Lazy Module Loader script is loaded, it defines a global loader instance, which can be used as follows:

const module = await loader.require('my/module');

The requested module is loaded with all its required dependencies resolved.

Path resolution

By default the resource path is caluclated with simple rules:

No extension - the JS file extension is appended:

  • my/module to my/module.js

Directory-like path - points to the main module in the directory:

  • my/module/ to my/module/main.js

Custom extension - returns the unchanged path:

  • my/module/base.css to my/module/base.css

Module format

Modules utilize the CommonJS format, they can define the async init() method which is used to inject references to dependencies.

let Sevice;

const Module = {

  async init() {
    Service = await loader.require('some/dependendency');
  }

  loadData() {
    return Service.loadSomeData();
  }
};

module.exports = Module;

Lazy loading

Modules can also define optional dependencies (in a form of symbols) to other modules which are loaded at runtime, if needed. This is particularly useful when no direct references are needed, for example:

const WelcomePage = loader.symbol('pages/welcome/');
const ContactPage = loader.symbol('pages/contact/');

class Router extends Component {

  render() {
    switch (this.props.page) {
      case 'contact':
        return ContactPage;
      default:
        return WelcomePage;
    }
  }
}

module.exports = Router;

The dependencies don't have to be loaded until the router component decides to render the particular page.

Preloading

In order to achieve the maximum performance and responsiveness at runtime, modules can be preloaded at startup, not to lazy-load them upon user actions.


const module = await loader.preload('my/module');

Such call will load recursively all the required and optional dependencies.

Bundling

Modules can also be bundled together with all their dependencies into a single file. What allows them to be loaded synchronously in a bunch.

require('./bundler.js');

bundler.generate('root/module', 'My Library', {
  prefix: 'some/prefix/',
});

Plugins

Loader can chain the interceptor plugins, to serve as a fallback to custom loaders.

const plugin = {
  path(key) {
    const path = super.path(key);
    if (someCondition) {
      return `http://www.example.com/module/${path}`;
    }
    return path;
  },
};
loader.use(plugin);

Registry

All the loaded modules are kept in a registry. It is global and can be accessed as follows:

loader.registry

The registry is a map allowing to access the module information and easily analyze the dependency graph. Each module can be checked for all its dependencies and clients referencing it.