tempo-sdk 0.0.19 → 0.0.22
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/README.md +32 -2
- package/dist/canvas/editor.d.ts +9 -8
- package/dist/canvas/editor.js +8 -37
- package/dist/canvas/editor.js.map +1 -1
- package/dist/canvas/index.d.ts +115 -18
- package/dist/canvas/index.js +1580 -724
- package/dist/canvas/index.js.map +1 -1
- package/dist/canvas/interact-projection.d.ts +8 -0
- package/dist/canvas/interact-projection.js +5532 -0
- package/dist/canvas/interact-projection.js.map +1 -0
- package/dist/canvas/parser.js +1 -1
- package/dist/{chunk-P277UVZE.js → chunk-GBKYTBEE.js} +1 -3
- package/dist/{chunk-P277UVZE.js.map → chunk-GBKYTBEE.js.map} +1 -1
- package/dist/chunk-QXB32HG4.js +11 -0
- package/dist/chunk-QXB32HG4.js.map +1 -0
- package/dist/chunk-ZT2REPH4.js +19 -0
- package/dist/chunk-ZT2REPH4.js.map +1 -0
- package/dist/index.d.ts +12 -4
- package/dist/index.js +962 -27
- package/dist/index.js.map +1 -1
- package/dist/{page-xi1aI5a7.d.ts → page-BUbHO0BI.d.ts} +2 -0
- package/package.json +7 -2
package/dist/index.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TEMPO_INTERACT_FRAME_NAME
|
|
3
|
+
} from "./chunk-QXB32HG4.js";
|
|
4
|
+
|
|
1
5
|
// index.ts
|
|
2
|
-
import {
|
|
3
|
-
|
|
6
|
+
import {
|
|
7
|
+
readFileSync,
|
|
8
|
+
realpathSync as realpathSync2,
|
|
9
|
+
statSync as statSync2,
|
|
10
|
+
watch as watchFileSystem
|
|
11
|
+
} from "fs";
|
|
12
|
+
import { createRequire } from "module";
|
|
13
|
+
import { dirname, isAbsolute, join, relative as relative2, resolve } from "path";
|
|
14
|
+
import { createFilter } from "@rollup/pluginutils";
|
|
4
15
|
|
|
5
16
|
// ../../modules/annotation/annotate-file.ts
|
|
6
17
|
import { parseSync as parseSync2 } from "oxc-parser";
|
|
@@ -142,9 +153,6 @@ function toHex8(n) {
|
|
|
142
153
|
return (n >>> 0).toString(16).padStart(8, "0");
|
|
143
154
|
}
|
|
144
155
|
|
|
145
|
-
// index.ts
|
|
146
|
-
import { createFilter } from "@rollup/pluginutils";
|
|
147
|
-
|
|
148
156
|
// annotation-strategy.ts
|
|
149
157
|
import { realpathSync, statSync } from "fs";
|
|
150
158
|
import { relative } from "path";
|
|
@@ -217,13 +225,271 @@ function computeTempoAnnotatedProps(params) {
|
|
|
217
225
|
return userProps;
|
|
218
226
|
}
|
|
219
227
|
|
|
228
|
+
// canvas-host-registration-contract.ts
|
|
229
|
+
var CANVAS_HOST_REGISTRATION_ENDPOINT = "/__tempo/canvas-host-registration";
|
|
230
|
+
var CANVAS_HOST_REGISTRATION_CAPABILITY_HEADER = "x-tempo-canvas-host-registration";
|
|
231
|
+
function normalizeCanvasRegistrationPath(canvasPath) {
|
|
232
|
+
return canvasPath.trim().replace(/\\/g, "/").replace(/^\.\/+/, "").replace(/^\/+/, "");
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// default-export-identity.ts
|
|
236
|
+
var FUNCTION_DEFAULT_RE = /^(?:async\s+)?function(?:\s*\*)?(?:\s+([A-Za-z_$][\w$]*))?\s*\(/;
|
|
237
|
+
var CLASS_DEFAULT_RE = /^(?:abstract\s+)?class(?:\s+([A-Za-z_$][\w$]*))?(?=[\s{])/;
|
|
238
|
+
var ARROW_DEFAULT_RE = /^(?:async\s+)?(?:\([^()]*\)|[A-Za-z_$][\w$]*)\s*=>/;
|
|
239
|
+
var IDENTIFIER_DEFAULT_RE = /^([A-Za-z_$][\w$]*)[ \t]*(?:;|\r?\n|$)/;
|
|
240
|
+
function readDefaultExportIdentity(source) {
|
|
241
|
+
const start = /\bexport\s+default\s+/.exec(source);
|
|
242
|
+
if (!start) return null;
|
|
243
|
+
const tail = source.slice(start.index + start[0].length);
|
|
244
|
+
const fn = FUNCTION_DEFAULT_RE.exec(tail);
|
|
245
|
+
if (fn) return { kind: "function", name: fn[1] ?? null };
|
|
246
|
+
const cls = CLASS_DEFAULT_RE.exec(tail);
|
|
247
|
+
if (cls) return { kind: "class", name: cls[1] ?? null };
|
|
248
|
+
if (ARROW_DEFAULT_RE.test(tail)) return { kind: "function", name: null };
|
|
249
|
+
const identifier = IDENTIFIER_DEFAULT_RE.exec(tail);
|
|
250
|
+
if (identifier) return { kind: "identifier", name: identifier[1] };
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
function isDefaultExportIdentityRewrite(before, after) {
|
|
254
|
+
if (!before) return false;
|
|
255
|
+
if (before.name === null) return true;
|
|
256
|
+
if (!after) return false;
|
|
257
|
+
if (before.name === after.name) return false;
|
|
258
|
+
return isComponentShaped(before) && isComponentShaped(after);
|
|
259
|
+
}
|
|
260
|
+
function isComponentShaped(identity) {
|
|
261
|
+
return identity.name === null || /^[A-Z]/.test(identity.name);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// hmr-generation-fence.ts
|
|
265
|
+
function generationIdentity(update) {
|
|
266
|
+
const type = typeof update.type === "string" ? update.type : null;
|
|
267
|
+
const acceptedPath = typeof update.acceptedPath === "string" ? update.acceptedPath : typeof update.path === "string" ? update.path : null;
|
|
268
|
+
if (!type || !acceptedPath) return null;
|
|
269
|
+
return `${type}\0${acceptedPath}`;
|
|
270
|
+
}
|
|
271
|
+
function createHmrGenerationFence() {
|
|
272
|
+
const newestTimestampByIdentity = /* @__PURE__ */ new Map();
|
|
273
|
+
return {
|
|
274
|
+
filter(updates) {
|
|
275
|
+
const accepted = [];
|
|
276
|
+
for (const update of updates) {
|
|
277
|
+
const identity = generationIdentity(update);
|
|
278
|
+
const timestamp = update.timestamp;
|
|
279
|
+
if (identity === null || typeof timestamp !== "number" || !Number.isFinite(timestamp)) {
|
|
280
|
+
accepted.push(update);
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const newestTimestamp = newestTimestampByIdentity.get(identity);
|
|
284
|
+
if (newestTimestamp !== void 0 && timestamp < newestTimestamp) {
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (newestTimestamp === void 0 || timestamp > newestTimestamp) {
|
|
288
|
+
newestTimestampByIdentity.set(identity, timestamp);
|
|
289
|
+
}
|
|
290
|
+
accepted.push(update);
|
|
291
|
+
}
|
|
292
|
+
return accepted;
|
|
293
|
+
},
|
|
294
|
+
clear() {
|
|
295
|
+
newestTimestampByIdentity.clear();
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// missed-change-sweep.ts
|
|
301
|
+
var MISSED_CHANGE_WATCHER_GRACE_MS = 1e3;
|
|
302
|
+
var MISSED_CHANGE_MAX_EMITS_PER_REVISION = 3;
|
|
303
|
+
function createMissedChangeSweep(args) {
|
|
304
|
+
const now = args.now ?? Date.now;
|
|
305
|
+
const graceMs = args.watcherGraceMs ?? MISSED_CHANGE_WATCHER_GRACE_MS;
|
|
306
|
+
const maxEmits = args.maxEmitsPerRevision ?? MISSED_CHANGE_MAX_EMITS_PER_REVISION;
|
|
307
|
+
const lastSeenMtimeMs = /* @__PURE__ */ new Map();
|
|
308
|
+
const pending = /* @__PURE__ */ new Map();
|
|
309
|
+
return {
|
|
310
|
+
async sweep() {
|
|
311
|
+
const emitted = [];
|
|
312
|
+
for (const [file, modules] of args.listServedFiles()) {
|
|
313
|
+
const mtimeMs = await args.statMtimeMs(file);
|
|
314
|
+
if (mtimeMs === null) {
|
|
315
|
+
pending.delete(file);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
let previous = lastSeenMtimeMs.get(file);
|
|
319
|
+
if (previous === void 0) {
|
|
320
|
+
const coveredMtimeMs = args.lastHotUpdateMtimeMs?.(file);
|
|
321
|
+
if (coveredMtimeMs === void 0 || coveredMtimeMs >= mtimeMs) {
|
|
322
|
+
lastSeenMtimeMs.set(file, mtimeMs);
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
previous = coveredMtimeMs;
|
|
326
|
+
lastSeenMtimeMs.set(file, coveredMtimeMs);
|
|
327
|
+
}
|
|
328
|
+
if (mtimeMs > previous) {
|
|
329
|
+
if (now() - mtimeMs < graceMs) continue;
|
|
330
|
+
lastSeenMtimeMs.set(file, mtimeMs);
|
|
331
|
+
} else if (mtimeMs < previous) {
|
|
332
|
+
lastSeenMtimeMs.set(file, mtimeMs);
|
|
333
|
+
}
|
|
334
|
+
const entry = pending.get(file);
|
|
335
|
+
if (mtimeMs <= previous && entry === void 0) continue;
|
|
336
|
+
const hotUpdateMtimeMs = args.lastHotUpdateMtimeMs?.(file);
|
|
337
|
+
let delivered;
|
|
338
|
+
if (hotUpdateMtimeMs !== void 0) {
|
|
339
|
+
delivered = hotUpdateMtimeMs >= mtimeMs;
|
|
340
|
+
} else {
|
|
341
|
+
delivered = false;
|
|
342
|
+
for (const module of modules) {
|
|
343
|
+
if ((module.lastInvalidationTimestamp ?? 0) >= mtimeMs) {
|
|
344
|
+
delivered = true;
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (delivered) {
|
|
350
|
+
pending.delete(file);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
const revision = entry !== void 0 && entry.mtimeMs === mtimeMs ? entry : { mtimeMs, emits: 0 };
|
|
354
|
+
pending.set(file, revision);
|
|
355
|
+
if (revision.emits >= maxEmits) continue;
|
|
356
|
+
revision.emits += 1;
|
|
357
|
+
args.emitChange(file);
|
|
358
|
+
emitted.push(file);
|
|
359
|
+
}
|
|
360
|
+
return emitted;
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// rapid-hot-update-replay.ts
|
|
366
|
+
var RAPID_HOT_UPDATE_QUIET_MS = 100;
|
|
367
|
+
function createRapidHotUpdateReplay(options) {
|
|
368
|
+
const quietMs = options?.quietMs ?? RAPID_HOT_UPDATE_QUIET_MS;
|
|
369
|
+
const schedule = options?.schedule ?? setTimeout;
|
|
370
|
+
const cancel = options?.cancel ?? clearTimeout;
|
|
371
|
+
const bursts = /* @__PURE__ */ new Map();
|
|
372
|
+
return {
|
|
373
|
+
observe(file, replay) {
|
|
374
|
+
const previous = bursts.get(file);
|
|
375
|
+
if (previous?.timer) cancel(previous.timer);
|
|
376
|
+
const burst = {
|
|
377
|
+
eventCount: (previous?.eventCount ?? 0) + 1,
|
|
378
|
+
replay
|
|
379
|
+
};
|
|
380
|
+
burst.timer = schedule(() => {
|
|
381
|
+
if (bursts.get(file) !== burst) return;
|
|
382
|
+
bursts.delete(file);
|
|
383
|
+
if (burst.eventCount < 2) return;
|
|
384
|
+
const replayAllowance = { eventCount: -1, replay: burst.replay };
|
|
385
|
+
replayAllowance.timer = schedule(() => {
|
|
386
|
+
if (bursts.get(file) === replayAllowance) bursts.delete(file);
|
|
387
|
+
}, quietMs);
|
|
388
|
+
bursts.set(file, replayAllowance);
|
|
389
|
+
burst.replay();
|
|
390
|
+
}, quietMs);
|
|
391
|
+
bursts.set(file, burst);
|
|
392
|
+
},
|
|
393
|
+
clear() {
|
|
394
|
+
for (const burst of bursts.values()) {
|
|
395
|
+
if (burst.timer) cancel(burst.timer);
|
|
396
|
+
}
|
|
397
|
+
bursts.clear();
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
220
402
|
// index.ts
|
|
221
403
|
var DEFAULT_INCLUDE = ["**/*.tsx", "**/*.jsx"];
|
|
222
404
|
var DEFAULT_EXCLUDE = ["**/node_modules/**"];
|
|
223
405
|
var ROOT_META_NAME = "tempo-project-root";
|
|
406
|
+
var SOURCE_IDENTITY_ROOT_META_NAME = "tempo-source-identity-root";
|
|
407
|
+
var INTERACT_PROJECTION_CLIENT_PATH = "/@tempo/interact-projection";
|
|
408
|
+
var INTERACT_PROJECTION_CLIENT_ID = "\0tempo:interact-projection";
|
|
409
|
+
var INTERACT_PROJECTION_BOOTSTRAP = `
|
|
410
|
+
(function() {
|
|
411
|
+
if (window.parent === window || (window.name !== ${JSON.stringify(TEMPO_INTERACT_FRAME_NAME)} && !window.name.startsWith(${JSON.stringify(`${TEMPO_INTERACT_FRAME_NAME}:`)}))) return;
|
|
412
|
+
window.__tempoInteractProjectionOwner = "vite";
|
|
413
|
+
var roots = new Map();
|
|
414
|
+
var existing = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
|
|
415
|
+
if (existing) {
|
|
416
|
+
if (existing.__tempoPendingFiberRoots) return;
|
|
417
|
+
existing.__tempoPendingFiberRoots = roots;
|
|
418
|
+
var originalCommit = existing.onCommitFiberRoot && existing.onCommitFiberRoot.bind(existing);
|
|
419
|
+
existing.onCommitFiberRoot = function(rendererId, root) {
|
|
420
|
+
if (existing.__tempoPendingFiberRoots === roots) roots.set(rendererId, root);
|
|
421
|
+
if (originalCommit) originalCommit.apply(null, arguments);
|
|
422
|
+
};
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {
|
|
426
|
+
supportsFiber: true,
|
|
427
|
+
renderers: new Map(),
|
|
428
|
+
__tempoPendingFiberRoots: roots,
|
|
429
|
+
inject: function(renderer) {
|
|
430
|
+
var id = this.renderers.size + 1;
|
|
431
|
+
this.renderers.set(id, renderer);
|
|
432
|
+
return id;
|
|
433
|
+
},
|
|
434
|
+
onCommitFiberRoot: function(rendererId, root) {
|
|
435
|
+
if (this.__tempoPendingFiberRoots === roots) roots.set(rendererId, root);
|
|
436
|
+
},
|
|
437
|
+
onCommitFiberUnmount: function() {},
|
|
438
|
+
checkDCE: function() {},
|
|
439
|
+
onScheduleFiberRoot: function() {}
|
|
440
|
+
};
|
|
441
|
+
})();`;
|
|
224
442
|
function cleanModuleId(id) {
|
|
225
443
|
return id.replace(/[?#].*$/, "");
|
|
226
444
|
}
|
|
445
|
+
function fsPathFromModuleId(id) {
|
|
446
|
+
return cleanModuleId(id).replace(/^\/@fs\//, "/");
|
|
447
|
+
}
|
|
448
|
+
function isCanvasHostRequest(requestPath) {
|
|
449
|
+
const url = new URL(requestPath, "http://tempo.local");
|
|
450
|
+
return url.pathname.replace(/\/+$/, "") === "/tempo-host";
|
|
451
|
+
}
|
|
452
|
+
function isDeclarativeCanvasHostRequest(requestPath) {
|
|
453
|
+
if (!isCanvasHostRequest(requestPath)) return false;
|
|
454
|
+
const params = new URL(requestPath, "http://tempo.local").searchParams;
|
|
455
|
+
const canvasPath = params.get("canvasPath")?.trim() ?? params.get("pageFile")?.trim() ?? params.get("importPath")?.trim();
|
|
456
|
+
const storyboardId = params.get("storyboardId")?.trim() ?? params.get("exportName")?.trim() ?? params.get("importName")?.trim();
|
|
457
|
+
return !canvasPath || !storyboardId;
|
|
458
|
+
}
|
|
459
|
+
var MAX_UNRESOLVED_IMPORTS = 256;
|
|
460
|
+
var RELATIVE_IMPORT_RESOLVE_EXTENSIONS = [
|
|
461
|
+
".mjs",
|
|
462
|
+
".js",
|
|
463
|
+
".mts",
|
|
464
|
+
".ts",
|
|
465
|
+
".jsx",
|
|
466
|
+
".tsx",
|
|
467
|
+
".json"
|
|
468
|
+
];
|
|
469
|
+
var UNRESOLVED_IMPORT_WATCH_REARM_MS = 1e3;
|
|
470
|
+
function isRelativeImportSpecifier(specifier) {
|
|
471
|
+
return specifier.startsWith("./") || specifier.startsWith("../");
|
|
472
|
+
}
|
|
473
|
+
function barePackageName(specifier) {
|
|
474
|
+
if (!specifier || specifier.startsWith(".") || specifier.startsWith("/") || specifier.startsWith("\0") || specifier.startsWith("#") || specifier.includes(":")) {
|
|
475
|
+
return void 0;
|
|
476
|
+
}
|
|
477
|
+
const segments = specifier.split("/");
|
|
478
|
+
const packageName = specifier.startsWith("@") ? segments.length >= 2 && segments[0] !== "@" && segments[1] ? `${segments[0]}/${segments[1]}` : void 0 : segments[0];
|
|
479
|
+
return packageName && /^@?[A-Za-z0-9_][A-Za-z0-9._/-]*$/.test(packageName) ? packageName : void 0;
|
|
480
|
+
}
|
|
481
|
+
function resolvedIdMatchesFile(id, file) {
|
|
482
|
+
const resolvedFile = fsPathFromModuleId(id);
|
|
483
|
+
try {
|
|
484
|
+
return realpathSync2.native(resolvedFile) === realpathSync2.native(file);
|
|
485
|
+
} catch {
|
|
486
|
+
return resolvedFile.replace(/\\/g, "/") === file.replace(/\\/g, "/");
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
function moduleIdsMatch(left, right) {
|
|
490
|
+
const normalize = (value) => cleanModuleId(value).replace(/^file:\/\//, "").replace(/^\/@fs\//, "/").replace(/\\/g, "/").replace(/^\/+/, "");
|
|
491
|
+
return normalize(left) === normalize(right);
|
|
492
|
+
}
|
|
227
493
|
function resolveCanonicalRoot(root) {
|
|
228
494
|
try {
|
|
229
495
|
return realpathSync2.native(root);
|
|
@@ -234,7 +500,13 @@ function resolveCanonicalRoot(root) {
|
|
|
234
500
|
function hasRootMetaTag(html) {
|
|
235
501
|
return /<meta[^>]+name=["']tempo-project-root["'][^>]*>/i.test(html);
|
|
236
502
|
}
|
|
237
|
-
function
|
|
503
|
+
function hasSourceIdentityRootMetaTag(html) {
|
|
504
|
+
return /<meta[^>]+name=["']tempo-source-identity-root["'][^>]*>/i.test(html);
|
|
505
|
+
}
|
|
506
|
+
function isSameError(left, right) {
|
|
507
|
+
return left.type === right.type && left.message === right.message && left.loc?.line === right.loc?.line && left.loc?.column === right.loc?.column;
|
|
508
|
+
}
|
|
509
|
+
function createErrorServer(onErrorFile) {
|
|
238
510
|
const errorsByFile = /* @__PURE__ */ new Map();
|
|
239
511
|
function getMostRecentError() {
|
|
240
512
|
if (errorsByFile.size === 0) return null;
|
|
@@ -254,9 +526,15 @@ function createErrorServer() {
|
|
|
254
526
|
get currentError() {
|
|
255
527
|
return getMostRecentError();
|
|
256
528
|
},
|
|
529
|
+
get errorFiles() {
|
|
530
|
+
return [...errorsByFile.keys()].filter((file) => file !== "unknown");
|
|
531
|
+
},
|
|
257
532
|
clients: /* @__PURE__ */ new Set(),
|
|
258
533
|
setError(error) {
|
|
259
534
|
const key = error.file ?? error.loc?.file ?? "unknown";
|
|
535
|
+
const recorded = errorsByFile.get(key);
|
|
536
|
+
if (recorded && isSameError(recorded, error)) return;
|
|
537
|
+
if (key !== "unknown" && !recorded) onErrorFile?.(key);
|
|
260
538
|
errorsByFile.delete(key);
|
|
261
539
|
errorsByFile.set(key, error);
|
|
262
540
|
broadcastCurrentState();
|
|
@@ -265,7 +543,7 @@ function createErrorServer() {
|
|
|
265
543
|
if (errorsByFile.size === 0) return;
|
|
266
544
|
let changed = false;
|
|
267
545
|
for (const errorKey of [...errorsByFile.keys()]) {
|
|
268
|
-
if (errorKey
|
|
546
|
+
if (errorKey !== "unknown" && updatedPaths.some((path) => moduleIdsMatch(errorKey, path))) {
|
|
269
547
|
errorsByFile.delete(errorKey);
|
|
270
548
|
changed = true;
|
|
271
549
|
}
|
|
@@ -280,16 +558,109 @@ function createErrorServer() {
|
|
|
280
558
|
return server;
|
|
281
559
|
}
|
|
282
560
|
function readBody(req) {
|
|
283
|
-
return new Promise((
|
|
561
|
+
return new Promise((resolve2, reject) => {
|
|
284
562
|
const chunks = [];
|
|
285
563
|
req.on("data", (chunk) => chunks.push(chunk));
|
|
286
|
-
req.on("end", () =>
|
|
564
|
+
req.on("end", () => resolve2(Buffer.concat(chunks).toString()));
|
|
287
565
|
req.on("error", reject);
|
|
288
566
|
});
|
|
289
567
|
}
|
|
290
|
-
|
|
568
|
+
var CANVAS_HOST_WRAPPER_URL = "/.tempo/tempo-host";
|
|
569
|
+
function readGeneratedCanvasPaths(source) {
|
|
570
|
+
const paths = /* @__PURE__ */ new Set();
|
|
571
|
+
const canvasPathPattern = /\bcanvasPath:\s*("(?:\\.|[^"\\])*")/g;
|
|
572
|
+
for (const match of source.matchAll(canvasPathPattern)) {
|
|
573
|
+
try {
|
|
574
|
+
const parsed = JSON.parse(match[1] ?? "");
|
|
575
|
+
if (typeof parsed !== "string") continue;
|
|
576
|
+
const normalized = normalizeCanvasRegistrationPath(parsed);
|
|
577
|
+
if (normalized) paths.add(normalized);
|
|
578
|
+
} catch {
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return [...paths];
|
|
582
|
+
}
|
|
583
|
+
async function synchronizeCanvasHostRegistrationTransform(server, canvasPath) {
|
|
584
|
+
const wrapperPath = resolve(server.config.root, ".tempo", "tempo-host.tsx");
|
|
585
|
+
let wrapperSource;
|
|
586
|
+
try {
|
|
587
|
+
wrapperSource = readFileSync(wrapperPath, "utf8");
|
|
588
|
+
} catch (error) {
|
|
589
|
+
throw new Error(
|
|
590
|
+
`Canvas host wrapper is unavailable at ${wrapperPath}: ${error instanceof Error ? error.message : String(error)}`
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
const registeredCanvasPaths = readGeneratedCanvasPaths(wrapperSource);
|
|
594
|
+
if (!registeredCanvasPaths.includes(canvasPath)) {
|
|
595
|
+
throw Object.assign(
|
|
596
|
+
new Error(
|
|
597
|
+
`Generated canvas host wrapper does not contain ${JSON.stringify(
|
|
598
|
+
canvasPath
|
|
599
|
+
)}`
|
|
600
|
+
),
|
|
601
|
+
{ statusCode: 409 }
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
const canonicalWrapperPath = realpathSync2(wrapperPath);
|
|
605
|
+
await retransformCanvasHostWrapper(
|
|
606
|
+
server.environments?.client ?? server,
|
|
607
|
+
canonicalWrapperPath,
|
|
608
|
+
wrapperPath
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
async function retransformCanvasHostWrapper(transformer, canonicalWrapperPath, wrapperPath) {
|
|
612
|
+
const modules = transformer.moduleGraph.getModulesByFile(canonicalWrapperPath) ?? transformer.moduleGraph.getModulesByFile(wrapperPath);
|
|
613
|
+
for (const module of modules ?? []) {
|
|
614
|
+
transformer.moduleGraph.invalidateModule(module);
|
|
615
|
+
}
|
|
616
|
+
const result = await transformer.transformRequest(CANVAS_HOST_WRAPPER_URL);
|
|
617
|
+
if (!result) {
|
|
618
|
+
throw new Error(
|
|
619
|
+
`Vite did not transform ${CANVAS_HOST_WRAPPER_URL} after registration`
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
function writeCanvasRegistrationResponseHeaders(res) {
|
|
624
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
625
|
+
res.setHeader(CANVAS_HOST_REGISTRATION_CAPABILITY_HEADER, "1");
|
|
626
|
+
}
|
|
627
|
+
function handleTempoMiddleware(errorServer, viteServer, req, res, next) {
|
|
291
628
|
const url = req.url;
|
|
292
629
|
if (!url?.startsWith("/__tempo/")) return next();
|
|
630
|
+
if (url === CANVAS_HOST_REGISTRATION_ENDPOINT && req.method === "OPTIONS") {
|
|
631
|
+
writeCanvasRegistrationResponseHeaders(res);
|
|
632
|
+
res.writeHead(204, {
|
|
633
|
+
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
634
|
+
"Access-Control-Allow-Headers": "content-type"
|
|
635
|
+
});
|
|
636
|
+
res.end();
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
if (url === CANVAS_HOST_REGISTRATION_ENDPOINT && req.method === "POST") {
|
|
640
|
+
writeCanvasRegistrationResponseHeaders(res);
|
|
641
|
+
void readBody(req).then(async (body) => {
|
|
642
|
+
const parsed = JSON.parse(body);
|
|
643
|
+
const canvasPath = typeof parsed.canvasPath === "string" ? normalizeCanvasRegistrationPath(parsed.canvasPath) : "";
|
|
644
|
+
if (!canvasPath) {
|
|
645
|
+
throw Object.assign(
|
|
646
|
+
new Error("canvasPath must be a non-empty string"),
|
|
647
|
+
{ statusCode: 400 }
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
await synchronizeCanvasHostRegistrationTransform(
|
|
651
|
+
viteServer,
|
|
652
|
+
canvasPath
|
|
653
|
+
);
|
|
654
|
+
res.statusCode = 200;
|
|
655
|
+
res.setHeader("Content-Type", "application/json");
|
|
656
|
+
res.end(JSON.stringify({ canvasPath }));
|
|
657
|
+
}).catch((error) => {
|
|
658
|
+
res.statusCode = typeof error === "object" && error !== null && "statusCode" in error && typeof error.statusCode === "number" ? error.statusCode : 500;
|
|
659
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
660
|
+
res.end(error instanceof Error ? error.message : String(error));
|
|
661
|
+
});
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
293
664
|
if (url === "/__tempo/report-error" && req.method === "POST") {
|
|
294
665
|
readBody(req).then((body) => {
|
|
295
666
|
try {
|
|
@@ -334,29 +705,491 @@ function handleErrorMiddleware(errorServer, req, res, next) {
|
|
|
334
705
|
}
|
|
335
706
|
next();
|
|
336
707
|
}
|
|
708
|
+
var MISSED_CHANGE_SWEEP_INTERVAL_MS = 3e3;
|
|
709
|
+
var COMPILE_ERROR_POLL_MAX_INTERVAL_MS = 3e4;
|
|
710
|
+
function installMissedChangeSweep(server, listErrorFiles, lastHotUpdateMtimeMs, pollCompileErrorRecovery) {
|
|
711
|
+
const sweep = createMissedChangeSweep({
|
|
712
|
+
listServedFiles: () => {
|
|
713
|
+
const files = new Map(
|
|
714
|
+
server.moduleGraph?.fileToModulesMap ?? /* @__PURE__ */ new Map()
|
|
715
|
+
);
|
|
716
|
+
const errorFiles = listErrorFiles();
|
|
717
|
+
for (const errorFile of errorFiles) {
|
|
718
|
+
const cleanFile = fsPathFromModuleId(errorFile);
|
|
719
|
+
const file = isAbsolute(cleanFile) ? cleanFile : resolve(server.config.root, cleanFile);
|
|
720
|
+
if (!files.has(file)) files.set(file, []);
|
|
721
|
+
}
|
|
722
|
+
return files.entries();
|
|
723
|
+
},
|
|
724
|
+
statMtimeMs: async (file) => {
|
|
725
|
+
try {
|
|
726
|
+
return statSync2(file).mtimeMs;
|
|
727
|
+
} catch {
|
|
728
|
+
return null;
|
|
729
|
+
}
|
|
730
|
+
},
|
|
731
|
+
emitChange: (file) => {
|
|
732
|
+
server.config?.logger?.info?.(
|
|
733
|
+
`[tempo] re-emitting lost watcher change: ${file}`,
|
|
734
|
+
{ timestamp: true }
|
|
735
|
+
);
|
|
736
|
+
server.watcher?.emit?.("change", file);
|
|
737
|
+
},
|
|
738
|
+
lastHotUpdateMtimeMs
|
|
739
|
+
});
|
|
740
|
+
let passRunning = false;
|
|
741
|
+
const timer = setInterval(() => {
|
|
742
|
+
if (passRunning) return;
|
|
743
|
+
passRunning = true;
|
|
744
|
+
void sweep.sweep().then(() => pollCompileErrorRecovery()).catch(() => {
|
|
745
|
+
}).finally(() => {
|
|
746
|
+
passRunning = false;
|
|
747
|
+
});
|
|
748
|
+
}, MISSED_CHANGE_SWEEP_INTERVAL_MS);
|
|
749
|
+
timer.unref?.();
|
|
750
|
+
server.httpServer?.once("close", () => clearInterval(timer));
|
|
751
|
+
}
|
|
337
752
|
function tempoVitePlugin(options = {}) {
|
|
338
753
|
const enabled = !!process.env.TEMPO;
|
|
339
754
|
const include = options.include === void 0 ? DEFAULT_INCLUDE : options.include;
|
|
340
755
|
const exclude = options.exclude === void 0 ? DEFAULT_EXCLUDE : options.exclude;
|
|
341
756
|
const filter = createFilter(include, exclude);
|
|
342
757
|
let canonicalRoot = process.cwd();
|
|
758
|
+
let sourceIdentityRoot = canonicalRoot;
|
|
343
759
|
let command = "serve";
|
|
344
|
-
const errorServer = createErrorServer(
|
|
760
|
+
const errorServer = createErrorServer(
|
|
761
|
+
(file) => pinErroneousRevisionCoverage(file)
|
|
762
|
+
);
|
|
763
|
+
const unresolvedImports = /* @__PURE__ */ new Map();
|
|
764
|
+
let devServer;
|
|
765
|
+
const unresolvedImportWatchers = /* @__PURE__ */ new Map();
|
|
766
|
+
let unresolvedImportResolutionScheduled = false;
|
|
767
|
+
let unresolvedImportResolutionRunning = false;
|
|
768
|
+
let rerunUnresolvedImportResolution = false;
|
|
769
|
+
let unresolvedImportWatchRearmTimer = null;
|
|
770
|
+
const processedUpdatePayloads = /* @__PURE__ */ new WeakSet();
|
|
771
|
+
const hmrGenerationFence = createHmrGenerationFence();
|
|
772
|
+
const rapidHotUpdateReplay = createRapidHotUpdateReplay();
|
|
773
|
+
const hotUpdateMtimesMs = /* @__PURE__ */ new Map();
|
|
774
|
+
const normalizedFileKey = (file) => file.replace(/\\/g, "/");
|
|
775
|
+
const recordHotUpdateCoverage = (file) => {
|
|
776
|
+
const cleanFile = fsPathFromModuleId(file);
|
|
777
|
+
const key = normalizedFileKey(cleanFile);
|
|
778
|
+
try {
|
|
779
|
+
hotUpdateMtimesMs.set(key, statSync2(cleanFile).mtimeMs);
|
|
780
|
+
} catch {
|
|
781
|
+
hotUpdateMtimesMs.delete(key);
|
|
782
|
+
}
|
|
783
|
+
};
|
|
784
|
+
const pinErroneousRevisionCoverage = (file) => {
|
|
785
|
+
recordHotUpdateCoverage(file);
|
|
786
|
+
};
|
|
787
|
+
const hasActiveCompileError = (file) => errorServer.errorFiles.some((errorFile) => moduleIdsMatch(errorFile, file));
|
|
788
|
+
const recoverActiveCompileError = async (server, file) => {
|
|
789
|
+
if (!hasActiveCompileError(file)) return false;
|
|
790
|
+
const cleanFile = fsPathFromModuleId(file);
|
|
791
|
+
const requestId = `/@fs/${cleanFile.replace(/^\/+/, "")}`;
|
|
792
|
+
const clientEnvironment = server.environments?.client;
|
|
793
|
+
try {
|
|
794
|
+
const result = clientEnvironment ? await clientEnvironment.transformRequest(requestId) : await server.transformRequest(requestId);
|
|
795
|
+
if (!result) return false;
|
|
796
|
+
} catch {
|
|
797
|
+
return false;
|
|
798
|
+
}
|
|
799
|
+
errorServer.clearErrorForModules([file, cleanFile, requestId]);
|
|
800
|
+
server.config.logger.info(
|
|
801
|
+
`[tempo] recovered compile-error module after validated transform: ${cleanFile}`,
|
|
802
|
+
{ timestamp: true }
|
|
803
|
+
);
|
|
804
|
+
(clientEnvironment?.hot ?? server.ws).send({
|
|
805
|
+
type: "full-reload",
|
|
806
|
+
path: "*"
|
|
807
|
+
});
|
|
808
|
+
return true;
|
|
809
|
+
};
|
|
810
|
+
const compileErrorPollSchedule = /* @__PURE__ */ new Map();
|
|
811
|
+
const pollCompileErrorRecovery = async (server) => {
|
|
812
|
+
const activeFiles = new Map(
|
|
813
|
+
errorServer.errorFiles.map((file) => [
|
|
814
|
+
normalizedFileKey(fsPathFromModuleId(file)),
|
|
815
|
+
file
|
|
816
|
+
])
|
|
817
|
+
);
|
|
818
|
+
for (const key of [...compileErrorPollSchedule.keys()]) {
|
|
819
|
+
if (!activeFiles.has(key)) compileErrorPollSchedule.delete(key);
|
|
820
|
+
}
|
|
821
|
+
const nowMs = Date.now();
|
|
822
|
+
for (const [key, file] of activeFiles) {
|
|
823
|
+
const schedule = compileErrorPollSchedule.get(key);
|
|
824
|
+
if (schedule && nowMs < schedule.nextAttemptAtMs) continue;
|
|
825
|
+
if (await recoverActiveCompileError(server, file)) {
|
|
826
|
+
compileErrorPollSchedule.delete(key);
|
|
827
|
+
continue;
|
|
828
|
+
}
|
|
829
|
+
const intervalMs = schedule?.intervalMs ?? MISSED_CHANGE_SWEEP_INTERVAL_MS;
|
|
830
|
+
compileErrorPollSchedule.set(key, {
|
|
831
|
+
nextAttemptAtMs: nowMs + intervalMs,
|
|
832
|
+
intervalMs: Math.min(
|
|
833
|
+
intervalMs * 2,
|
|
834
|
+
COMPILE_ERROR_POLL_MAX_INTERVAL_MS
|
|
835
|
+
)
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
};
|
|
839
|
+
const defaultExportIdentityByFile = /* @__PURE__ */ new Map();
|
|
840
|
+
const canonicalIdentityKey = (idOrFile) => {
|
|
841
|
+
const file = fsPathFromModuleId(idOrFile);
|
|
842
|
+
try {
|
|
843
|
+
return realpathSync2.native(file);
|
|
844
|
+
} catch {
|
|
845
|
+
return file.replace(/\\/g, "/");
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
const repairDefaultExportIdentityRewrite = (file) => {
|
|
849
|
+
const key = canonicalIdentityKey(file);
|
|
850
|
+
const before = defaultExportIdentityByFile.get(key);
|
|
851
|
+
if (before === void 0) return false;
|
|
852
|
+
let source;
|
|
853
|
+
try {
|
|
854
|
+
source = readFileSync(file, "utf8");
|
|
855
|
+
} catch {
|
|
856
|
+
return false;
|
|
857
|
+
}
|
|
858
|
+
const after = readDefaultExportIdentity(source);
|
|
859
|
+
defaultExportIdentityByFile.set(key, after);
|
|
860
|
+
if (!isDefaultExportIdentityRewrite(before, after)) return false;
|
|
861
|
+
devServer?.ws.send({ type: "full-reload", path: "*" });
|
|
862
|
+
return true;
|
|
863
|
+
};
|
|
864
|
+
const rememberUnresolvedImport = (specifier, importer, packageName) => {
|
|
865
|
+
const key = `${importer}\0${specifier}`;
|
|
866
|
+
unresolvedImports.delete(key);
|
|
867
|
+
unresolvedImports.set(key, { specifier, importer, packageName });
|
|
868
|
+
while (unresolvedImports.size > MAX_UNRESOLVED_IMPORTS) {
|
|
869
|
+
const oldestKey = unresolvedImports.keys().next().value;
|
|
870
|
+
if (oldestKey === void 0) break;
|
|
871
|
+
unresolvedImports.delete(oldestKey);
|
|
872
|
+
}
|
|
873
|
+
syncUnresolvedImportWatcher();
|
|
874
|
+
};
|
|
875
|
+
const forgetUnresolvedImportsFrom = (file) => {
|
|
876
|
+
for (const [key, unresolved] of unresolvedImports) {
|
|
877
|
+
if (resolvedIdMatchesFile(unresolved.importer, file)) {
|
|
878
|
+
unresolvedImports.delete(key);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
syncUnresolvedImportWatcher();
|
|
882
|
+
};
|
|
883
|
+
const closeUnresolvedImportWatcher = (path) => {
|
|
884
|
+
unresolvedImportWatchers.get(path)?.watcher.close();
|
|
885
|
+
unresolvedImportWatchers.delete(path);
|
|
886
|
+
};
|
|
887
|
+
const cancelUnresolvedImportWatchRearm = () => {
|
|
888
|
+
if (unresolvedImportWatchRearmTimer === null) return;
|
|
889
|
+
clearTimeout(unresolvedImportWatchRearmTimer);
|
|
890
|
+
unresolvedImportWatchRearmTimer = null;
|
|
891
|
+
};
|
|
892
|
+
const scheduleUnresolvedImportWatchRearm = () => {
|
|
893
|
+
if (unresolvedImportWatchRearmTimer !== null || !devServer || unresolvedImports.size === 0) {
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
unresolvedImportWatchRearmTimer = setTimeout(() => {
|
|
897
|
+
unresolvedImportWatchRearmTimer = null;
|
|
898
|
+
if (devServer && unresolvedImports.size > 0) {
|
|
899
|
+
syncUnresolvedImportWatcher();
|
|
900
|
+
scheduleUnresolvedImportResolution();
|
|
901
|
+
}
|
|
902
|
+
}, UNRESOLVED_IMPORT_WATCH_REARM_MS);
|
|
903
|
+
unresolvedImportWatchRearmTimer.unref?.();
|
|
904
|
+
};
|
|
905
|
+
const closeUnresolvedImportWatchers = () => {
|
|
906
|
+
cancelUnresolvedImportWatchRearm();
|
|
907
|
+
for (const path of [...unresolvedImportWatchers.keys()]) {
|
|
908
|
+
closeUnresolvedImportWatcher(path);
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
const hasUnresolvedBarePackages = () => [...unresolvedImports.values()].some(
|
|
912
|
+
(unresolved) => unresolved.packageName !== void 0
|
|
913
|
+
);
|
|
914
|
+
const packageWatchTarget = (unresolved) => {
|
|
915
|
+
let candidate = dirname(cleanModuleId(unresolved.importer));
|
|
916
|
+
let nearestPackageRoot;
|
|
917
|
+
while (true) {
|
|
918
|
+
try {
|
|
919
|
+
if (statSync2(join(candidate, "node_modules")).isDirectory()) {
|
|
920
|
+
return join(candidate, "node_modules", unresolved.packageName);
|
|
921
|
+
}
|
|
922
|
+
} catch {
|
|
923
|
+
}
|
|
924
|
+
if (nearestPackageRoot === void 0) {
|
|
925
|
+
try {
|
|
926
|
+
if (statSync2(join(candidate, "package.json")).isFile()) {
|
|
927
|
+
nearestPackageRoot = candidate;
|
|
928
|
+
}
|
|
929
|
+
} catch {
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
const parent = dirname(candidate);
|
|
933
|
+
if (parent === candidate) break;
|
|
934
|
+
candidate = parent;
|
|
935
|
+
}
|
|
936
|
+
return join(
|
|
937
|
+
nearestPackageRoot ?? canonicalRoot,
|
|
938
|
+
"node_modules",
|
|
939
|
+
unresolved.packageName
|
|
940
|
+
);
|
|
941
|
+
};
|
|
942
|
+
const unresolvedImportWatchTarget = (unresolved) => {
|
|
943
|
+
return unresolved.packageName ? packageWatchTarget(
|
|
944
|
+
unresolved
|
|
945
|
+
) : resolve(
|
|
946
|
+
dirname(cleanModuleId(unresolved.importer)),
|
|
947
|
+
cleanModuleId(unresolved.specifier)
|
|
948
|
+
);
|
|
949
|
+
};
|
|
950
|
+
const nearestExistingDirectory = (target) => {
|
|
951
|
+
let candidate = target;
|
|
952
|
+
while (true) {
|
|
953
|
+
try {
|
|
954
|
+
if (statSync2(candidate).isDirectory()) return candidate;
|
|
955
|
+
} catch {
|
|
956
|
+
}
|
|
957
|
+
const parent = dirname(candidate);
|
|
958
|
+
if (parent === candidate) return void 0;
|
|
959
|
+
candidate = parent;
|
|
960
|
+
}
|
|
961
|
+
};
|
|
962
|
+
function syncUnresolvedImportWatcher() {
|
|
963
|
+
if (!devServer || unresolvedImports.size === 0) {
|
|
964
|
+
closeUnresolvedImportWatchers();
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
const desired = /* @__PURE__ */ new Map();
|
|
968
|
+
for (const unresolved of unresolvedImports.values()) {
|
|
969
|
+
const target = unresolvedImportWatchTarget(unresolved);
|
|
970
|
+
const watchPath = nearestExistingDirectory(target);
|
|
971
|
+
if (!watchPath) continue;
|
|
972
|
+
const previous = desired.get(watchPath);
|
|
973
|
+
desired.set(watchPath, {
|
|
974
|
+
recursive: previous?.recursive === true || watchPath === target
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
for (const [path, existing] of [...unresolvedImportWatchers]) {
|
|
978
|
+
const next = desired.get(path);
|
|
979
|
+
if (!next || next.recursive !== existing.recursive) {
|
|
980
|
+
closeUnresolvedImportWatcher(path);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
for (const [path, options2] of desired) {
|
|
984
|
+
if (unresolvedImportWatchers.has(path)) continue;
|
|
985
|
+
try {
|
|
986
|
+
const watcher = watchFileSystem(
|
|
987
|
+
path,
|
|
988
|
+
{ recursive: options2.recursive },
|
|
989
|
+
scheduleUnresolvedImportResolution
|
|
990
|
+
);
|
|
991
|
+
unresolvedImportWatchers.set(path, {
|
|
992
|
+
watcher,
|
|
993
|
+
recursive: options2.recursive
|
|
994
|
+
});
|
|
995
|
+
watcher.on("error", () => {
|
|
996
|
+
closeUnresolvedImportWatcher(path);
|
|
997
|
+
scheduleUnresolvedImportWatchRearm();
|
|
998
|
+
});
|
|
999
|
+
if (options2.recursive) scheduleUnresolvedImportResolution();
|
|
1000
|
+
} catch {
|
|
1001
|
+
closeUnresolvedImportWatcher(path);
|
|
1002
|
+
scheduleUnresolvedImportWatchRearm();
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
if ([...desired.keys()].every((path) => unresolvedImportWatchers.has(path))) {
|
|
1006
|
+
cancelUnresolvedImportWatchRearm();
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
function scheduleUnresolvedImportResolution() {
|
|
1010
|
+
if (unresolvedImportResolutionScheduled || unresolvedImportResolutionRunning) {
|
|
1011
|
+
rerunUnresolvedImportResolution = true;
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
unresolvedImportResolutionScheduled = true;
|
|
1015
|
+
queueMicrotask(() => {
|
|
1016
|
+
unresolvedImportResolutionScheduled = false;
|
|
1017
|
+
void runUnresolvedImportResolution();
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
async function runUnresolvedImportResolution() {
|
|
1021
|
+
const server = devServer;
|
|
1022
|
+
if (!server || unresolvedImportResolutionRunning) return;
|
|
1023
|
+
unresolvedImportResolutionRunning = true;
|
|
1024
|
+
try {
|
|
1025
|
+
do {
|
|
1026
|
+
rerunUnresolvedImportResolution = false;
|
|
1027
|
+
syncUnresolvedImportWatcher();
|
|
1028
|
+
let resolvedImportInPass = false;
|
|
1029
|
+
for (const [key, unresolved] of [...unresolvedImports]) {
|
|
1030
|
+
let file;
|
|
1031
|
+
if (isRelativeImportSpecifier(unresolved.specifier)) {
|
|
1032
|
+
const suffixIndex = unresolved.specifier.search(/[?#]/);
|
|
1033
|
+
const relativePath = suffixIndex < 0 ? unresolved.specifier : unresolved.specifier.slice(0, suffixIndex);
|
|
1034
|
+
const candidate = resolve(
|
|
1035
|
+
dirname(cleanModuleId(unresolved.importer)),
|
|
1036
|
+
relativePath
|
|
1037
|
+
);
|
|
1038
|
+
const probes = [
|
|
1039
|
+
candidate,
|
|
1040
|
+
...RELATIVE_IMPORT_RESOLVE_EXTENSIONS.map(
|
|
1041
|
+
(extension) => `${candidate}${extension}`
|
|
1042
|
+
),
|
|
1043
|
+
...RELATIVE_IMPORT_RESOLVE_EXTENSIONS.map(
|
|
1044
|
+
(extension) => join(candidate, `index${extension}`)
|
|
1045
|
+
)
|
|
1046
|
+
];
|
|
1047
|
+
for (const probe of probes) {
|
|
1048
|
+
try {
|
|
1049
|
+
if (statSync2(probe).isFile()) {
|
|
1050
|
+
file = probe;
|
|
1051
|
+
break;
|
|
1052
|
+
}
|
|
1053
|
+
} catch {
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
if (!file && unresolved.packageName) {
|
|
1058
|
+
try {
|
|
1059
|
+
file = createRequire(cleanModuleId(unresolved.importer)).resolve(
|
|
1060
|
+
unresolved.specifier
|
|
1061
|
+
);
|
|
1062
|
+
} catch {
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
if (!file) continue;
|
|
1066
|
+
server.watcher.add(file);
|
|
1067
|
+
unresolvedImports.delete(key);
|
|
1068
|
+
errorServer.clearErrorForModules([unresolved.importer]);
|
|
1069
|
+
resolvedImportInPass = true;
|
|
1070
|
+
}
|
|
1071
|
+
if (resolvedImportInPass) {
|
|
1072
|
+
server.ws.send({ type: "full-reload", path: "*" });
|
|
1073
|
+
}
|
|
1074
|
+
syncUnresolvedImportWatcher();
|
|
1075
|
+
} while (rerunUnresolvedImportResolution);
|
|
1076
|
+
} finally {
|
|
1077
|
+
unresolvedImportResolutionRunning = false;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
345
1080
|
return {
|
|
346
1081
|
name: "tempo-annotate",
|
|
1082
|
+
apply: "serve",
|
|
347
1083
|
enforce: "pre",
|
|
1084
|
+
resolveId: {
|
|
1085
|
+
order: "pre",
|
|
1086
|
+
async handler(id, importer, resolveOptions) {
|
|
1087
|
+
if (id === INTERACT_PROJECTION_CLIENT_PATH) {
|
|
1088
|
+
return INTERACT_PROJECTION_CLIENT_ID;
|
|
1089
|
+
}
|
|
1090
|
+
if (enabled && importer && !importer.startsWith("\0")) {
|
|
1091
|
+
const packageName = barePackageName(id);
|
|
1092
|
+
if (!isRelativeImportSpecifier(id) && !packageName) return null;
|
|
1093
|
+
try {
|
|
1094
|
+
const resolved = await this.resolve(id, importer, {
|
|
1095
|
+
...resolveOptions,
|
|
1096
|
+
skipSelf: true
|
|
1097
|
+
});
|
|
1098
|
+
if (!resolved) rememberUnresolvedImport(id, importer, packageName);
|
|
1099
|
+
return resolved;
|
|
1100
|
+
} catch (error) {
|
|
1101
|
+
rememberUnresolvedImport(id, importer, packageName);
|
|
1102
|
+
throw error;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
return null;
|
|
1106
|
+
}
|
|
1107
|
+
},
|
|
1108
|
+
load(id) {
|
|
1109
|
+
if (id === INTERACT_PROJECTION_CLIENT_ID) {
|
|
1110
|
+
return `let stop;
|
|
1111
|
+
let disposed = false;
|
|
1112
|
+
if (window.parent !== window && (window.name === ${JSON.stringify(TEMPO_INTERACT_FRAME_NAME)} || window.name.startsWith(${JSON.stringify(`${TEMPO_INTERACT_FRAME_NAME}:`)}))) {
|
|
1113
|
+
window.__tempoInteractProjectionOwner = "vite";
|
|
1114
|
+
void import("tempo-sdk/canvas/interact-projection").then(({ installTempoInteractProjectionSource }) => {
|
|
1115
|
+
if (disposed) return;
|
|
1116
|
+
stop = installTempoInteractProjectionSource();
|
|
1117
|
+
});
|
|
1118
|
+
}
|
|
1119
|
+
if (import.meta.hot) import.meta.hot.dispose(() => {
|
|
1120
|
+
disposed = true;
|
|
1121
|
+
stop?.();
|
|
1122
|
+
});`;
|
|
1123
|
+
}
|
|
1124
|
+
return null;
|
|
1125
|
+
},
|
|
348
1126
|
// Expose error server for testing
|
|
349
1127
|
_tempoErrorServer: errorServer,
|
|
1128
|
+
// Test seam pinning the exact evidence the missed-change sweep reads.
|
|
1129
|
+
_tempoHotUpdateState: {
|
|
1130
|
+
lastHotUpdateMtimeMs(file) {
|
|
1131
|
+
return hotUpdateMtimesMs.get(normalizedFileKey(file));
|
|
1132
|
+
}
|
|
1133
|
+
},
|
|
1134
|
+
_tempoUnresolvedImportState: {
|
|
1135
|
+
get size() {
|
|
1136
|
+
return unresolvedImports.size;
|
|
1137
|
+
},
|
|
1138
|
+
get barePackageCount() {
|
|
1139
|
+
return [...unresolvedImports.values()].filter(
|
|
1140
|
+
(unresolved) => unresolved.packageName !== void 0
|
|
1141
|
+
).length;
|
|
1142
|
+
},
|
|
1143
|
+
get hasBarePackageWatcher() {
|
|
1144
|
+
return unresolvedImportWatchers.size > 0 && hasUnresolvedBarePackages();
|
|
1145
|
+
},
|
|
1146
|
+
get barePackageWatchPath() {
|
|
1147
|
+
return hasUnresolvedBarePackages() ? unresolvedImportWatchers.keys().next().value : void 0;
|
|
1148
|
+
},
|
|
1149
|
+
get hasUnresolvedImportWatcher() {
|
|
1150
|
+
return unresolvedImportWatchers.size > 0;
|
|
1151
|
+
},
|
|
1152
|
+
get unresolvedImportWatchPath() {
|
|
1153
|
+
return unresolvedImportWatchers.keys().next().value;
|
|
1154
|
+
},
|
|
1155
|
+
get unresolvedImportWatchPaths() {
|
|
1156
|
+
return [...unresolvedImportWatchers.keys()];
|
|
1157
|
+
},
|
|
1158
|
+
get hasUnresolvedImportWatchRearm() {
|
|
1159
|
+
return unresolvedImportWatchRearmTimer !== null;
|
|
1160
|
+
},
|
|
1161
|
+
emitUnresolvedImportWatcherErrorForTesting() {
|
|
1162
|
+
for (const { watcher } of unresolvedImportWatchers.values()) {
|
|
1163
|
+
watcher.emit(
|
|
1164
|
+
"error",
|
|
1165
|
+
new Error("Simulated unresolved-import watcher failure")
|
|
1166
|
+
);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
},
|
|
350
1170
|
configResolved(resolvedConfig) {
|
|
351
1171
|
if (!enabled) return;
|
|
352
1172
|
command = resolvedConfig.command;
|
|
353
1173
|
canonicalRoot = resolveCanonicalRoot(resolvedConfig.root);
|
|
1174
|
+
sourceIdentityRoot = options.sourceIdentityRoot ? resolveCanonicalRoot(
|
|
1175
|
+
resolve(resolvedConfig.root, options.sourceIdentityRoot)
|
|
1176
|
+
) : canonicalRoot;
|
|
354
1177
|
},
|
|
355
1178
|
configureServer(server) {
|
|
356
1179
|
if (!enabled) return;
|
|
1180
|
+
devServer = server;
|
|
1181
|
+
syncUnresolvedImportWatcher();
|
|
1182
|
+
server.watcher?.once?.("close", closeUnresolvedImportWatchers);
|
|
1183
|
+
server.httpServer?.once("close", closeUnresolvedImportWatchers);
|
|
357
1184
|
server.middlewares.use((req, res, next) => {
|
|
358
|
-
|
|
1185
|
+
handleTempoMiddleware(errorServer, server, req, res, next);
|
|
359
1186
|
});
|
|
1187
|
+
installMissedChangeSweep(
|
|
1188
|
+
server,
|
|
1189
|
+
() => errorServer.errorFiles,
|
|
1190
|
+
(file) => hotUpdateMtimesMs.get(normalizedFileKey(file)),
|
|
1191
|
+
() => pollCompileErrorRecovery(server)
|
|
1192
|
+
);
|
|
360
1193
|
const interceptHmrSend = (target) => {
|
|
361
1194
|
const orig = target.send.bind(target);
|
|
362
1195
|
target.send = ((...args) => {
|
|
@@ -374,38 +1207,135 @@ function tempoVitePlugin(options = {}) {
|
|
|
374
1207
|
plugin: err?.plugin,
|
|
375
1208
|
loc: err?.loc
|
|
376
1209
|
});
|
|
377
|
-
} else if (typed.type === "update") {
|
|
378
|
-
|
|
379
|
-
const
|
|
1210
|
+
} else if (typed.type === "update" && !processedUpdatePayloads.has(payload)) {
|
|
1211
|
+
processedUpdatePayloads.add(payload);
|
|
1212
|
+
const updatePayload = typed;
|
|
1213
|
+
const updates = updatePayload.updates;
|
|
1214
|
+
const acceptedUpdates = updates ? hmrGenerationFence.filter(updates) : void 0;
|
|
1215
|
+
if (updates && acceptedUpdates && acceptedUpdates.length !== updates.length) {
|
|
1216
|
+
updatePayload.updates = acceptedUpdates;
|
|
1217
|
+
}
|
|
1218
|
+
const paths = acceptedUpdates?.map((u) => u.path).filter((p) => typeof p === "string") ?? [];
|
|
380
1219
|
errorServer.clearErrorForModules(paths);
|
|
1220
|
+
if (updates && updates.length > 0 && acceptedUpdates?.length === 0) {
|
|
1221
|
+
return;
|
|
1222
|
+
}
|
|
381
1223
|
}
|
|
382
1224
|
}
|
|
383
1225
|
return orig(...args);
|
|
384
1226
|
});
|
|
385
1227
|
};
|
|
386
1228
|
interceptHmrSend(server.ws);
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
1229
|
+
return () => {
|
|
1230
|
+
const clientHot = server.environments?.client?.hot;
|
|
1231
|
+
if (clientHot && clientHot !== server.ws) {
|
|
1232
|
+
interceptHmrSend(clientHot);
|
|
1233
|
+
}
|
|
1234
|
+
};
|
|
1235
|
+
},
|
|
1236
|
+
closeBundle() {
|
|
1237
|
+
devServer = void 0;
|
|
1238
|
+
rapidHotUpdateReplay.clear();
|
|
1239
|
+
hmrGenerationFence.clear();
|
|
1240
|
+
hotUpdateMtimesMs.clear();
|
|
1241
|
+
compileErrorPollSchedule.clear();
|
|
1242
|
+
closeUnresolvedImportWatchers();
|
|
1243
|
+
},
|
|
1244
|
+
hotUpdate: {
|
|
1245
|
+
order: "post",
|
|
1246
|
+
async handler(context) {
|
|
1247
|
+
if (!enabled || this.environment.config.consumer !== "client") return;
|
|
1248
|
+
recordHotUpdateCoverage(context.file);
|
|
1249
|
+
if (context.type === "update" && context.modules.length > 0) {
|
|
1250
|
+
rapidHotUpdateReplay.observe(context.file, () => {
|
|
1251
|
+
context.server.config.logger.info(
|
|
1252
|
+
`[tempo] replaying settled rapid watcher change: ${context.file}`,
|
|
1253
|
+
{ timestamp: true }
|
|
1254
|
+
);
|
|
1255
|
+
context.server.watcher.emit("change", context.file);
|
|
1256
|
+
});
|
|
1257
|
+
}
|
|
1258
|
+
if (context.type !== "create") {
|
|
1259
|
+
forgetUnresolvedImportsFrom(context.file);
|
|
1260
|
+
}
|
|
1261
|
+
if (context.type === "update" && hasActiveCompileError(context.file) && await recoverActiveCompileError(context.server, context.file)) {
|
|
1262
|
+
return [];
|
|
1263
|
+
}
|
|
1264
|
+
if (context.type === "delete") {
|
|
1265
|
+
defaultExportIdentityByFile.delete(
|
|
1266
|
+
canonicalIdentityKey(context.file)
|
|
1267
|
+
);
|
|
1268
|
+
return;
|
|
1269
|
+
}
|
|
1270
|
+
if (context.type === "update" && repairDefaultExportIdentityRewrite(context.file)) {
|
|
1271
|
+
return [];
|
|
1272
|
+
}
|
|
390
1273
|
}
|
|
391
1274
|
},
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
1275
|
+
// Vite 5 compatibility. Vite 6+ prefers `hotUpdate` above.
|
|
1276
|
+
handleHotUpdate: {
|
|
1277
|
+
order: "post",
|
|
1278
|
+
async handler(context) {
|
|
1279
|
+
if (!enabled) return;
|
|
1280
|
+
recordHotUpdateCoverage(context.file);
|
|
1281
|
+
if (context.modules.length > 0) {
|
|
1282
|
+
rapidHotUpdateReplay.observe(context.file, () => {
|
|
1283
|
+
context.server.config.logger.info(
|
|
1284
|
+
`[tempo] replaying settled rapid watcher change: ${context.file}`,
|
|
1285
|
+
{ timestamp: true }
|
|
1286
|
+
);
|
|
1287
|
+
context.server.watcher.emit("change", context.file);
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
forgetUnresolvedImportsFrom(context.file);
|
|
1291
|
+
if (hasActiveCompileError(context.file) && await recoverActiveCompileError(context.server, context.file)) {
|
|
1292
|
+
return [];
|
|
1293
|
+
}
|
|
1294
|
+
if (repairDefaultExportIdentityRewrite(context.file)) {
|
|
1295
|
+
return [];
|
|
1296
|
+
}
|
|
395
1297
|
}
|
|
396
|
-
|
|
1298
|
+
},
|
|
1299
|
+
transformIndexHtml(html, context) {
|
|
1300
|
+
if (!enabled || command !== "serve") {
|
|
397
1301
|
return;
|
|
398
1302
|
}
|
|
399
|
-
|
|
400
|
-
|
|
1303
|
+
const tags = [];
|
|
1304
|
+
if (!hasRootMetaTag(html)) {
|
|
1305
|
+
tags.push({
|
|
401
1306
|
tag: "meta",
|
|
402
1307
|
attrs: {
|
|
403
1308
|
name: ROOT_META_NAME,
|
|
404
1309
|
content: canonicalRoot
|
|
405
1310
|
},
|
|
406
1311
|
injectTo: "head"
|
|
407
|
-
}
|
|
408
|
-
|
|
1312
|
+
});
|
|
1313
|
+
}
|
|
1314
|
+
if (!hasSourceIdentityRootMetaTag(html)) {
|
|
1315
|
+
tags.push({
|
|
1316
|
+
tag: "meta",
|
|
1317
|
+
attrs: {
|
|
1318
|
+
name: SOURCE_IDENTITY_ROOT_META_NAME,
|
|
1319
|
+
content: sourceIdentityRoot
|
|
1320
|
+
},
|
|
1321
|
+
injectTo: "head"
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
const requestPath = context?.originalUrl ?? context?.path;
|
|
1325
|
+
const isDeclarativeCanvasHost = requestPath ? isDeclarativeCanvasHostRequest(requestPath) : false;
|
|
1326
|
+
if (requestPath && !isDeclarativeCanvasHost) {
|
|
1327
|
+
tags.push({
|
|
1328
|
+
tag: "script",
|
|
1329
|
+
children: INTERACT_PROJECTION_BOOTSTRAP,
|
|
1330
|
+
injectTo: "head-prepend"
|
|
1331
|
+
});
|
|
1332
|
+
tags.push({
|
|
1333
|
+
tag: "script",
|
|
1334
|
+
attrs: { type: "module", src: INTERACT_PROJECTION_CLIENT_PATH },
|
|
1335
|
+
injectTo: "head-prepend"
|
|
1336
|
+
});
|
|
1337
|
+
}
|
|
1338
|
+
return tags.length > 0 ? tags : void 0;
|
|
409
1339
|
},
|
|
410
1340
|
transform(source, id) {
|
|
411
1341
|
if (!enabled || command === "build") {
|
|
@@ -421,7 +1351,12 @@ function tempoVitePlugin(options = {}) {
|
|
|
421
1351
|
try {
|
|
422
1352
|
const filepath = relative2(canonicalRoot, cleanId);
|
|
423
1353
|
const modifiedtsMs = Math.trunc(statSync2(cleanId).mtimeMs);
|
|
424
|
-
const
|
|
1354
|
+
const rawSource = readFileSync(cleanId, "utf8");
|
|
1355
|
+
const sourceVersion = sourceVersionHash(rawSource);
|
|
1356
|
+
defaultExportIdentityByFile.set(
|
|
1357
|
+
canonicalIdentityKey(cleanId),
|
|
1358
|
+
readDefaultExportIdentity(rawSource)
|
|
1359
|
+
);
|
|
425
1360
|
const result = annotateFile(
|
|
426
1361
|
source,
|
|
427
1362
|
cleanId,
|