assetSource
uses node:path
Where a place in a production build's chunk (its line and column, from 1) came from: the codebase's generated module
(.tcm/cache/<hash>.ts) and its line, as the chunk's source map (mapsDir) says. Undefined for a chunk with no map
kept (one built before maps were, or pruned), or a place in a package's code.
import type { Codebase } from "#codebase/tcmts/cli/codebase"; import { readMaps } from "#codebase/tcmts/cli/main"; import { mapsDir } from "#codebase/tcmts/cli/web"; import fs from "node:fs"; import { SourceMap } from "node:module"; import path from "node:path"; export function assetSource( cb: Codebase, asset: string, line: number, column: number, ): { file: string; line: number } | undefined { const at = path.join(mapsDir(cb), `${asset}.map`); let map = readMaps.get(at); if (map === undefined) { try { map = fs.existsSync(at) ? new SourceMap(JSON.parse(fs.readFileSync(at, "utf8"))) : null; } catch { map = null; } if (readMaps.size > 200) readMaps.delete(readMaps.keys().next().value!); readMaps.set(at, map); } const entry = map?.findEntry(line - 1, column - 1) as | { originalSource?: string; originalLine?: number } | undefined; // the module, by the component hash it's named by: wherever the build had it, it's in the cache const module = entry?.originalSource && /([a-z2-7]{52}\.tsx?)$/.exec(entry.originalSource)?.[1]; if (!module || entry.originalLine === undefined) return undefined; return { file: path.join(cb.lay.cacheDir, module), line: entry.originalLine + 1 }; }