JsModuleBundle class

A set of ES modules compiled to QuickJS bytecode ahead of time.

Loading modules from source makes QuickJS call the module loader for every import, which is synchronous in the engine. A bundle resolves the whole import graph once, at build time: install registers every module in the context first, so evaluating the entry links against modules that are already there and the loader is never called.

final bundle = JsModuleBundle.compileSources(
  entry: 'main.js',
  sources: {
    'main.js': "import {greet} from './greet.js'; globalThis.out = greet();",
    'greet.js': "export const greet = () => 'hi';",
  },
);
final runtime = getJavascriptRuntime();
final result = bundle.evaluate(runtime);

Module names are the specifiers import resolves to. QuickJS normalizes only a leading ./ / ../ against the importing module's name, so keys should be paths relative to one common root ('main.js', 'lib/util.js').

Bytecode is tied to the QuickJS build shipped with this package. Compile in the same process (or the same app version) that evaluates it; see doc/wiki/api/bytecode.md.

Constructors

JsModuleBundle({required String entry, required Map<String, Uint8List> modules})
const
JsModuleBundle.fromBytes(Uint8List bytes)
Inverse of toBytes.
factory

Properties

entry String
Module name of the entry point; must be a key of modules.
final
hashCode int
The hash code for this object.
no setterinherited
modules Map<String, Uint8List>
Module name to compiled bytecode, including entry.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

evaluate(JavascriptRuntime runtime) JsEvalResult
install the bundle and evaluate its entry module.
install(JavascriptRuntime runtime, {bool resolveEntry = false}) → void
Register every module of the bundle in runtime's current context without running it. Call once per context (after a reset, register again).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toBytes() Uint8List
Serialize to a single blob (for an asset, a cache file, or a message to another isolate).
toString() String
A string representation of this object.
override
verify() → void
Link the import graph in a scratch engine without executing any module. Throws a JSError naming the specifiers that are not part of the bundle.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

compileSources({required String entry, required Map<String, String> sources, bool stripSource = true, bool verify = true}) JsModuleBundle
Compile sources (module name → source text) into a bundle.