pwaAssets

tcmts.cli.web.pwaAssets · #hyc2cdvd · term · runs on the server

uses node:url

A PWA's manifest and images, as its pwa (a hash, materialized) says them (webManifest): kept among the assets every build shares, compressed, by their content. Returns their names, with what the page's head gets. hold is told each name before it's kept (a build leases them: leaseBuilds).

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { assetsDir, precompress, webManifest, writeWhole } from "#codebase/tcmts/cli/web";
import { exportId } from "#codebase/tcmts/core/hash";
import { createHash } from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";

export async function pwaAssets(
  cb: Codebase,
  hash: string,
  hold: (name: string) => void = () => {},
): Promise<{ files: string[]; head: string[]; title: string; warnings: string[] }> {
  const rec = cb.store.getDef(hash)!;
  const mod = (await import(pathToFileURL(cb.modulePath(rec.componentHash)).href)) as Record<
    string,
    unknown
  >;
  const dir = assetsDir(cb);
  fs.mkdirSync(dir, { recursive: true });
  const files: string[] = [];
  const made = webManifest(mod[exportId(hash)], (content, stem, ext) => {
    const name = `${stem}-${createHash("sha256").update(content).digest("hex").slice(0, 12)}${ext}`;
    const file = path.join(dir, name);
    hold(name);
    if (!fs.existsSync(file)) writeWhole(file, content);
    precompress(file);
    if (!files.includes(name)) files.push(name);
    return name;
  });
  return { files, ...made };
}

uses

used by