Singleton constructor. The first time, this constructor is
called, it will persist the nodeModules path Kernel.Modules
should be loaded from. Subsequent constructor calls will ignore this
argument and return the Singleton instance. Through subscribing to
the Subscribable returned by the well-known Symbol.observable
method, the Kernel.Module loading progress can be tracked.
Optional location to load node modules from.
Instantiate the Kernel and require Kernel.Modules:
import { Kernel } from '@sgrud/core';
import { forkJoin } from 'rxjs';
const kernel = new Kernel('https://unpkg.com');
forkJoin([
kernel.require('example-module'),
kernel.require('/static/local-module')
]).subscribe(console.log);
Readonly
nodeOptional location to load node modules from.
'/node_modules'
Private
Readonly
changesInternal ReplaySubject tracking the loading state and therefore
changes of loaded Kernel.Modules. An Observable form of
this internal ReplaySubject may be retrieved by invoking the
well-known Symbol.observable
method and subscribing to the returned
Subscribable. The internal changes ReplaySubject emits
all Kernel.Module definitions loaded throughout the lifespan of
this class.
Private
Readonly
importsInternal Mapping to keep track of all via importmap
s declared
Kernel.Module identifiers to their corresponding paths. This map is
used for housekeeping, e.g., to prevent the same Kernel.Module
identifier to be defined multiple times.
Private
Readonly
loadersInternal Mapping of all Kernel.Modules loaders to a ReplaySubject. This ReplaySubject tracks the loading process as such, that it emits the Kernel.Module definition once the respective Kernel.Module is fully loaded (including dependencies etc.) and then completes.
Private
Readonly
shimmedInternally used string to suffix the importmap
and module
types of
HTMLScriptElements with, if applicable. This string is set to
whatever trails the type of HTMLScriptElements encountered upon
initialization, iff their type starts with importmap
.
Well-known Symbol.observable
method returning a Subscribable. The
returned Subscribable emits every Kernel.Module that is
successfully loaded.
A Subscribable emitting loaded Kernel.Modules.
Subscribe to the loaded Kernel.Modules:
import { Kernel } from '@sgrud/core';
import { from } from 'rxjs';
from(new Kernel()).subscribe(console.log);
Calling this method while supplying a valid module
definition will chain
the insert module operations of the module
dependencies and the
module
itself into an Observable, which is then returned. When
multiple Kernel.Modules are inserted, their dependencies are
deduplicated by internally tracking all Kernel.Modules and their
transitive dependencies as separate loaders. Depending on the
browser context, either the UMD or ESM bundles (and their respective
importmap
s) are loaded via calling the script method. When
insmodding Kernel.Modules which contain transitive
Kernel.Module.sgrudDependencies, their compatibility is checked.
Should a dependency version mismatch, the Observable returned by
this method will throw.
The Kernel.Module definition to insmod.
An optional Kernel.Module source
.
Whether to execute
the Kernel.Module.
An Observable of the Kernel.Module definition.
An Observable RangeError or ReferenceError.
insmod a Kernel.Module by definition:
import { Kernel } from '@sgrud/core';
import packageJson from './module/package.json';
new Kernel().insmod(packageJson).subscribe(console.log);
requires a Kernel.Module by name or source. If the supplied
id
is a relative path starting with ./
, an absolute path starting with
/
or an URL starting with http
, the id
is used as-is, otherwise it is
appended to the nodeModules path and the package.json
file within
this path is retrieved via Http GET. The Kernel.Module
definition is then passed to the insmod method and returned.
The Kernel.Module name or source to require.
Whether to execute
the Kernel.Module.
An Observable of the Kernel.Module definition.
require a Kernel.Module by id
:
import { Kernel } from '@sgrud/core';
new Kernel().require('/static/lazy-module').subscribe(console.log);
resolves a Kernel.Module definition by its name
. The
Kernel.Module name
is appended to the source
path or, of none
is supplied, the nodeModules path and the package.json
file
therein retrieved via Http GET. The parsed package.json
is then
emitted by the returned Observable.
The Kernel.Module name
to resolve.
An optional Kernel.Module source
.
An Observable of the Kernel.Module definition.
resolve a Kernel.Module definition:
import { Kernel } from '@sgrud/core';
new Kernel().resolve('module').subscribe(console.log);
Inserts an HTMLScriptElement and applies the supplied props
to
it. The returned Observable emits and completes when the onload
handler of the HTMLScriptElement is called. If no external src
is
supplied through the props
, the onload
handler of the element is called
asynchronously. When the returned Observable completes, the
inserted HTMLScriptElement is removed.
Any properties to apply to the HTMLScriptElement.
An Observable of the HTMLScriptElements onload
.
Insert an HTMLScriptElement:
import { Kernel } from '@sgrud/core';
new Kernel().script({
src: '/node_modules/module/bundle.js',
type: 'text/javascript'
}).subscribe();
Inserts an HTMLLinkElement and applies the supplied props
to it.
This method is used to verify a Kernel.Module bundle before
importing and executing it by verifying its Kernel.Digest.
Any properties to apply to the HTMLLinkElement.
An Observable of the appendage and removal of the element.
verify a Kernel.Module by Kernel.Digest:
import { Kernel } from '@sgrud/core';
new Kernel().verify({
href: '/node_modules/module/index.js',
integrity: 'sha256-[...]',
rel: 'modulepreload'
}).subscribe();
Generated using TypeDoc
Singleton Kernel class. The Kernel is essentially a dependency loader for ESM bundles (and their respective
importmap
s) or, depending on the runtime context and capabilities, UMD bundles and their transitive dependencies. By making use of the Kernel, applications based on the SGRUD client libraries may be comprised of multiple, optionally loaded Kernel.Modules.Decorator
Singleton