Two ways to register modules
Register the whole module registry or only what the page uses, then look modules up.
Apache-2.0Runs on this page
Package: @awacloud/fw
Register the whole fw registry with one call, or only the descriptors a page
uses, then look modules up with has, resolve and list.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
You are starting an fw page and want to decide how modules get into the
runtime. registerAll(modules) is the one-liner for a prototype;
registerAllDeep([...]) is the production shape: it keeps the page to the
module files it actually uses (fewer requests under an import map, and what a
bundler can tree-shake).
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link. - Keep one style: in a real page, delete the
registerEverythinghalf.
What it shows
fw.runtimeis the app's ready-madeModuleRuntime;new ModuleRuntime()from@awacloud/fw/core/runtime.jsgives an independent one (used here so the two counts stay honest side by side).registerAll(modules)with the registry from@awacloud/fw/core/modules.jsversusregisterAllDeep([hex, crc32]): the page prints both counts. The registry is some 200 module files, so the page imports it only when you press Load the whole registry (a dynamicimport()); the targeted runtime is ready at load.has(name),resolve(name)(instantiates once and caches) andlist({ type: 'fw.io.' })(a type prefix filter).- A module resolved from the targeted runtime doing real work: the CRC-32 of the input, as hex.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import { ModuleRuntime } from '@awacloud/fw/core/runtime.js';
import { hex } from '@awacloud/fw/io/codec/hex.js';
import { crc32 } from '@awacloud/fw/io/calc/crc32.js';
/**
* Style 1 — everything: one import of the registry, one call. Simplest, but
* the browser fetches every module file behind that import (some 200), so
* this page loads it only when asked.
* @returns {Promise<ModuleRuntime>}
*/
export async function registerEverything() {
const { default: modules } = await import('@awacloud/fw/core/modules.js');
return new ModuleRuntime().registerAll(modules);
}
/**
* Style 2 — targeted: import the descriptors you use; registerAllDeep walks
* their `deps` so transitive dependencies come along. Fewer requests, and the
* shape every other example uses (there on the app's own `fw.runtime`).
* @returns {ModuleRuntime}
*/
export function registerTargeted() {
return new ModuleRuntime().registerAllDeep([hex, crc32]);
}
/**
* @param {ModuleRuntime} runtime
* @param {string} text
* @returns {string} CRC-32 of the UTF-8 bytes, 8 hex digits
*/
export function checksum(runtime, text) {
const Crc32 = runtime.resolve('crc32');
const crc = new Crc32();
crc.append(new TextEncoder().encode(text));
return crc.get().toString(16).padStart(8, '0');
}
const targeted = registerTargeted();
/** @param {string} id @param {string} value */
const show = (id, value) => { /** @type {HTMLElement} */ (document.getElementById(id)).textContent = value; };
show('count-deep', String(targeted.list().length));
show('has-hex', String(targeted.has('hex')));
show('has-sha256', String(targeted.has('sha256')));
show('listed', targeted.list({ type: 'fw.io.' }).map((m) => m.name).sort().join(', '));
const input = /** @type {HTMLInputElement} */ (document.getElementById('input'));
const update = () => show('crc', checksum(targeted, input.value));
input.addEventListener('input', update);
update();
const loadAll = /** @type {HTMLButtonElement} */ (document.getElementById('load-all'));
loadAll.addEventListener('click', async () => {
loadAll.disabled = true;
show('count-all', String((await registerEverything()).list().length));
});index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · Two ways to register modules</title>
<link rel="stylesheet" href="../../_style/examples.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Two ways to register modules</h1>
<dl>
<dt><code>registerAll(modules)</code> — the whole registry, one import</dt>
<dd><output id="count-all">not loaded yet</output> modules registered
<button id="load-all" type="button">Load the whole registry</button></dd>
<dt><code>registerAllDeep([hex, crc32])</code> — only what this page uses</dt>
<dd><output id="count-deep"></output> modules registered</dd>
</dl>
<p>On the targeted runtime, <code>has('hex')</code> is <output id="has-hex"></output>
and <code>has('sha256')</code> is <output id="has-sha256"></output>.</p>
<p><code>list({ type: 'fw.io.' })</code> names <output id="listed"></output>.</p>
<label for="input">Text</label>
<input id="input" type="text" value="Hello" autocomplete="off">
<p>CRC-32 of the text, resolved from the targeted runtime: <output id="crc" for="input"></output></p>
</main>
</body>
</html>