What is a Fragment in Codama?
mainA Fragment is a building block used by Codama renderers to compose generated source code. It solves the problem of managing code snippets and their dependencies simultaneously.
Instead of just a string, a fragment carries:
content: The actual source code string.imports: (In flavored fragments) AnImportMapthat tracks the symbolic dependencies required by the content.
When fragments are interpolated into one another using the fragment tagged template, the imports propagate upwards automatically. This allows you to build small pieces (like a single field) and compose them into larger structures (like a struct or a file) without manually threading import statements through every helper function.
import { fragment, use } from '@codama/fragments/javascript';
const pubkey = use('type Address', '@solana/kit');
const interfaceFragment = fragment`
export interface Account {
readonly owner: ${pubkey};
}
`;
console.log(interfaceFragment.content);
// export interface Account {
// readonly owner: Address;
// }
console.log(interfaceFragment.imports);
// Map { '@solana/kit' => Map { 'Address' => { ..., isType: true } } }