One self-contained ESM file for one fw module
Ship one fw module as a single self-contained ES module file, no import map needed.
Apache-2.0Command line
Package: @awacloud/tool-fw-bundler
Run it
Command line Guide
Emit a single ESM file for one fw module with its dependencies inlined and no framework runtime, then import that file and use it: the deployment path with no import map.
Uses @awacloud/tool-fw-bundler (Apache-2.0) over @awacloud/fw
(Apache-2.0). This example's code is Apache-2.0.
When to use it
You want one fw module (a codec, a hash, a compressor) in a page, a worker or
a script without serving /vendor/fw/: a bookmarklet, a plugin, a
<script type="module"> with a plain relative import.
Copy-paste steps
npm install @awacloud/fwandnpm install --save-dev @awacloud/tool-fw-bundlerbunx fw-bundler standalone b64 --pkg node_modules/@awacloud/fw --out dist/b64.jsimport b64 from './dist/b64.js'and use it; or copymain.jsand runbun main.js b64 node_modules/@awacloud/fwto see the same through the tool'srunCli.
What it shows
runCli([moduleName, '--pkg', dir, '--out', file])from@awacloud/tool-fw-bundler/standalone, returning the exit code (0, or 1 for an unknown module or a build error) without spawning a process.- The emitted file ends with
export default <instance>; dependencies such asdeflateandcrc32forgzipare inlined in dependency order. - The output is deterministic for a given Bun version and carries no build
stamp;
--minis available for production. - A whole page without
/vendor/fw/: run it once per module the page uses (--out out/<name>.js) and point an import map at the files ("@awacloud/fw/io/utils/valid.js": "./out/valid.js", …). The result is a folder of static files. Note the difference: a standalone fileexport defaults the module instance, while the other examples import descriptors and register them onfw.runtime. Page code written for the folder imports the instance directly.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
// Run: bun main.js [moduleName] [fwPackageDir] (or node main.js …)
// Equivalent CLI: bunx fw-bundler standalone <moduleName> --pkg <fwPackageDir> --out <file>
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { runCli } from '@awacloud/tool-fw-bundler/standalone';
/**
* Emit `<out>`: the module's factory and its transitive dependencies as
* IIFEs in dependency order, finished by `export default <instance>`.
* @param {string} moduleName a registry name, e.g. `b64`
* @param {{ pkg: string, out: string }} opts the fw package root and the output file
* @returns {Promise<number>} the tool's exit code (0 ok, 1 unknown module or build error)
*/
export function buildStandalone(moduleName, { pkg, out }) {
return runCli([moduleName, '--pkg', pkg, '--out', out, '--no-source-comments']);
}
/**
* Use the emitted file the way a page or a script would: import it, get the
* instance. No runtime, no registry, no import map.
* @param {string} file
* @returns {Promise<any>} the module instance
*/
export async function useStandalone(file) {
return (await import(pathToFileURL(file).href)).default;
}
/**
* @param {string[]} argv
* @returns {Promise<string[]>} report lines
*/
export async function main(argv = process.argv.slice(2)) {
const [moduleName = 'b64', pkg = 'node_modules/@awacloud/fw'] = argv;
const scratch = mkdtempSync(join(tmpdir(), 'awa-example-standalone-'));
try {
const out = join(scratch, `${moduleName}.js`);
const code = await buildStandalone(moduleName, { pkg, out });
if (code !== 0) return [`standalone exited with ${code} for "${moduleName}"`];
const instance = await useStandalone(out);
const lines = [`wrote ${moduleName}.js (exit ${code})`, `exports: ${Object.keys(instance).sort().join(', ')}`];
if (moduleName === 'b64') lines.push(`b64 of "fw": ${instance.fromBytes(new TextEncoder().encode('fw'))}`);
return lines;
} finally {
rmSync(scratch, { recursive: true, force: true }); // a real build keeps --out; this demo cleans up
}
}
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
for (const line of await main()) console.log(line);
}