How bytecodealliance/jco works
Project type and purpose
• This is an open-source, multi-tool CLI and library for working with WebAssembly Components from JavaScript/TypeScript.
• It combines a JS/Node CLI (packages/jco), a runtime shim for WASI preview2 (packages/preview2-shim), and several Rust crates that compile to WebAssembly components (crates/js-component-bindgen, crates/js-component-bindgen-component, crates/wasm-tools-component).
File structure
• Top-level “packages” directory holds the JavaScript/TypeScript npm packages:
• jco – the CLI and JS API
• preview2-shim – a runtime library mapping WASI Preview2 to Node/browser
• “crates” directory holds Rust subprojects:
• js-component-bindgen – core Rust logic for generating JS bindings
• js-component-bindgen-component – the Rust crate compiled as a WebAssembly Component to expose js-component-bindgen at runtime
• wasm-tools-component – exposes selected pieces of wasm-tools as a WebAssembly Component
• xtask – a Rust build/task runner orchestrating code-gen, tests, and packaging
• examples / docs hold usage samples and documentation
• scripts contain ancillary automation (e.g., IDL generation, versioning)
README for additional insights
• The README’s “Organization” table explicitly maps each subproject to its role
• The core workflows: build components, transpile to ES modules, run components, reuse workflows, and invoke wasm-tools from JS
Main components
a. CLI & JS API (packages/jco)
b. Runtime Shim (packages/preview2-shim)
c. Wasm-based Components:
• js-component-bindgen-component.wasm
• wasm-tools-component.wasm
d. Rust Build & Code-gen Tasks (crates/xtask + submodules: wit-bindgen, wasmtime, wasm-tools, WASI-Virt)
e. External JS library: componentize-js (dynamically imported at runtime)
f. Host Environments: Node.js and Browser
Relationships and data flows
• User invokes jco CLI → CLI parses commands → calls into the JS API
• JS API dynamically imports/instantiates the appropriate WebAssembly Component(s) under the WASI Preview2 environment provided by preview2-shim
• For “transpile” and “componentize” commands:
• JS API instantiates js-component-bindgen-component.wasm → marshals arguments (WIT, source files) → receives back a WebAssembly Component or JS binding code
• JS API instantiates wasm-tools-component.wasm for lower-level wasm-tools operations (e.g., optimization, validate)
• Results flow back to the JS API → CLI prints output or writes generated files
• Build-time: xtask orchestrates compiling the Rust crates (producing .wasm files), generating bindings/tests, and copying outputs into packages/* for npm packaging
Architectural patterns and design principles
• Polyglot monorepo: JS/TS packages alongside Rust crates
• Plugin-style extensibility: functionality shipped as wasm “plugins” (components) that the CLI can load on demand
• Clear separation of concerns:
• CLI & orchestration (JS)
• Runtime environment abstraction (preview2-shim)
• Core logic in Rust, exposed via WebAssembly Components
• Build orchestration via xtask
• Reuse of standard WASI Preview2 and WebAssembly Component Model interoperability
Elements
• User → CLI → JS API → instantiate(WASM component) → host environment
• JS API → dynamic import(componentize-js)
• xtask → cargo build → .wasm artifacts → npm package
• JS/TS packages (e.g. blue rectangles)
• Runtime shim (green)
• WASM Components (yellow)
• Rust build tasks (grey)
• External dependencies (light border)
Detailed capture of essential elements
• Don’t omit the two wasm-component crates or the preview2-shim—these are what truly differentiate Jco from a plain JS CLI
• Call out that js-component-bindgen-component.wasm and wasm-tools-component.wasm are first-class runtime modules
• “xtask” as the orchestrator that ties together Cargo, submodules, and packaging into the npm packages