@timber-js/app 0.2.0-alpha.85 → 0.2.0-alpha.87

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -361,6 +361,35 @@ function formatRscDebugContext(components) {
361
361
  return lines.join("\n");
362
362
  }
363
363
  /**
364
+ * Dynamically compute the line offset that Vite's module runner adds
365
+ * when wrapping modules in an async function.
366
+ *
367
+ * Vite's `calculateOffsetOnce()` uses the same technique: create a new
368
+ * AsyncFunction, throw from line 1, and check where the engine reports
369
+ * the error. The difference between the reported line and 1 is the offset.
370
+ *
371
+ * This is engine-dependent (currently 2 on Node 18-22) and could change
372
+ * in future Node.js or V8 versions. Computing it at runtime ensures we
373
+ * always match the actual behavior.
374
+ */
375
+ var _cachedOffset = null;
376
+ function calculateModuleRunnerOffset() {
377
+ if (_cachedOffset !== null) return _cachedOffset;
378
+ try {
379
+ const AsyncFunction = async function() {}.constructor;
380
+ const src = new AsyncFunction("BODY").toString();
381
+ const bodyIndex = src.indexOf("BODY");
382
+ if (bodyIndex === -1) {
383
+ _cachedOffset = 2;
384
+ return _cachedOffset;
385
+ }
386
+ _cachedOffset = (src.slice(0, bodyIndex).match(/\n/g) || []).length;
387
+ } catch {
388
+ _cachedOffset = 2;
389
+ }
390
+ return _cachedOffset;
391
+ }
392
+ /**
364
393
  * Phases where the error originated in the RSC environment.
365
394
  * These use `server.environments.rsc.moduleGraph` for source-mapping.
366
395
  */
@@ -420,7 +449,8 @@ function rewriteStacktrace(stack, moduleGraph) {
420
449
  if (!id) return input;
421
450
  const rawSourceMap = moduleGraph.getModuleById(id)?.transformResult?.map;
422
451
  if (!rawSourceMap) return input;
423
- const origLine = Number(lineStr) - 2;
452
+ const OFFSET = calculateModuleRunnerOffset();
453
+ const origLine = Number(lineStr) - OFFSET;
424
454
  const origCol = Number(colStr) - 1;
425
455
  if (origLine <= 0 || origCol < 0) return input;
426
456
  let pos;
@@ -445,6 +475,19 @@ function rewriteStacktrace(stack, moduleGraph) {
445
475
  };
446
476
  }
447
477
  /**
478
+ * Source-map an error's stack trace using the Vite dev server's module graph.
479
+ *
480
+ * Exported so that the fallback error renderer (fallback-error.ts) can
481
+ * source-map errors before rendering the dev error page. Without this,
482
+ * the dev error page shows transpiled positions (e.g. page.tsx:1:1 instead
483
+ * of page.tsx:4:9).
484
+ *
485
+ * See TIM-811.
486
+ */
487
+ function fixErrorStacktrace(server, error, phase) {
488
+ fixStacktraceForEnvironment(server, error, phase ?? classifyErrorPhase(error, server.config.root));
489
+ }
490
+ /**
448
491
  * Send an error to Vite's browser overlay and log it to stderr.
449
492
  *
450
493
  * Uses `server.ssrFixStacktrace()` to map stack traces back to source,
@@ -1321,6 +1364,10 @@ function createTimberMiddleware(server, projectRoot) {
1321
1364
  if (typeof setHandler === "function") setHandler((error, _phase, debugComponents) => {
1322
1365
  sendErrorToOverlay(server, error, classifyErrorPhase(error, projectRoot), projectRoot, debugComponents);
1323
1366
  });
1367
+ const setSourceMap = rscModule.setDevSourceMapHandler;
1368
+ if (typeof setSourceMap === "function") setSourceMap((error) => {
1369
+ fixErrorStacktrace(server, error);
1370
+ });
1324
1371
  } catch (error) {
1325
1372
  if (error instanceof Error) {
1326
1373
  addTimberContext(error);