# @gram-lang/modules [![npm](https://img.shields.io/npm/v/@gram-lang/modules?color=cb3837&style=flat-square)](https://www.npmjs.com/package/@gram-lang/modules) [![License: GPL v3](https://img.shields.io/badge/License-GPL_v3-blue?style=flat-square)](https://www.gnu.org/licenses/gpl-3.0) *Part of the [Gram monorepo](https://git.gram-lang.org/gram-lang/gram) ยท [Documentation](https://gram-lang.org/docs/)* 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 once. This package defines the host interface (`ModuleHost`) and touches no filesystem of its own: file resolution and reading are the caller's job (CLI, language server, conformance runner), keeping the package pure with no Node built-ins. It ships `createMemoryHost()`, an in-memory `ModuleHost` for browser or sandboxed environments such as the playground. ## Installation ```bash npm install @gram-lang/modules # or bun add @gram-lang/modules ``` ## Usage ```typescript 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 the [GPL-3.0](https://www.gnu.org/licenses/gpl-3.0) license.