↑ Usage Guide Index | ← Core API OVERVIEW
Runtime dynamic imports with a tiny registry. Use
net.modules.load(id, url)to import an ES module and retrieve its namespace. Subsequent calls return the cached module unless you opt to invalidate.
The ModuleManager is a thin convenience wrapper around import() that:
- Keeps a registry of loaded modules keyed by
id. - Normalizes absolute vs relative URLs via the HTTP base rules when helpful.
- Provides simple cache control (re‑load / invalidate patterns).
- Plays nicely with BootStrap or other systems that want to look up modules by ID later.
Creates a module manager bound to a Net instance (used for URL resolution and shared defaults).
-
Parameters
net(Net) — ANetwith configured HTTP/base URL semantics.
-
Returns
ModuleManagerinstance withload,get,has,unloadhelpers.
Dynamically import a module and register it under id.
-
Parameters
-
id(string) — Registry key to store the module namespace under. -
url(string) — URL to the module (relative or absolute). Relative paths resolve against the HTTP base when set. -
opts(object, optional)force(boolean) — Iftrue, bypass cache and re‑import (adds a cache‑busting query param by default).resolve(function) — Optional(url, net) => stringto customize resolution.
-
-
Returns
Promise<any>— The module namespace (whatimport(url)returns).
-
Behavior
- If
idalready exists andforce !== true, returns the cached module. - If
force === true, re‑imports the URL (e.g.,?t=TIMESTAMP) and replaces the cache.
- If
Return the cached module namespace for id, or undefined if missing.
Boolean — true if a module with id exists in the registry.
Remove the entry from the registry (does not purge the browser/module loader cache). Useful to opt a name out of lookups; a subsequent load(id, url, { force:true }) will re‑import.
- Absolute URLs (
https://…) import as‑is. - Relative URLs are resolved against
net.httpbase URL when configured; otherwise, they are resolved relative to the current document/module. - Provide a custom
opts.resolve(url, net)to inject repo/CDN rewriting.
import Net from 'm7Fetch';
const net = new Net();
const math = await net.modules.load('math', '/modules/math.js');
console.log(math.add(2, 3));await net.modules.load('plugin', '/plugins/logger.js', { force: true });await net.modules.load(
'ui.toast',
'ui/toast.js',
{ resolve: (url, net) => new URL(url, 'https://cdn.example.com/app/').href }
);const toast = net.modules.get('ui.toast');
toast.show('Hello');- Duplicate IDs: calling
load(id, url)twice returns the cached module unlessforce:trueis passed. - Failed import: errors from dynamic
import()propagate; wrap withtry/catchor preferformat:'full'on related HTTP probes. - URL typos: path resolution follows the same rules as HTTP; verify base URL if relative imports fail.
- Specs/HTTP: independent. Only uses Net’s base rules for URL resolution.
- Batch: not used directly; module import is a single async operation.
- BootStrap: modules loaded here can be referenced by ID in your bootstrap/mount flows.
class ModuleManager {
constructor(net: Net)
load(id: string, url: string, opts?: { force?: boolean; resolve?: (url: string, net: Net) => string }): Promise<any>
get(id: string): any | undefined
has(id: string): boolean
unload(id: string): boolean
}- CORE_API_NET.md — where
modulesis exposed. - EXAMPLES_LIBRARY.md — dynamic module load and invocation recipes.