village-builder

VB-375: One loader with a hook for timed and replaced functions

Status: done (2026-11-21) Priority: 3 Size: M Arch: yardstick Phase: implement Design ref: none (code structure). docs/typescript-plan.md (P3); the survey in backlog/VB-055.md. Goal: An ES module’s exports cannot be reassigned, and ten files do that today to count or time a function: tools/perf.js (about 25 functions of 12 modules), tools/moving-profile.js, tools/route-bench.js, tools/wood.js, tests/spread.js, tests/plans.js (R.value), tests/lib/owner-caches.js (heirs.refreshRoles) and tests/lib/record.js, tests/lib/line.js and tests/bench.js (which patch data tables or a fixture loader, and may need nothing). Seven of them also read src/manifest.json and require each module in turn to wrap one right after it registers, so that the modules loaded later destructure the wrapper. Choose, build and use one hook for all of them, and put the loading itself in one place (src/load.js today, read by 120 files) so that the later tasks change one file. Three ways were weighed in the survey: (a) a registry in the source, probe(key, name, fn), an identity unless a hook is installed before load; (b) the build’s own instrumenting, an esbuild plugin that wraps the named exports in a bundle made for the tool; (c) a Node module-customisation hook. Proposed: (b), because tools/perf.js states that “nothing is added to src/”, production has no cost, and it needs no change to the sources when the modules become ES modules; (a) is the fallback if the plugin proves fragile. The first step of the task is to prototype (b) on tools/perf.js and tests/spread.js and say what it cost. Acceptance criteria: