gram/packages/modules
Repository files (latest commit first)
Filename Latest commit message Latest commit date
abiwab 2b5ae73f33
All checks were successful
CI / Build & Test (pull_request) Successful in 7m22s
chore: release v1.5.2
2026-11-27 23:06:21 +01:00
..
src fix(kitchen): say what a ratio's target problem is, by the name its author wrote 2026-11-19 20:06:46 +01:00
tests fix(kitchen): warn on a second baker's percentage base instead of throwing 2026-11-21 20:42:35 +01:00
CHANGELOG.md chore: release v1.5.2 2026-11-27 23:06:21 +01:00
package.json chore: release v1.5.2 2026-11-27 23:06:21 +01:00
README.md docs: refresh root and package READMEs for v1.5 2026-11-19 12:01:21 +01:00
tsconfig.json feat(modules): scaffold @gram-lang/modules — host interface, exports, graph loader 2026-08-17 18:56:18 +02:00

@gram-lang/modules

npm License: GPL v3

Part of the Gram monorepo.

Resolves and composes @use import directives. Loads a document's import graph, computes each module's public exports and yield, renames its intermediates to avoid collisions with the host, and splices its sections into a single composed AST — so the whole document is compiled, and ALAP-scheduled, exactly once.

This package defines the host interface (ModuleHost) and implements none that touches a disk: file resolution and reading are the caller's job (CLI, language server, conformance runner), so this package stays pure — no built-in Node APIs, no filesystem access of its own. The one host it ships is createMemoryHost(), an in-memory ModuleHost for places with no real filesystem, like the playground, where a base recipe and its importer can be edited side by side.


📚 General Documentation

For full syntax specifications, @use import rules, and best practices, please refer to the Gram Documentation.


🛠️ Installation

npm install @gram-lang/modules
# or
bun add @gram-lang/modules

⚡ Usage

import { createMemoryHost, loadModuleGraph, composeRecipe } from '@gram-lang/modules';
import { compile } from '@gram-lang/kitchen';

const host = createMemoryHost({
  '/bases/dough.gram': '## Dough ->&dough\n\nMix @flour{500g} and @water{300ml}.\n',
  '/pizza.gram': '@use "./bases/dough.gram" as &dough\n\n## Bake\n\nTop &dough{250g} and bake.\n'
});

const graph = await loadModuleGraph('/pizza.gram', host);
const composed = composeRecipe(graph, { db: {} });
const compiled = compile(composed.ast);

🏗️ Structure

  • src/host.ts: ModuleHost interface and ModuleGraph types.
  • src/memory-host.ts: createMemoryHost(), in-memory virtual host.
  • src/graph.ts: loadModuleGraph(), recursive import loader and cycle detection.
  • src/exports.ts: Computes each module's public exports.
  • src/yield.ts: Measures imported module yields for scaling and --stock.
  • src/rename.ts: Renames intermediates to avoid collisions across modules.
  • src/compose.ts: composeRecipe(), splices composed sections into a single AST.

📄 License

Licensed under GPL-3.0-or-later.