writePairModule

tcmts.loader.writePairModule · #5n2tdrtf · term · runs on the server

uses node:path

One module exporting a type and a value under one name. TypeScript won't merge two re-exports, but it merges a local declaration with a re-export, so the type is rendered here (under this name, with its type parameters intact) and the value is re-exported beside it. Returns the file name within the cache.

import { exportId } from "#codebase/tcmts/core/hash";
import type { DefRecord } from "#codebase/tcmts/core/types";
import { moduleFile, render, writeIfChanged } from "#codebase/tcmts/loader";
import type { DefSource, Layout } from "#codebase/tcmts/loader";
import path_2 from "node:path";

export function writePairModule(
  name: string,
  recs: DefRecord[],
  src: DefSource,
  lay: Layout,
): string {
  const type = recs.find((r) => r.kind === "type")!;
  const value = recs.find((r) => r.kind !== "type")!;
  const file = `pair_${type.hash}_${value.hash}.ts`;
  const body = render([type], src, lay, {
    style: "internal",
    exportDeclarations: true,
    nameOf: (h) => (h === type.hash ? name : src.nameOf(h)),
  }).text;
  writeIfChanged(
    path_2.join(lay.cacheDir, file),
    `// generated by tcm — do not edit. ${name}: type ${type.hash} and value ${value.hash}\n${body}\nexport { ${exportId(value.hash)} as ${name} } from "./${moduleFile(value.componentHash, src)}";\n`,
  );
  return file;
}

uses

used by