The ComponentsManager offers three different ways for registering modules and their components:

  1. By automatic NPM module scanning
  2. By URL
  3. By triple stream

Note

All of these registration methods return void promises. They resolve when they finish registration, and they reject on errors.

The caller is responsible for ensuring that registrations are finished before instantiations are being done, otherwise undefined behaviour may occur.

This could for example by done by using the await keyword.

Registering by Automatic NPM Module Scanning

In most cases, the most convenient way to register modules and components is by letting Components.js look for them automatically. This is the default mode of operation when creating a ComponentsManager when no moduleLoader is passed:

import { ComponentsManager } from 'componentsjs';

const manager = await ComponentsManager.build({
  mainModulePath: __dirname, // Path to your npm package's root
});

By default, Components.js will iterate over the current main module and its NPM dependencies. It will look for an lsd:components entry in each package.json file, which is the standard way of exposing components. If such an entry is found, the referenced modules and components are registered to the Loader. Example package.json contents:

{
  ...
  "lsd:module": true,
  ...
}

Note

When many dependencies are installed, scanning can introduce a noticable overhead. So when this overhead becomes a problem, one of the other registration methods should be used.

Registering by URL

A more hands-on way of registering modules and components is by registering them from a certain URL or path. For this, you can override the moduleLoader callback when building your ComponentsManager.

For example:

const manager = await ComponentsManager.build({
  mainModulePath: __dirname, // Path to your npm package's root
  moduleLoader: async(registry) => {
    await registry.registerModule('http://example.org/my/module.jsonld');
    await registry.registerModule('path/to/my/module.jsonld');
  },
});

This method will read the file at the given path and register all the modules and their components it can find into the ComponentsManager.

The promise will resolve when the file has been read and all discovered modules have been fully registered.

Note

JSON-LD files are not a requirement, other RDF serializations can also be interpreted automatically as mentioned here.

Registering by triple stream

A more advanced way of registering modules is by registering them using a triple stream.

For example:

const manager = await ComponentsManager.build({
  mainModulePath: __dirname, // Path to your npm package's root
  moduleLoader: async(registry) => {
    await registry.registerModuleStream(myTripleStream);
  },
});

This stream should contain triples using the appropriate module and component vocabularies. The triples in this stream must be defined using quad representation from RDF/JS. All discovered modules and components will be registered into the ComponentsManager.

The promise will resolve when the stream has been fully consumed and all discovered modules have been fully registered.

Example Source