fileModule

tcmts.loader.fileModule · #pxvd6o2p · term · runs anywhere

A file's module: its content as the value of its export, as saved (its {@link}s as written, so the module is a function of its hash). Text is a string; bytes, a Uint8Array.

import { exportId } from "#codebase/tcmts/core/hash";
import type { DefRecord } from "#codebase/tcmts/core/types";

export function fileModule(rec: DefRecord): string {
  const [, format, first] = JSON.parse(rec.ir) as [string, string, unknown];
  const binary = Array.isArray(first) && first[0] === "Bytes";
  const value = binary ? `Uint8Array.from(atob(${JSON.stringify(rec.sourceText)}), (c) => c.charCodeAt(0))` : JSON.stringify(rec.sourceText);
  return `// generated by tcm: a file (${format}) kept as written\nexport const ${exportId(rec.hash)}: ${binary ? "Uint8Array" : "string"} = ${value};\n`;
}

uses

used by