The most important class that is exposed by Components.js is the
which essentially does only two things:
- It accepts module and component registrations, so that Components.js knows how to instantiate components.
- It instantiates components based on configurations.
This is an example of registering and instantiating by URL:
const Loader = require('componentsjs').Loader; const loader = new Loader(); await loader.registerModuleResourcesUrl('path/or/url/to/my-module.jsonld'); const myComponent = await loader.instantiateFromUrl( 'http://example.org/myInstance', 'path/or/url/to/config-my-component.jsonld');
Loader has a strict registration and instantiation phase. This means that all registrations should happen before instantiations. Any registrations being done after an instantiation has happened will result in a runtime exception.
This is needed because the Loader creates internal caches when the first instantiation is done to increase the efficiency of all following instantiations.
The Loader can be constructed with an optional options hash. This hash accepts the following optional entries:
||If Components.js modules and JSON-LD contexts should be scanned in all globally installed NPM modules as well, instead of only the locally installed modules.||
||If component configurations with relative file path strings (
||A hash with JSON-LD contexts. Entries with key a JSON-LD context URI and values a string representation of a JSON-LD context.||Collected from all scanned modules.|
||Path that should be considered the root Node module. Everything will happen relative to this path.||The path of the closest NPM module, which is determined by iteratively checking the parents of the Node working directory.|