refactor(h3d-carve-2): extract Three.js loader to src/three-loader.js

Move the D-section (~21 lines) from screen.js factory scope to its own module.

Exports:
- loadThree() — memoised import() with local-vendor→CDN fallback; same body verbatim
- T (live let-binding) — updated to the Three.js namespace on first resolution

screen.js gains: import { loadThree, T } from './src/three-loader.js'
screen.js loses: let T = null; let threeLoadPromise = null; function loadThree()
T and loadThree remain accessible to the IIFE via module-scope closure. The live
let-binding means the IIFE reads the populated T after loadThree() resolves
without any call-site changes.

Panel-controls vm test: strip regex extended to consume all consecutive import
lines; T: null + loadThree stubs added to sandbox context.

Class-killer tests (8 new — highway_3d_three_loader.test.js):
- loadThree exported (mutation: rename/remove → import fails)
- T exported as mutable let (mutation: const → T=mod throws TypeError)
- T=mod in both .then handlers (mutation: remove both → T stays null)
- memoisation guard !threeLoadPromise (mutation: remove → race + duplicate loads)
- CDN fallback .catch chain (mutation: remove → deploy failures unrecoverable)
- threeLoadPromise reset on failure (mutation: remove → no retry possible)
- screen.js imports loadThree+T (wiring confirmed)
- IIFE no longer declares local T or threeLoadPromise (shadow defeated)

All class-killer mutations confirmed distinguishable before commit.

Base run (5e401af): 180/180 pass.
Post-cut run: 188/188 pass.
Command: node --test tests/js/highway_3d*.test.js plugins/highway_3d/tests/*.test.js

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014uZ169yfoFYArXz962g7KW
This commit is contained in:
byrongamatos
2026-09-05 07:35:27 +02:00
co-authored by Claude Sonnet 4.6
parent 5e401afe87
commit 8ea123deaf
5 changed files with 137 additions and 25 deletions
+7 -4
View File
@@ -28,10 +28,10 @@ function loadHighway3dStatics() {
1,
'expected exactly one factory-registration anchor in screen.js',
);
// Since h3d-carve-1 screen.js starts with an ES module import statement.
// vm.runInContext does not support static import — strip the import line
// and provide stub implementations of the geometry exports in the sandbox.
const stripped = src.replace(/^import\s+\{[^}]+\}\s+from\s+['"][^'"]+['"];\s*\/\/[^\n]*\n/m, '');
// Since h3d-carve-1 screen.js starts with ES module import statements.
// vm.runInContext does not support static import — strip all leading import
// lines and provide stub implementations of the exports in the sandbox.
const stripped = src.replace(/^(import\s+\{[^}]+\}\s+from\s+['"][^'"]+['"];\s*\/\/[^\n]*\n)+/m, '');
const instrumented = stripped.replace(
ANCHOR,
`${ANCHOR}\n window.__h3dTestExports = { BG_DEFAULTS };`,
@@ -72,6 +72,9 @@ function loadHighway3dStatics() {
lowerBoundT: () => 0,
hwyFirstRelevantFrettedTime: () => null,
geoFretMid: (f, _uniform) => f * 0.1,
// h3d-carve-2: T and loadThree moved to src/three-loader.js.
T: null,
loadThree: () => Promise.resolve(),
};
vm.createContext(sandbox);
vm.runInContext(instrumented, sandbox, { filename: SCREEN_JS });