chunkGroup

tcmts.cli.web.chunkGroup · #uqkltpun · term · runs anywhere

The chunk a module of an app's browser code goes in, so what changes together ships together and what doesn't keeps its URL: React (its packages change together, and rarely); the other npm packages; tcm's runtime; and the app's own code; the entry stays its own. A few files, not one per definition: each is a request, and small files compress worse.

import { REACT_RUNTIME, STUB } from "#codebase/tcmts/cli/web";

export function chunkGroup(id: string): string | null {
  // (a server function's stub names the function's hash: it changes with the app)
  if (id.startsWith(STUB)) return "app";
  if (id.startsWith(REACT_RUNTIME) || id.startsWith("\0virtual:tcm") || id.startsWith("\0tcm"))
    return "tcm";
  const pkg = /node_modules\/(?:\.pnpm\/[^/]+\/node_modules\/)?((?:@[^/]+\/)?[^/]+)/.exec(id)?.[1];
  if (pkg) return /^(npm__)?(react|react-dom|scheduler)(__|$)/.test(pkg) ? "react" : "packages";
  // the codebase's modules, named by their hash; the entry stays in no group (in one, it takes everything with it)
  return /[a-z2-7]{52}\.(ts|tsx|js)$/.test(id) ? "app" : null;
}

uses

used by