@vitejs/devtools-vite 0.7.6 → 0.8.1

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.
Files changed (40) hide show
  1. package/dist/dirs.d.mts +1 -3
  2. package/dist/dirs.mjs +1 -2
  3. package/dist/index.d.mts +1 -8
  4. package/dist/index.mjs +600 -385
  5. package/dist/nitro.json +2 -2
  6. package/dist/public/200.html +1 -1
  7. package/dist/public/404.html +1 -1
  8. package/dist/public/_nuxt/BG--lX9h.js +2 -0
  9. package/dist/public/_nuxt/BG3xwZh0.js +3 -0
  10. package/dist/public/_nuxt/BHvJDY_a.js +1 -0
  11. package/dist/public/_nuxt/BRJBCNrs.js +1 -0
  12. package/dist/public/_nuxt/{BU3RLqzC.js → B_-pr0hI.js} +1 -1
  13. package/dist/public/_nuxt/{DgNunWKg.js → B_NKCZsG.js} +5 -5
  14. package/dist/public/_nuxt/CEBMiqg6.js +1 -0
  15. package/dist/public/_nuxt/Ct3C12NP.js +4 -0
  16. package/dist/public/_nuxt/DEyNBz-F.js +2 -0
  17. package/dist/public/_nuxt/DSAFJh4r.js +1 -0
  18. package/dist/public/_nuxt/DbCy1lFc.js +1 -0
  19. package/dist/public/_nuxt/{DGlgHJBp.js → Dp2A4F6Z.js} +1 -1
  20. package/dist/public/_nuxt/builds/latest.json +1 -1
  21. package/dist/public/_nuxt/builds/meta/13f74333-301c-46d6-87fb-e1b6264a3189.json +1 -0
  22. package/dist/public/_nuxt/{entry.p_0EwC2c.css → entry.bKphV1Y3.css} +1 -1
  23. package/dist/public/_nuxt/error-404.BtjbGiHS.css +1 -0
  24. package/dist/public/_nuxt/error-500.DqJ9a5ig.css +1 -0
  25. package/dist/public/_nuxt/n6SbU7pj.js +1 -0
  26. package/dist/public/index.html +1 -1
  27. package/package.json +9 -9
  28. package/dist/public/_nuxt/51O8tKLC.js +0 -1
  29. package/dist/public/_nuxt/B3eE09d-.js +0 -4
  30. package/dist/public/_nuxt/BMyPfja7.js +0 -1
  31. package/dist/public/_nuxt/BXzW-15h.js +0 -3
  32. package/dist/public/_nuxt/BiywBLuM.js +0 -2
  33. package/dist/public/_nuxt/BomN9dnN.js +0 -1
  34. package/dist/public/_nuxt/C0tXHD8J.js +0 -1
  35. package/dist/public/_nuxt/D_shstzp.js +0 -1
  36. package/dist/public/_nuxt/DxqvJVyq.js +0 -2
  37. package/dist/public/_nuxt/Qwmw5xxh.js +0 -1
  38. package/dist/public/_nuxt/builds/meta/06da823c-d5d5-4d15-b76b-9285b009cd67.json +0 -1
  39. package/dist/public/_nuxt/error-404.CLjXqCCe.css +0 -1
  40. package/dist/public/_nuxt/error-500.DCqDWmxV.css +0 -1
package/dist/index.mjs CHANGED
@@ -7,9 +7,9 @@ import { defineRpcFunction } from "@vitejs/devtools-kit";
7
7
  import { createFilter } from "vite";
8
8
  import { Buffer } from "node:buffer";
9
9
  import { relative } from "pathe";
10
- import { open } from "node:fs/promises";
11
10
  import { AsyncLocalStorage } from "node:async_hooks";
12
- //#region src/node/diagnostics.ts
11
+ import { open } from "node:fs/promises";
12
+ // src/node/diagnostics.ts
13
13
  const diagnostics = /* #__PURE__ */ defineDiagnostics({
14
14
  docsBase: "https://devtools.vite.dev/errors",
15
15
  reporters: [createConsoleReporter()],
@@ -19,11 +19,14 @@ const diagnostics = /* #__PURE__ */ defineDiagnostics({
19
19
  VDT0003: {
20
20
  why: (p) => `Vite inspect storage failed while ${p.operation}.`,
21
21
  fix: "Restart the Vite dev server. If the error persists, clear the Vite cache directory and reinstall dependencies."
22
+ },
23
+ VDT0004: {
24
+ why: (p) => `Vite inspect could not write the ${p.archive} archive to disk and now keeps new records in memory.`,
25
+ fix: "Check that the Vite cache directory is writable and has free disk space, then restart the Vite dev server to resume writing to disk."
22
26
  }
23
27
  }
24
28
  });
25
- //#endregion
26
- //#region src/node/inspect/utils.ts
29
+ // src/node/inspect/utils.ts
27
30
  const DUMMY_LOAD_PLUGIN_NAME = "__load__";
28
31
  function serializePlugin(plugin) {
29
32
  return JSON.parse(JSON.stringify(plugin, (key, value) => {
@@ -82,14 +85,24 @@ async function getAllModuleIds(ctx) {
82
85
  }
83
86
  return result;
84
87
  }
85
- //#endregion
86
- //#region src/node/inspect/module.ts
88
+ // src/node/inspect/module.ts
89
+ const MODULES_LIST_SLICE_MS = 4;
87
90
  async function getModulesList(ctx, pluginCtx) {
88
91
  const moduleGraph = ctx.env.mode === "dev" ? ctx.env.moduleGraph : void 0;
89
92
  const getDeps = moduleGraph ? (id) => Array.from(moduleGraph.getModuleById(id)?.importedModules || []).map((module) => module.id || "").filter(Boolean) : pluginCtx ? (id) => Array.from(pluginCtx.getModuleInfo(id)?.importedIds || []) : () => [];
90
93
  const getImporters = moduleGraph ? (id) => Array.from(moduleGraph.getModuleById(id)?.importers || []).map((module) => module.id || "").filter(Boolean) : pluginCtx ? (id) => Array.from(pluginCtx.getModuleInfo(id)?.importers || []) : () => [];
91
94
  const transformList = await ctx.inspectContext.store.getTransformList(ctx.scope);
92
95
  const resolveIdList = await ctx.inspectContext.store.getResolveIdList(ctx.scope);
96
+ const declinedLoadMetrics = await ctx.inspectContext.store.getDeclinedLoadMetrics(ctx.scope);
97
+ const declinedLoadMetricsById = /* @__PURE__ */ new Map();
98
+ for (const load of declinedLoadMetrics) {
99
+ const plugins = declinedLoadMetricsById.get(load.publicModuleId) ?? [];
100
+ plugins.push({
101
+ name: load.pluginName,
102
+ transform: load.totalTime
103
+ });
104
+ declinedLoadMetricsById.set(load.publicModuleId, plugins);
105
+ }
93
106
  const transformsById = transformList.reduce((map, transform) => {
94
107
  (map[transform.moduleId] ||= []).push(transform);
95
108
  return map;
@@ -100,10 +113,10 @@ async function getModulesList(ctx, pluginCtx) {
100
113
  return map;
101
114
  }, {});
102
115
  const ids = new Set(Object.keys(transformsById).concat(Object.keys(transformedIdMap)));
103
- return Array.from(ids).sort().map((id) => {
116
+ const toModuleInfo = (id) => {
104
117
  let totalTime = 0;
105
118
  const transforms = transformsById[id] || [];
106
- const transformPlugins = transforms.filter((transform) => transform.hasResult).map((transform) => {
119
+ const transformPlugins = transforms.map((transform) => {
107
120
  const delta = transform.end - transform.start;
108
121
  totalTime += delta;
109
122
  return {
@@ -115,7 +128,9 @@ async function getModulesList(ctx, pluginCtx) {
115
128
  name: resolveId.name,
116
129
  resolveId: resolveId.end - resolveId.start
117
130
  }));
118
- const plugins = transformPlugins.concat(resolveIdPlugins);
131
+ const loadPlugins = declinedLoadMetricsById.get(ctx.getPublicModuleId(id)) ?? [];
132
+ totalTime += loadPlugins.reduce((sum, plugin) => sum + (plugin.transform ?? 0), 0);
133
+ const plugins = transformPlugins.concat(loadPlugins, resolveIdPlugins);
119
134
  const firstTransform = transforms[0];
120
135
  const lastTransform = transforms.at(-1);
121
136
  return {
@@ -129,7 +144,17 @@ async function getModulesList(ctx, pluginCtx) {
129
144
  sourceSize: firstTransform?.hasResult ? firstTransform.resultSize : 0,
130
145
  distSize: lastTransform?.hasResult ? lastTransform.resultSize : 0
131
146
  };
132
- });
147
+ };
148
+ const modules = [];
149
+ let sliceStart = performance.now();
150
+ for (const id of Array.from(ids).sort()) {
151
+ modules.push(toModuleInfo(id));
152
+ if (performance.now() - sliceStart > MODULES_LIST_SLICE_MS) {
153
+ await new Promise((resolve) => setImmediate(resolve));
154
+ sliceStart = performance.now();
155
+ }
156
+ }
157
+ return modules;
133
158
  }
134
159
  function createGraphModuleIdResolver(moduleIds, extensions = []) {
135
160
  const moduleIdMap = /* @__PURE__ */ new Map();
@@ -165,8 +190,7 @@ function removeResolvedExtension(id, extensions) {
165
190
  const extension = extensions.find((extension) => path.endsWith(extension));
166
191
  return extension ? `${path.slice(0, -extension.length)}${query}` : void 0;
167
192
  }
168
- //#endregion
169
- //#region src/node/inspect/plugins.ts
193
+ // src/node/inspect/plugins.ts
170
194
  async function getPluginMetrics(ctx) {
171
195
  const map = {};
172
196
  const defaultMetricInfo = () => ({
@@ -228,8 +252,104 @@ async function withGraphModuleIds(ctx, calls) {
228
252
  graphModuleId: resolveGraphModuleId(call.module)
229
253
  }));
230
254
  }
231
- //#endregion
232
- //#region src/node/inspect/store/payload.ts
255
+ // src/node/inspect/server.ts
256
+ const timestampRE = /\bt=\d{13}&?\b/;
257
+ const trailingSeparatorRE = /[?&]$/;
258
+ const transformRequestStorage = new AsyncLocalStorage();
259
+ const environmentInvalidationStates = /* @__PURE__ */ new WeakMap();
260
+ function trackTransformRequestId(id) {
261
+ const request = transformRequestStorage.getStore();
262
+ if (request?.active) request.ids.add(normalizeRequestId(id));
263
+ return request?.active ? request : void 0;
264
+ }
265
+ function isTransformRequestStale(request) {
266
+ return request?.stale === true;
267
+ }
268
+ function setupEnvironmentInvalidation(server, vite) {
269
+ Object.values(server.environments).forEach((env) => {
270
+ if (!vite.isEnvironmentEnabled(env.name)) return;
271
+ const envContext = vite.getEnvContext(env);
272
+ const existingState = environmentInvalidationStates.get(env);
273
+ if (existingState) {
274
+ existingState.contexts.add(envContext);
275
+ return;
276
+ }
277
+ const state = {
278
+ activeRequests: /* @__PURE__ */ new Set(),
279
+ contexts: /* @__PURE__ */ new Set([envContext])
280
+ };
281
+ environmentInvalidationStates.set(env, state);
282
+ const transformRequest = env.transformRequest;
283
+ env.transformRequest = function(...args) {
284
+ const request = {
285
+ active: true,
286
+ ids: /* @__PURE__ */ new Set([normalizeRequestId(args[0])]),
287
+ stale: false
288
+ };
289
+ state.activeRequests.add(request);
290
+ return transformRequestStorage.run(request, () => transformRequest.apply(this, args)).finally(() => {
291
+ for (const context of state.contexts) context.inspectContext.store.clearPendingLoadMetrics(context.scope, request);
292
+ request.active = false;
293
+ request.ids.clear();
294
+ state.activeRequests.delete(request);
295
+ });
296
+ };
297
+ const invalidateModule = env.moduleGraph.invalidateModule;
298
+ env.moduleGraph.invalidateModule = function(...args) {
299
+ const mod = args[0];
300
+ const seen = args[1];
301
+ const softInvalidate = args[4] === true;
302
+ const alreadyHardInvalidated = mod?.invalidationState === "HARD_INVALIDATED";
303
+ const shouldInvalidateInspect = !softInvalidate && (!seen?.has(mod) || !alreadyHardInvalidated);
304
+ if (mod) {
305
+ markStaleRequests(state.activeRequests, mod.id, mod.url);
306
+ if (mod.id && shouldInvalidateInspect) for (const context of state.contexts) context.invalidate(mod.id);
307
+ }
308
+ return invalidateModule.apply(this, args);
309
+ };
310
+ });
311
+ }
312
+ function markStaleRequests(requests, ...ids) {
313
+ const normalizedIds = ids.filter((id) => id != null).map(normalizeRequestId);
314
+ for (const request of requests) if (normalizedIds.some((id) => request.ids.has(id))) request.stale = true;
315
+ }
316
+ function normalizeRequestId(id) {
317
+ return id.replace(timestampRE, "").replace(trailingSeparatorRE, "");
318
+ }
319
+ function setupMiddlewarePerformance(vite, middlewares) {
320
+ let firstMiddlewareIndex = -1;
321
+ middlewares.forEach((middleware, index) => {
322
+ const originalHandle = middleware.handle;
323
+ if (typeof originalHandle !== "function" || !originalHandle.name) return;
324
+ middleware.handle = function(...middlewareArgs) {
325
+ let req;
326
+ if (middlewareArgs.length === 4) req = middlewareArgs[1];
327
+ else req = middlewareArgs[0];
328
+ const start = Date.now();
329
+ const url = req.url?.replace(timestampRE, "").replace(trailingSeparatorRE, "") || "/";
330
+ const metrics = vite.data.serverMetrics.middleware[url] ||= [];
331
+ if (firstMiddlewareIndex < 0) firstMiddlewareIndex = index;
332
+ if (index === firstMiddlewareIndex) metrics.length = 0;
333
+ const result = originalHandle.apply(this, middlewareArgs);
334
+ Promise.resolve(result).then(() => {
335
+ const total = Date.now() - start;
336
+ metrics.push({
337
+ self: metrics.length ? Math.max(total - (metrics.at(-1)?.total || 0), 0) : total,
338
+ total,
339
+ name: originalHandle.name
340
+ });
341
+ });
342
+ return result;
343
+ };
344
+ Object.defineProperty(middleware.handle, "name", {
345
+ value: originalHandle.name,
346
+ configurable: true,
347
+ enumerable: true
348
+ });
349
+ });
350
+ }
351
+ // src/node/inspect/store/payload.ts
352
+ const MAX_SIZE_BUCKET = 53;
233
353
  async function createInspectPayloadStore(filename) {
234
354
  if (!filename) return new MemoryInspectPayloadStore();
235
355
  return new FileInspectPayloadStore(await open(filename, "w+"));
@@ -266,6 +386,8 @@ var FileInspectPayloadStore = class {
266
386
  file;
267
387
  nextOffset = 0;
268
388
  freeRanges = [];
389
+ freeRangesBySize = /* @__PURE__ */ new Map();
390
+ fallback;
269
391
  constructor(file) {
270
392
  this.file = file;
271
393
  }
@@ -285,11 +407,23 @@ var FileInspectPayloadStore = class {
285
407
  range
286
408
  });
287
409
  }
288
- await this.writeAllocatedPayloads(writes);
410
+ if (!this.fallback) try {
411
+ await this.writeAllocatedPayloads(writes);
412
+ return ranges;
413
+ } catch (error) {
414
+ this.fallback = /* @__PURE__ */ new Map();
415
+ diagnostics.VDT0004({
416
+ archive: "payload",
417
+ cause: error instanceof Error ? error : new Error(String(error))
418
+ });
419
+ }
420
+ for (const { buffer, range } of writes) this.fallback.set(range.offset, buffer);
289
421
  return ranges;
290
422
  }
291
423
  async read(range) {
292
424
  if (range.length === 0) return "";
425
+ const fallback = this.fallback?.get(range.offset);
426
+ if (fallback?.length === range.length) return fallback.toString();
293
427
  const buffer = Buffer.allocUnsafe(range.length);
294
428
  let readOffset = 0;
295
429
  while (readOffset < buffer.length) {
@@ -301,33 +435,38 @@ var FileInspectPayloadStore = class {
301
435
  }
302
436
  async close() {
303
437
  this.freeRanges.length = 0;
438
+ this.freeRangesBySize.clear();
439
+ this.fallback?.clear();
304
440
  await this.file.close();
305
441
  }
306
442
  reclaim(ranges) {
307
- this.freeRanges.push(...ranges.filter((range) => range.length > 0));
308
- this.mergeFreeRanges();
443
+ for (const range of ranges) this.fallback?.delete(range.offset);
444
+ for (const range of ranges) if (range.length > 0) this.insertFreeRange(range.offset, range.length);
309
445
  }
310
446
  allocate(length) {
311
447
  if (length === 0) return {
312
448
  offset: this.nextOffset,
313
449
  length
314
450
  };
315
- let bestIndex = -1;
316
- for (let index = 0; index < this.freeRanges.length; index++) {
317
- const range = this.freeRanges[index];
318
- if (range.length < length) continue;
319
- if (bestIndex < 0 || range.length < this.freeRanges[bestIndex].length) bestIndex = index;
451
+ let best;
452
+ for (let bucket = sizeBucket(length); bucket <= MAX_SIZE_BUCKET; bucket++) {
453
+ for (const range of this.freeRangesBySize.get(bucket) ?? []) {
454
+ if (range.length < length) continue;
455
+ if (!best || range.length < best.length || range.length === best.length && range.offset < best.offset) best = range;
456
+ }
457
+ if (best) break;
320
458
  }
321
- if (bestIndex >= 0) {
322
- const range = this.freeRanges[bestIndex];
459
+ if (best) {
323
460
  const allocated = {
324
- offset: range.offset,
461
+ offset: best.offset,
325
462
  length
326
463
  };
327
- if (range.length === length) this.freeRanges.splice(bestIndex, 1);
464
+ this.removeFromSizeBucket(best);
465
+ if (best.length === length) this.freeRanges.splice(this.findFreeRangeIndex(best.offset), 1);
328
466
  else {
329
- range.offset += length;
330
- range.length -= length;
467
+ best.offset += length;
468
+ best.length -= length;
469
+ this.addToSizeBucket(best);
331
470
  }
332
471
  return allocated;
333
472
  }
@@ -338,6 +477,55 @@ var FileInspectPayloadStore = class {
338
477
  this.nextOffset += length;
339
478
  return allocated;
340
479
  }
480
+ insertFreeRange(offset, length) {
481
+ let end = offset + length;
482
+ let index = this.findFreeRangeIndex(offset);
483
+ let start = offset;
484
+ const previous = this.freeRanges[index - 1];
485
+ if (previous && previous.offset + previous.length >= start) {
486
+ index -= 1;
487
+ start = previous.offset;
488
+ end = Math.max(end, previous.offset + previous.length);
489
+ }
490
+ let removeCount = 0;
491
+ while (index + removeCount < this.freeRanges.length && this.freeRanges[index + removeCount].offset <= end) {
492
+ const next = this.freeRanges[index + removeCount];
493
+ end = Math.max(end, next.offset + next.length);
494
+ removeCount += 1;
495
+ }
496
+ for (let i = index; i < index + removeCount; i++) this.removeFromSizeBucket(this.freeRanges[i]);
497
+ const merged = {
498
+ offset: start,
499
+ length: end - start
500
+ };
501
+ this.freeRanges.splice(index, removeCount, merged);
502
+ this.addToSizeBucket(merged);
503
+ }
504
+ findFreeRangeIndex(offset) {
505
+ let low = 0;
506
+ let high = this.freeRanges.length;
507
+ while (low < high) {
508
+ const middle = low + high >>> 1;
509
+ if (this.freeRanges[middle].offset < offset) low = middle + 1;
510
+ else high = middle;
511
+ }
512
+ return low;
513
+ }
514
+ addToSizeBucket(range) {
515
+ const bucket = sizeBucket(range.length);
516
+ let ranges = this.freeRangesBySize.get(bucket);
517
+ if (!ranges) {
518
+ ranges = /* @__PURE__ */ new Set();
519
+ this.freeRangesBySize.set(bucket, ranges);
520
+ }
521
+ ranges.add(range);
522
+ }
523
+ removeFromSizeBucket(range) {
524
+ const bucket = sizeBucket(range.length);
525
+ const ranges = this.freeRangesBySize.get(bucket);
526
+ ranges?.delete(range);
527
+ if (ranges?.size === 0) this.freeRangesBySize.delete(bucket);
528
+ }
341
529
  async writeAllocatedPayloads(writes) {
342
530
  const ordered = writes.toSorted((a, b) => a.range.offset - b.range.offset);
343
531
  let buffers = [];
@@ -364,20 +552,10 @@ var FileInspectPayloadStore = class {
364
552
  offset += bytesWritten;
365
553
  }
366
554
  }
367
- mergeFreeRanges() {
368
- this.freeRanges.sort((a, b) => a.offset - b.offset);
369
- const merged = [];
370
- for (const range of this.freeRanges) {
371
- const previous = merged.at(-1);
372
- if (!previous || range.offset > previous.offset + previous.length) {
373
- merged.push({ ...range });
374
- continue;
375
- }
376
- previous.length = Math.max(previous.length, range.offset + range.length - previous.offset);
377
- }
378
- this.freeRanges = merged;
379
- }
380
555
  };
556
+ function sizeBucket(length) {
557
+ return Math.floor(Math.log2(length));
558
+ }
381
559
  function consumeBuffers$1(input, bytes) {
382
560
  const buffers = input.slice();
383
561
  let consumed = bytes;
@@ -388,8 +566,7 @@ function consumeBuffers$1(input, bytes) {
388
566
  if (buffers.length > 0 && consumed > 0) buffers[0] = buffers[0].subarray(consumed);
389
567
  return buffers;
390
568
  }
391
- //#endregion
392
- //#region src/node/inspect/store/plugin-calls.ts
569
+ // src/node/inspect/store/plugin-calls.ts
393
570
  const RECORD_SIZE = 40;
394
571
  const RECORDS_PER_SEGMENT = 256;
395
572
  const SEGMENT_HEADER_SIZE = 32;
@@ -678,10 +855,35 @@ var BinaryInspectPluginCallStore = class {
678
855
  };
679
856
  var FilePluginCallStorage = class {
680
857
  file;
858
+ fallback;
681
859
  constructor(file) {
682
860
  this.file = file;
683
861
  }
684
862
  async write(input, startPosition) {
863
+ if (!this.fallback) try {
864
+ await this.writeFile(input, startPosition);
865
+ return;
866
+ } catch (error) {
867
+ this.fallback = {
868
+ start: startPosition,
869
+ storage: new MemoryPluginCallStorage()
870
+ };
871
+ diagnostics.VDT0004({
872
+ archive: "plugin call",
873
+ cause: error instanceof Error ? error : new Error(String(error))
874
+ });
875
+ }
876
+ await this.fallback.storage.write(input, startPosition - this.fallback.start);
877
+ }
878
+ async read(position, length) {
879
+ if (this.fallback && position >= this.fallback.start) return this.fallback.storage.read(position - this.fallback.start, length);
880
+ return this.readFile(position, length);
881
+ }
882
+ async close() {
883
+ await this.fallback?.storage.close();
884
+ await this.file.close();
885
+ }
886
+ async writeFile(input, startPosition) {
685
887
  let buffers = input;
686
888
  let position = startPosition;
687
889
  while (buffers.length > 0) {
@@ -691,7 +893,7 @@ var FilePluginCallStorage = class {
691
893
  position += bytesWritten;
692
894
  }
693
895
  }
694
- async read(position, length) {
896
+ async readFile(position, length) {
695
897
  const buffer = Buffer.allocUnsafe(length);
696
898
  let offset = 0;
697
899
  while (offset < buffer.length) {
@@ -701,9 +903,6 @@ var FilePluginCallStorage = class {
701
903
  }
702
904
  return buffer.subarray(0, offset);
703
905
  }
704
- async close() {
705
- await this.file.close();
706
- }
707
906
  };
708
907
  var MemoryPluginCallStorage = class {
709
908
  buffer = Buffer.alloc(0);
@@ -797,8 +996,7 @@ function yieldToEventLoop() {
797
996
  function toError$1(error) {
798
997
  return error instanceof Error ? error : new Error(String(error));
799
998
  }
800
- //#endregion
801
- //#region src/node/inspect/store/index.ts
999
+ // src/node/inspect/store/index.ts
802
1000
  const DEFAULT_MAX_BATCH_ITEMS = 256;
803
1001
  const DEFAULT_MAX_BATCH_BYTES = 8388608;
804
1002
  async function createViteInspectStore(options = {}) {
@@ -835,6 +1033,7 @@ var PayloadViteInspectStore = class {
835
1033
  peakQueuedItems = 0;
836
1034
  peakQueuedBytes = 0;
837
1035
  writeBatches = 0;
1036
+ changeVersion = 0;
838
1037
  activeReaders = 0;
839
1038
  readerWaiters = [];
840
1039
  constructor(payloads, pluginCalls, options) {
@@ -843,7 +1042,7 @@ var PayloadViteInspectStore = class {
843
1042
  this.maxBatchItems = positiveInteger(options.maxBatchItems, DEFAULT_MAX_BATCH_ITEMS);
844
1043
  this.maxBatchBytes = positiveInteger(options.maxBatchBytes, DEFAULT_MAX_BATCH_BYTES);
845
1044
  }
846
- recordTransform(scope, moduleId, publicModuleId, info, preTransformCode, pluginCall) {
1045
+ recordTransform(scope, moduleId, publicModuleId, info, preTransformCode, pluginCall, transformRequest) {
847
1046
  this.enqueue({
848
1047
  operation: "recordTransform",
849
1048
  scope,
@@ -851,17 +1050,19 @@ var PayloadViteInspectStore = class {
851
1050
  publicModuleId,
852
1051
  info,
853
1052
  preTransformCode,
854
- pluginCall
1053
+ pluginCall,
1054
+ transformRequest
855
1055
  });
856
1056
  }
857
- recordLoad(scope, moduleId, publicModuleId, info, pluginCall) {
1057
+ recordLoad(scope, moduleId, publicModuleId, info, pluginCall, transformRequest) {
858
1058
  this.enqueue({
859
1059
  operation: "recordLoad",
860
1060
  scope,
861
1061
  moduleId,
862
1062
  publicModuleId,
863
1063
  info,
864
- pluginCall
1064
+ pluginCall,
1065
+ transformRequest
865
1066
  });
866
1067
  }
867
1068
  recordResolveId(scope, sourceId, sourcePublicId, info, resultPublicId, pluginCall) {
@@ -875,11 +1076,19 @@ var PayloadViteInspectStore = class {
875
1076
  pluginCall
876
1077
  });
877
1078
  }
878
- recordPluginCall(scope, info) {
1079
+ recordPluginCall(scope, info, transformRequest) {
879
1080
  this.enqueue({
880
1081
  operation: "recordPluginCall",
881
1082
  scope,
882
- info
1083
+ info,
1084
+ transformRequest
1085
+ });
1086
+ }
1087
+ clearPendingLoadMetrics(scope, transformRequest) {
1088
+ this.enqueue({
1089
+ operation: "clearPendingLoadMetrics",
1090
+ scope,
1091
+ transformRequest
883
1092
  });
884
1093
  }
885
1094
  invalidate(scope, moduleId, publicModuleId) {
@@ -912,6 +1121,13 @@ var PayloadViteInspectStore = class {
912
1121
  });
913
1122
  return result;
914
1123
  }
1124
+ async getDeclinedLoadMetrics(scope) {
1125
+ await this.flush();
1126
+ return Array.from(this.scopes.get(scope)?.declinedLoadMetrics ?? []).flatMap(([publicModuleId, metrics]) => Array.from(metrics.values(), (metric) => ({
1127
+ ...metric,
1128
+ publicModuleId
1129
+ })));
1130
+ }
915
1131
  async getResolveIdList(scope) {
916
1132
  await this.flush();
917
1133
  return Array.from(this.scopes.get(scope)?.resolveIds.values() ?? []).flat();
@@ -962,6 +1178,9 @@ var PayloadViteInspectStore = class {
962
1178
  for (const resolveIds of data.resolveIds.values()) for (const resolveId of resolveIds) moduleIds.add(resolveId.result);
963
1179
  return Array.from(moduleIds);
964
1180
  }
1181
+ getChangeVersion() {
1182
+ return this.changeVersion;
1183
+ }
965
1184
  getStats() {
966
1185
  return {
967
1186
  maxBatchItems: this.maxBatchItems,
@@ -993,6 +1212,7 @@ var PayloadViteInspectStore = class {
993
1212
  }
994
1213
  enqueue(write) {
995
1214
  if (this.closing || this.closed || this.lastWriteError) return;
1215
+ if (write.operation !== "clearPendingLoadMetrics") this.changeVersion += 1;
996
1216
  const estimatedBytes = estimateWriteBytes(write);
997
1217
  this.queue.push({
998
1218
  write,
@@ -1065,6 +1285,9 @@ var PayloadViteInspectStore = class {
1065
1285
  for (const { write } of batch) if (write.operation === "invalidate") {
1066
1286
  await flushDataWrites();
1067
1287
  this.invalidateNow(write.scope, write.moduleId, write.publicModuleId);
1288
+ } else if (write.operation === "clearPendingLoadMetrics") {
1289
+ await flushDataWrites();
1290
+ this.scopes.get(write.scope)?.pendingLoadMetrics.delete(write.transformRequest);
1068
1291
  } else if (write.operation === "clearScope") {
1069
1292
  await flushDataWrites();
1070
1293
  this.clearScopeNow(write.scope);
@@ -1075,14 +1298,38 @@ var PayloadViteInspectStore = class {
1075
1298
  const values = [];
1076
1299
  const prepared = [];
1077
1300
  const pluginCallWrites = [];
1078
- const sourceState = /* @__PURE__ */ new Map();
1079
- const hasSource = (scope, moduleId) => {
1080
- const key = scopeKey(scope, moduleId);
1081
- const pending = sourceState.get(key);
1082
- if (pending != null) return pending;
1083
- const stored = this.scopes.get(scope)?.transforms.get(moduleId)?.items.some((item) => item.hasResult) ?? false;
1084
- sourceState.set(key, stored);
1085
- return stored;
1301
+ const moduleStates = /* @__PURE__ */ new Map();
1302
+ const getModuleState = (scope, moduleId) => {
1303
+ let scoped = moduleStates.get(scope);
1304
+ if (!scoped) {
1305
+ scoped = /* @__PURE__ */ new Map();
1306
+ moduleStates.set(scope, scoped);
1307
+ }
1308
+ let state = scoped.get(moduleId);
1309
+ if (!state) {
1310
+ state = {};
1311
+ scoped.set(moduleId, state);
1312
+ }
1313
+ return state;
1314
+ };
1315
+ const pushCode = (state, value) => {
1316
+ if (value != null && state.latest?.value === value) return state.latest.index;
1317
+ const index = values.push(value) - 1;
1318
+ if (value != null) state.latest = {
1319
+ value,
1320
+ index
1321
+ };
1322
+ return index;
1323
+ };
1324
+ const hasSource = (state, scope, moduleId, transformRequest) => {
1325
+ if (!state.source) {
1326
+ const stored = this.scopes.get(scope)?.transforms.get(moduleId);
1327
+ state.source = {
1328
+ hasSource: stored?.items.some((item) => item.hasResult) ?? false,
1329
+ transformRequest: stored?.transformRequest
1330
+ };
1331
+ }
1332
+ return state.source.hasSource && (!transformRequest || state.source.transformRequest === transformRequest);
1086
1333
  };
1087
1334
  for (const write of writes) {
1088
1335
  const pluginCall = getQueuedPluginCall(write);
@@ -1090,20 +1337,27 @@ var PayloadViteInspectStore = class {
1090
1337
  scope: write.scope,
1091
1338
  info: pluginCall
1092
1339
  });
1093
- if (write.operation === "recordPluginCall") continue;
1340
+ if (write.operation === "recordPluginCall" && write.info.type !== "load") continue;
1094
1341
  const item = { write };
1095
1342
  if (write.operation === "recordTransform") {
1096
- const key = scopeKey(write.scope, write.moduleId);
1097
- if (!hasSource(write.scope, write.moduleId)) {
1098
- item.sourceIndex = values.push(write.preTransformCode) - 1;
1099
- sourceState.set(key, true);
1343
+ const state = getModuleState(write.scope, write.moduleId);
1344
+ if (!hasSource(state, write.scope, write.moduleId, write.transformRequest)) {
1345
+ state.latest = void 0;
1346
+ item.sourceIndex = pushCode(state, write.preTransformCode);
1347
+ state.source = {
1348
+ hasSource: true,
1349
+ transformRequest: write.transformRequest
1350
+ };
1100
1351
  }
1101
- item.resultIndex = values.push(write.info.result) - 1;
1102
- item.sourcemapIndex = pushSerializedPayload(values, write.info.sourcemaps);
1352
+ item.resultIndex = pushCode(state, write.info.result);
1103
1353
  } else if (write.operation === "recordLoad") {
1104
- item.resultIndex = values.push(write.info.result) - 1;
1105
- item.sourcemapIndex = pushSerializedPayload(values, write.info.sourcemaps);
1106
- sourceState.set(scopeKey(write.scope, write.moduleId), write.info.result != null);
1354
+ const state = getModuleState(write.scope, write.moduleId);
1355
+ state.latest = void 0;
1356
+ item.resultIndex = pushCode(state, write.info.result);
1357
+ state.source = {
1358
+ hasSource: write.info.result != null,
1359
+ transformRequest: write.transformRequest
1360
+ };
1107
1361
  }
1108
1362
  prepared.push(item);
1109
1363
  }
@@ -1112,93 +1366,136 @@ var PayloadViteInspectStore = class {
1112
1366
  }
1113
1367
  applyPreparedWrite(prepared, ranges) {
1114
1368
  const { write } = prepared;
1115
- if (write.operation === "recordTransform") this.applyTransform(write, rangeAt(ranges, prepared.resultIndex), rangeAt(ranges, prepared.sourceIndex), rangeAt(ranges, prepared.sourcemapIndex));
1116
- else if (write.operation === "recordLoad") this.applyLoad(write, rangeAt(ranges, prepared.resultIndex), rangeAt(ranges, prepared.sourcemapIndex));
1117
- else if (write.operation === "recordResolveId") {
1369
+ if (write.operation === "recordTransform") this.applyTransform(write, rangeAt(ranges, prepared.resultIndex), rangeAt(ranges, prepared.sourceIndex));
1370
+ else if (write.operation === "recordLoad") this.applyLoad(write, rangeAt(ranges, prepared.resultIndex));
1371
+ else if (write.operation === "recordPluginCall" && write.info.type === "load") {
1118
1372
  const data = this.getScope(write.scope);
1119
- let resolveIds = data.resolveIds.get(write.sourceId);
1120
- if (!resolveIds) {
1121
- resolveIds = [];
1122
- data.resolveIds.set(write.sourceId, resolveIds);
1373
+ let pendingLoadMetrics = data.pendingLoadMetrics.get(write.transformRequest);
1374
+ if (!pendingLoadMetrics) {
1375
+ pendingLoadMetrics = /* @__PURE__ */ new Map();
1376
+ data.pendingLoadMetrics.set(write.transformRequest, pendingLoadMetrics);
1123
1377
  }
1124
- resolveIds.push({
1125
- sourceId: write.sourceId,
1126
- sourcePublicId: write.sourcePublicId,
1127
- result: write.info.result,
1128
- resultPublicId: write.resultPublicId,
1129
- name: write.info.name,
1130
- pluginId: write.info.plugin_id,
1131
- start: write.info.start,
1132
- end: write.info.end
1133
- });
1134
- }
1378
+ let metrics = pendingLoadMetrics.get(write.info.module);
1379
+ if (!metrics) {
1380
+ metrics = /* @__PURE__ */ new Map();
1381
+ pendingLoadMetrics.set(write.info.module, metrics);
1382
+ }
1383
+ addMetric(metrics, write.info.plugin_id, write.info.plugin_name, write.info.duration);
1384
+ } else if (write.operation === "recordResolveId") addResolveId(this.getScope(write.scope), {
1385
+ sourceId: write.sourceId,
1386
+ sourcePublicId: write.sourcePublicId,
1387
+ result: write.info.result,
1388
+ resultPublicId: write.resultPublicId,
1389
+ name: write.info.name,
1390
+ pluginId: write.info.plugin_id,
1391
+ start: write.info.start,
1392
+ end: write.info.end
1393
+ });
1135
1394
  }
1136
- applyTransform(write, result, source, sourcemaps) {
1395
+ applyTransform(write, result, source) {
1137
1396
  const data = this.getScope(write.scope);
1138
1397
  let transforms = data.transforms.get(write.moduleId);
1139
- if (!transforms) {
1398
+ if (!transforms || write.transformRequest && transforms.transformRequest !== write.transformRequest) {
1399
+ if (transforms) this.payloads.reclaim(collectPayloadRanges(transforms.items));
1140
1400
  transforms = {
1401
+ transformRequest: write.transformRequest,
1141
1402
  publicModuleId: write.publicModuleId,
1142
- invokeCount: 0,
1403
+ invokeCount: transforms?.invokeCount ?? 0,
1143
1404
  items: []
1144
1405
  };
1145
- data.transforms.set(write.moduleId, transforms);
1406
+ setTransforms(data, write.moduleId, transforms);
1146
1407
  }
1147
1408
  if (!transforms.items.some((item) => item.hasResult)) {
1409
+ this.commitDeclinedLoadMetrics(data, write.publicModuleId, write.transformRequest);
1148
1410
  transforms.items.push({
1149
1411
  name: DUMMY_LOAD_PLUGIN_NAME,
1150
1412
  hasResult: true,
1151
1413
  result: source,
1152
1414
  resultSize: source?.length ?? 0,
1153
1415
  start: write.info.start,
1154
- end: write.info.start,
1155
- sourcemaps
1416
+ end: write.info.start
1156
1417
  });
1157
1418
  transforms.invokeCount += 1;
1158
1419
  }
1159
- transforms.items.push(toStoredTransformInfo(write.info, result, sourcemaps));
1420
+ transforms.items.push(toStoredTransformInfo(write.info, result));
1160
1421
  }
1161
- applyLoad(write, result, sourcemaps) {
1422
+ applyLoad(write, result) {
1162
1423
  const data = this.getScope(write.scope);
1424
+ this.commitDeclinedLoadMetrics(data, write.publicModuleId, write.transformRequest);
1163
1425
  const existing = data.transforms.get(write.moduleId);
1164
1426
  if (existing) this.payloads.reclaim(collectPayloadRanges(existing.items));
1165
- data.transforms.set(write.moduleId, {
1427
+ setTransforms(data, write.moduleId, {
1428
+ transformRequest: write.transformRequest,
1166
1429
  publicModuleId: write.publicModuleId,
1167
1430
  invokeCount: (existing?.invokeCount ?? 0) + 1,
1168
- items: [toStoredTransformInfo(write.info, result, sourcemaps)]
1431
+ items: [toStoredTransformInfo(write.info, result)]
1169
1432
  });
1170
1433
  }
1434
+ commitDeclinedLoadMetrics(data, publicModuleId, transformRequest) {
1435
+ const pendingLoadMetrics = data.pendingLoadMetrics.get(transformRequest);
1436
+ const declinedLoadMetrics = pendingLoadMetrics?.get(publicModuleId);
1437
+ if (declinedLoadMetrics) data.declinedLoadMetrics.set(publicModuleId, declinedLoadMetrics);
1438
+ else data.declinedLoadMetrics.delete(publicModuleId);
1439
+ pendingLoadMetrics?.delete(publicModuleId);
1440
+ if (pendingLoadMetrics?.size === 0) data.pendingLoadMetrics.delete(transformRequest);
1441
+ }
1171
1442
  invalidateNow(scope, moduleId, publicModuleId) {
1172
1443
  const data = this.scopes.get(scope);
1173
1444
  if (!data) {
1174
1445
  this.pluginCalls.invalidate(scope, /* @__PURE__ */ new Set([publicModuleId]));
1175
1446
  return;
1176
1447
  }
1177
- const invalidIds = /* @__PURE__ */ new Set([moduleId]);
1178
- const invalidPublicIds = /* @__PURE__ */ new Set([publicModuleId]);
1179
- let changed = true;
1180
- while (changed) {
1181
- changed = false;
1182
- for (const [id, transforms] of data.transforms) if (invalidIds.has(id) || invalidPublicIds.has(transforms.publicModuleId)) changed = addInvalidId(invalidIds, invalidPublicIds, id, transforms.publicModuleId) || changed;
1183
- for (const resolveIds of data.resolveIds.values()) for (const resolveId of resolveIds) {
1184
- if (!matchesInvalid(resolveId.sourceId, resolveId.sourcePublicId, invalidIds, invalidPublicIds) && !matchesInvalid(resolveId.result, resolveId.resultPublicId, invalidIds, invalidPublicIds)) continue;
1185
- changed = addInvalidId(invalidIds, invalidPublicIds, resolveId.sourceId, resolveId.sourcePublicId) || changed;
1186
- changed = addInvalidId(invalidIds, invalidPublicIds, resolveId.result, resolveId.resultPublicId) || changed;
1448
+ const invalidIds = /* @__PURE__ */ new Set();
1449
+ const invalidPublicIds = /* @__PURE__ */ new Set();
1450
+ const pendingIds = [];
1451
+ const pendingPublicIds = [];
1452
+ const linkedResolveIds = /* @__PURE__ */ new Set();
1453
+ const addInvalid = (id, publicId) => {
1454
+ if (!invalidIds.has(id)) {
1455
+ invalidIds.add(id);
1456
+ pendingIds.push(id);
1457
+ }
1458
+ if (!invalidPublicIds.has(publicId)) {
1459
+ invalidPublicIds.add(publicId);
1460
+ pendingPublicIds.push(publicId);
1461
+ }
1462
+ };
1463
+ const linkResolveIds = (items) => {
1464
+ for (const item of items ?? []) {
1465
+ if (linkedResolveIds.has(item)) continue;
1466
+ linkedResolveIds.add(item);
1467
+ addInvalid(item.sourceId, item.sourcePublicId);
1468
+ addInvalid(item.result, item.resultPublicId);
1187
1469
  }
1470
+ };
1471
+ addInvalid(moduleId, publicModuleId);
1472
+ while (pendingIds.length > 0 || pendingPublicIds.length > 0) {
1473
+ const id = pendingIds.pop();
1474
+ if (id !== void 0) {
1475
+ const transforms = data.transforms.get(id);
1476
+ if (transforms) addInvalid(id, transforms.publicModuleId);
1477
+ linkResolveIds(data.resolveIdsById.get(id));
1478
+ continue;
1479
+ }
1480
+ const publicId = pendingPublicIds.pop();
1481
+ for (const transformId of data.transformIdsByPublicId.get(publicId) ?? []) addInvalid(transformId, data.transforms.get(transformId).publicModuleId);
1482
+ linkResolveIds(data.resolveIdsByPublicId.get(publicId));
1188
1483
  }
1189
1484
  const reclaimed = [];
1190
- for (const [id, transforms] of data.transforms) {
1191
- if (!matchesInvalid(id, transforms.publicModuleId, invalidIds, invalidPublicIds)) continue;
1485
+ for (const id of invalidIds) {
1486
+ const transforms = data.transforms.get(id);
1487
+ if (!transforms) continue;
1192
1488
  reclaimed.push(...collectPayloadRanges(transforms.items));
1193
- data.transforms.delete(id);
1489
+ deleteTransforms(data, id);
1194
1490
  }
1195
1491
  this.payloads.reclaim(reclaimed);
1196
- for (const [sourceId, resolveIds] of data.resolveIds) {
1197
- const remaining = resolveIds.filter((resolveId) => {
1198
- return !matchesInvalid(sourceId, resolveId.sourcePublicId, invalidIds, invalidPublicIds) && !matchesInvalid(resolveId.result, resolveId.resultPublicId, invalidIds, invalidPublicIds);
1199
- });
1200
- if (remaining.length > 0) data.resolveIds.set(sourceId, remaining);
1201
- else data.resolveIds.delete(sourceId);
1492
+ deleteResolveIds(data, linkedResolveIds);
1493
+ for (const id of invalidPublicIds) {
1494
+ data.declinedLoadMetrics.delete(id);
1495
+ for (const [request, pendingLoadMetrics] of data.pendingLoadMetrics) {
1496
+ pendingLoadMetrics.delete(id);
1497
+ if (pendingLoadMetrics.size === 0) data.pendingLoadMetrics.delete(request);
1498
+ }
1202
1499
  }
1203
1500
  this.pluginCalls.invalidate(scope, invalidPublicIds);
1204
1501
  }
@@ -1215,7 +1512,7 @@ var PayloadViteInspectStore = class {
1215
1512
  this.scopes.delete(scope);
1216
1513
  }
1217
1514
  async restoreTransformInfo(info) {
1218
- const [result, sourcemaps] = await Promise.all([info.result ? this.payloads.read(info.result) : void 0, info.sourcemaps ? this.payloads.read(info.sourcemaps) : void 0]);
1515
+ const result = info.result ? await this.payloads.read(info.result) : void 0;
1219
1516
  return {
1220
1517
  name: info.name,
1221
1518
  plugin_id: info.pluginId,
@@ -1223,7 +1520,6 @@ var PayloadViteInspectStore = class {
1223
1520
  start: info.start,
1224
1521
  end: info.end,
1225
1522
  order: info.order,
1226
- sourcemaps: sourcemaps == null ? void 0 : parsePayload(sourcemaps),
1227
1523
  error: info.error
1228
1524
  };
1229
1525
  }
@@ -1248,7 +1544,12 @@ var PayloadViteInspectStore = class {
1248
1544
  if (!data) {
1249
1545
  data = {
1250
1546
  transforms: /* @__PURE__ */ new Map(),
1251
- resolveIds: /* @__PURE__ */ new Map()
1547
+ resolveIds: /* @__PURE__ */ new Map(),
1548
+ transformIdsByPublicId: /* @__PURE__ */ new Map(),
1549
+ resolveIdsById: /* @__PURE__ */ new Map(),
1550
+ resolveIdsByPublicId: /* @__PURE__ */ new Map(),
1551
+ declinedLoadMetrics: /* @__PURE__ */ new Map(),
1552
+ pendingLoadMetrics: /* @__PURE__ */ new Map()
1252
1553
  };
1253
1554
  this.scopes.set(scope, data);
1254
1555
  }
@@ -1305,22 +1606,64 @@ function addMetric(metrics, pluginId, pluginName, duration) {
1305
1606
  metric.invokeCount += 1;
1306
1607
  metric.totalTime += duration;
1307
1608
  }
1308
- function addInvalidId(ids, publicIds, id, publicId) {
1309
- const idSize = ids.size;
1310
- const publicIdSize = publicIds.size;
1311
- ids.add(id);
1312
- publicIds.add(publicId);
1313
- return ids.size !== idSize || publicIds.size !== publicIdSize;
1609
+ function setTransforms(data, id, transforms) {
1610
+ deleteTransforms(data, id);
1611
+ data.transforms.set(id, transforms);
1612
+ addToIndex(data.transformIdsByPublicId, transforms.publicModuleId, id);
1613
+ }
1614
+ function deleteTransforms(data, id) {
1615
+ const existing = data.transforms.get(id);
1616
+ if (!existing) return;
1617
+ data.transforms.delete(id);
1618
+ removeFromIndex(data.transformIdsByPublicId, existing.publicModuleId, id);
1619
+ }
1620
+ function addResolveId(data, item) {
1621
+ let items = data.resolveIds.get(item.sourceId);
1622
+ if (!items) {
1623
+ items = [];
1624
+ data.resolveIds.set(item.sourceId, items);
1625
+ }
1626
+ items.push(item);
1627
+ addToIndex(data.resolveIdsById, item.sourceId, item);
1628
+ addToIndex(data.resolveIdsById, item.result, item);
1629
+ addToIndex(data.resolveIdsByPublicId, item.sourcePublicId, item);
1630
+ addToIndex(data.resolveIdsByPublicId, item.resultPublicId, item);
1314
1631
  }
1315
- function matchesInvalid(id, publicId, ids, publicIds) {
1316
- return ids.has(id) || publicIds.has(publicId);
1632
+ function deleteResolveIds(data, items) {
1633
+ const sourceIds = /* @__PURE__ */ new Set();
1634
+ for (const item of items) {
1635
+ sourceIds.add(item.sourceId);
1636
+ removeFromIndex(data.resolveIdsById, item.sourceId, item);
1637
+ removeFromIndex(data.resolveIdsById, item.result, item);
1638
+ removeFromIndex(data.resolveIdsByPublicId, item.sourcePublicId, item);
1639
+ removeFromIndex(data.resolveIdsByPublicId, item.resultPublicId, item);
1640
+ }
1641
+ for (const sourceId of sourceIds) {
1642
+ const remaining = data.resolveIds.get(sourceId).filter((item) => !items.has(item));
1643
+ if (remaining.length > 0) data.resolveIds.set(sourceId, remaining);
1644
+ else data.resolveIds.delete(sourceId);
1645
+ }
1646
+ }
1647
+ function addToIndex(index, key, value) {
1648
+ let values = index.get(key);
1649
+ if (!values) {
1650
+ values = /* @__PURE__ */ new Set();
1651
+ index.set(key, values);
1652
+ }
1653
+ values.add(value);
1654
+ }
1655
+ function removeFromIndex(index, key, value) {
1656
+ const values = index.get(key);
1657
+ if (!values) return;
1658
+ values.delete(value);
1659
+ if (values.size === 0) index.delete(key);
1317
1660
  }
1318
1661
  function collectPayloadRanges(items) {
1319
1662
  const ranges = /* @__PURE__ */ new Map();
1320
- for (const item of items) for (const range of [item.result, item.sourcemaps]) if (range) ranges.set(`${range.offset}:${range.length}`, range);
1663
+ for (const item of items) if (item.result) ranges.set(`${item.result.offset}:${item.result.length}`, item.result);
1321
1664
  return Array.from(ranges.values());
1322
1665
  }
1323
- function toStoredTransformInfo(info, result, sourcemaps) {
1666
+ function toStoredTransformInfo(info, result) {
1324
1667
  return {
1325
1668
  name: info.name,
1326
1669
  pluginId: info.plugin_id,
@@ -1330,44 +1673,20 @@ function toStoredTransformInfo(info, result, sourcemaps) {
1330
1673
  start: info.start,
1331
1674
  end: info.end,
1332
1675
  order: info.order,
1333
- sourcemaps,
1334
1676
  error: info.error
1335
1677
  };
1336
1678
  }
1337
- function pushSerializedPayload(values, value) {
1338
- const serialized = serializePayload(value);
1339
- if (serialized == null) return void 0;
1340
- return values.push(serialized) - 1;
1341
- }
1342
- function serializePayload(value) {
1343
- if (value == null) return void 0;
1344
- try {
1345
- return JSON.stringify(value);
1346
- } catch {
1347
- return JSON.stringify(String(value));
1348
- }
1349
- }
1350
- function parsePayload(value) {
1351
- try {
1352
- return JSON.parse(value);
1353
- } catch {
1354
- return;
1355
- }
1356
- }
1357
1679
  function rangeAt(ranges, index) {
1358
1680
  return index == null ? void 0 : ranges[index];
1359
1681
  }
1360
- function scopeKey(scope, moduleId) {
1361
- return `${scope}\0${moduleId}`;
1362
- }
1363
1682
  function positiveInteger(value, fallback) {
1364
1683
  return Number.isFinite(value) && value > 0 ? Math.max(1, Math.floor(value)) : fallback;
1365
1684
  }
1366
1685
  function estimateWriteBytes(write) {
1367
- const pluginCall = write.operation === "invalidate" || write.operation === "clearScope" ? void 0 : getQueuedPluginCall(write);
1686
+ const pluginCall = write.operation === "invalidate" || write.operation === "clearScope" || write.operation === "clearPendingLoadMetrics" ? void 0 : getQueuedPluginCall(write);
1368
1687
  const pluginCallBytes = pluginCall ? 48 + estimateStringBytes(pluginCall.id) + estimateStringBytes(pluginCall.plugin_name) + estimateStringBytes(pluginCall.module) : 0;
1369
- if (write.operation === "recordTransform") return pluginCallBytes + estimateStringBytes(write.info.result) + estimateStringBytes(write.preTransformCode) + estimateTopLevelSourcemapBytes(write.info.sourcemaps);
1370
- if (write.operation === "recordLoad") return pluginCallBytes + estimateStringBytes(write.info.result) + estimateTopLevelSourcemapBytes(write.info.sourcemaps);
1688
+ if (write.operation === "recordTransform") return pluginCallBytes + estimateStringBytes(write.info.result) + estimateStringBytes(write.preTransformCode);
1689
+ if (write.operation === "recordLoad") return pluginCallBytes + estimateStringBytes(write.info.result);
1371
1690
  return pluginCallBytes;
1372
1691
  }
1373
1692
  function getQueuedPluginCall(write) {
@@ -1376,13 +1695,6 @@ function getQueuedPluginCall(write) {
1376
1695
  function estimateStringBytes(value) {
1377
1696
  return typeof value === "string" ? value.length * 2 : 0;
1378
1697
  }
1379
- function estimateTopLevelSourcemapBytes(value) {
1380
- if (value == null) return 0;
1381
- if (typeof value === "string") return value.length * 2;
1382
- if (typeof value !== "object") return 0;
1383
- const mappings = value.mappings;
1384
- return estimateStringBytes(mappings);
1385
- }
1386
1698
  function reportStorageError(operation, error) {
1387
1699
  diagnostics.VDT0003({
1388
1700
  operation,
@@ -1392,8 +1704,7 @@ function reportStorageError(operation, error) {
1392
1704
  function toError(error) {
1393
1705
  return error instanceof Error ? error : new Error(String(error));
1394
1706
  }
1395
- //#endregion
1396
- //#region src/node/inspect/context.ts
1707
+ // src/node/inspect/context.ts
1397
1708
  let viteCount = 0;
1398
1709
  const contextMap = /* @__PURE__ */ new WeakMap();
1399
1710
  function setViteInspectContext(devtoolsCtx, ctx) {
@@ -1502,6 +1813,7 @@ var ViteInspectViteContext = class {
1502
1813
  return envContext;
1503
1814
  }
1504
1815
  };
1816
+ const MAX_PUBLIC_MODULE_ID_CACHE_SIZE = 1e5;
1505
1817
  var ViteInspectEnvironmentContext = class {
1506
1818
  inspectContext;
1507
1819
  viteContext;
@@ -1509,6 +1821,9 @@ var ViteInspectEnvironmentContext = class {
1509
1821
  scope;
1510
1822
  pluginCallCount = 0;
1511
1823
  graphModuleIdResolver;
1824
+ modulesList;
1825
+ publicModuleIds = /* @__PURE__ */ new Map();
1826
+ publicModuleIdsRoot;
1512
1827
  constructor(inspectContext, viteContext, env) {
1513
1828
  this.inspectContext = inspectContext;
1514
1829
  this.viteContext = viteContext;
@@ -1532,7 +1847,7 @@ var ViteInspectEnvironmentContext = class {
1532
1847
  this.inspectContext.store.recordTransform(this.scope, id, publicModuleId, {
1533
1848
  ...info,
1534
1849
  plugin_id: pluginId
1535
- }, preTransformCode, pluginCall);
1850
+ }, preTransformCode, pluginCall, trackTransformRequestId(id));
1536
1851
  }
1537
1852
  recordLoad(id, info, plugin) {
1538
1853
  this.invalidateGraphModuleIdResolver();
@@ -1551,7 +1866,7 @@ var ViteInspectEnvironmentContext = class {
1551
1866
  this.inspectContext.store.recordLoad(this.scope, id, publicModuleId, {
1552
1867
  ...info,
1553
1868
  plugin_id: pluginId
1554
- }, pluginCall);
1869
+ }, pluginCall, trackTransformRequestId(id));
1555
1870
  }
1556
1871
  recordLoadCall(id, info, plugin) {
1557
1872
  id = this.normalizeId(id);
@@ -1566,7 +1881,7 @@ var ViteInspectEnvironmentContext = class {
1566
1881
  unchanged: true
1567
1882
  });
1568
1883
  if (!pluginCall) return;
1569
- this.inspectContext.store.recordPluginCall(this.scope, pluginCall);
1884
+ this.inspectContext.store.recordPluginCall(this.scope, pluginCall, trackTransformRequestId(id));
1570
1885
  }
1571
1886
  recordResolveId(id, info, plugin) {
1572
1887
  this.invalidateGraphModuleIdResolver();
@@ -1621,7 +1936,17 @@ var ViteInspectEnvironmentContext = class {
1621
1936
  return this.env.getTopLevelConfig().root;
1622
1937
  }
1623
1938
  getPublicModuleId(id) {
1624
- return normalizeModuleId(this.normalizeId(id), this.getModuleIdBaseRoot());
1939
+ const root = this.getModuleIdBaseRoot();
1940
+ if (root !== this.publicModuleIdsRoot || this.publicModuleIds.size >= MAX_PUBLIC_MODULE_ID_CACHE_SIZE) {
1941
+ this.publicModuleIds.clear();
1942
+ this.publicModuleIdsRoot = root;
1943
+ }
1944
+ let publicId = this.publicModuleIds.get(id);
1945
+ if (publicId === void 0) {
1946
+ publicId = normalizeModuleId(this.normalizeId(id), root);
1947
+ this.publicModuleIds.set(id, publicId);
1948
+ }
1949
+ return publicId;
1625
1950
  }
1626
1951
  async getModuleId(id) {
1627
1952
  const normalizedId = this.getPublicModuleId(id);
@@ -1655,10 +1980,24 @@ var ViteInspectEnvironmentContext = class {
1655
1980
  };
1656
1981
  }
1657
1982
  async getModulesList(pluginCtx) {
1983
+ const store = this.inspectContext.store;
1984
+ await store.flush();
1985
+ const version = store.getChangeVersion();
1986
+ if (!pluginCtx && this.modulesList?.version === version) {
1987
+ this.graphModuleIdResolver ??= this.createGraphModuleIdResolver(this.modulesList.modules);
1988
+ return this.modulesList.modules;
1989
+ }
1658
1990
  const modules = await getModulesList(this, pluginCtx);
1659
- this.graphModuleIdResolver = createGraphModuleIdResolver(modules.map((module) => module.id), this.env.getTopLevelConfig().resolve?.extensions ?? []);
1991
+ this.graphModuleIdResolver = this.createGraphModuleIdResolver(modules);
1992
+ if (!pluginCtx) this.modulesList = {
1993
+ version,
1994
+ modules
1995
+ };
1660
1996
  return modules;
1661
1997
  }
1998
+ createGraphModuleIdResolver(modules) {
1999
+ return createGraphModuleIdResolver(modules.map((module) => module.id), this.env.getTopLevelConfig().resolve?.extensions ?? []);
2000
+ }
1662
2001
  async getGraphModuleIdResolver() {
1663
2002
  if (!this.graphModuleIdResolver) await this.getModulesList();
1664
2003
  return this.graphModuleIdResolver;
@@ -1701,8 +2040,7 @@ var ViteInspectEnvironmentContext = class {
1701
2040
  this.graphModuleIdResolver = void 0;
1702
2041
  }
1703
2042
  };
1704
- //#endregion
1705
- //#region src/node/rpc/inspect-module-updated.ts
2043
+ // src/node/rpc/inspect-module-updated.ts
1706
2044
  const VITE_INSPECT_MODULE_UPDATED_STATE_KEY = "vite:inspect:module-updated";
1707
2045
  function getViteInspectModuleUpdatedState(context) {
1708
2046
  return context.rpc.sharedState.get(VITE_INSPECT_MODULE_UPDATED_STATE_KEY, { initialValue: {
@@ -1716,8 +2054,7 @@ function notifyViteInspectModuleUpdated(state, ids = null) {
1716
2054
  value.updatedAt = Date.now();
1717
2055
  });
1718
2056
  }
1719
- //#endregion
1720
- //#region src/node/rpc/functions/vite-clear-module-transform.ts
2057
+ // src/node/rpc/functions/vite-clear-module-transform.ts
1721
2058
  const viteClearModuleTransform = defineRpcFunction({
1722
2059
  name: "vite:inspect:clear-module-transform",
1723
2060
  type: "action",
@@ -1731,8 +2068,7 @@ const viteClearModuleTransform = defineRpcFunction({
1731
2068
  } };
1732
2069
  }
1733
2070
  });
1734
- //#endregion
1735
- //#region src/node/rpc/functions/vite-env-info.ts
2071
+ // src/node/rpc/functions/vite-env-info.ts
1736
2072
  const viteEnvInfo = defineRpcFunction({
1737
2073
  name: "vite:env-info",
1738
2074
  type: "query",
@@ -1764,8 +2100,7 @@ const viteEnvInfo = defineRpcFunction({
1764
2100
  } };
1765
2101
  }
1766
2102
  });
1767
- //#endregion
1768
- //#region src/node/rpc/functions/vite-get-metadata.ts
2103
+ // src/node/rpc/functions/vite-get-metadata.ts
1769
2104
  const viteGetMetadata = defineRpcFunction({
1770
2105
  name: "vite:inspect:get-metadata",
1771
2106
  type: "query",
@@ -1777,8 +2112,7 @@ const viteGetMetadata = defineRpcFunction({
1777
2112
  return { handler: async () => ctx.getMetadata() };
1778
2113
  }
1779
2114
  });
1780
- //#endregion
1781
- //#region src/node/rpc/functions/vite-get-module-transform-info.ts
2115
+ // src/node/rpc/functions/vite-get-module-transform-info.ts
1782
2116
  const viteGetModuleTransformInfo = defineRpcFunction({
1783
2117
  name: "vite:inspect:get-module-transform-info",
1784
2118
  type: "query",
@@ -1792,8 +2126,7 @@ const viteGetModuleTransformInfo = defineRpcFunction({
1792
2126
  return { handler: async (query, id) => ctx.queryEnv(query).getModuleTransformInfo(id) };
1793
2127
  }
1794
2128
  });
1795
- //#endregion
1796
- //#region src/node/rpc/functions/vite-get-modules-list.ts
2129
+ // src/node/rpc/functions/vite-get-modules-list.ts
1797
2130
  const viteGetModulesList = defineRpcFunction({
1798
2131
  name: "vite:inspect:get-modules-list",
1799
2132
  type: "query",
@@ -1807,8 +2140,7 @@ const viteGetModulesList = defineRpcFunction({
1807
2140
  return { handler: async (query) => ctx.queryEnv(query).getModulesList() };
1808
2141
  }
1809
2142
  });
1810
- //#endregion
1811
- //#region src/node/rpc/functions/vite-get-plugin-details.ts
2143
+ // src/node/rpc/functions/vite-get-plugin-details.ts
1812
2144
  const viteGetPluginDetails = defineRpcFunction({
1813
2145
  name: "vite:inspect:get-plugin-details",
1814
2146
  type: "query",
@@ -1819,8 +2151,7 @@ const viteGetPluginDetails = defineRpcFunction({
1819
2151
  return { handler: async (query, id) => ctx.queryEnv(query).getPluginDetails(Number(id)) };
1820
2152
  }
1821
2153
  });
1822
- //#endregion
1823
- //#region src/node/rpc/functions/vite-get-plugin-metrics.ts
2154
+ // src/node/rpc/functions/vite-get-plugin-metrics.ts
1824
2155
  const viteGetPluginMetrics = defineRpcFunction({
1825
2156
  name: "vite:inspect:get-plugin-metrics",
1826
2157
  type: "query",
@@ -1834,8 +2165,7 @@ const viteGetPluginMetrics = defineRpcFunction({
1834
2165
  return { handler: async (query) => ctx.queryEnv(query).getPluginMetrics() };
1835
2166
  }
1836
2167
  });
1837
- //#endregion
1838
- //#region src/node/rpc/functions/vite-get-server-metrics.ts
2168
+ // src/node/rpc/functions/vite-get-server-metrics.ts
1839
2169
  const viteGetServerMetrics = defineRpcFunction({
1840
2170
  name: "vite:inspect:get-server-metrics",
1841
2171
  type: "query",
@@ -1849,8 +2179,7 @@ const viteGetServerMetrics = defineRpcFunction({
1849
2179
  return { handler: async (query) => ctx.getViteContext(query.vite).data.serverMetrics };
1850
2180
  }
1851
2181
  });
1852
- //#endregion
1853
- //#region src/node/rpc/functions/vite-meta-info.ts
2182
+ // src/node/rpc/functions/vite-meta-info.ts
1854
2183
  const viteMetaInfo = defineRpcFunction({
1855
2184
  name: "vite:meta-info",
1856
2185
  type: "query",
@@ -1867,8 +2196,7 @@ const viteMetaInfo = defineRpcFunction({
1867
2196
  } };
1868
2197
  }
1869
2198
  });
1870
- //#endregion
1871
- //#region src/node/rpc/functions/vite-resolve-id.ts
2199
+ // src/node/rpc/functions/vite-resolve-id.ts
1872
2200
  const viteResolveId = defineRpcFunction({
1873
2201
  name: "vite:inspect:resolve-id",
1874
2202
  type: "query",
@@ -1882,8 +2210,7 @@ const viteResolveId = defineRpcFunction({
1882
2210
  return { handler: async (query, id) => ctx.queryEnv(query).resolveId(id) };
1883
2211
  }
1884
2212
  });
1885
- //#endregion
1886
- //#region src/node/rpc/index.ts
2213
+ // src/node/rpc/index.ts
1887
2214
  const viteRpcFunctions = [viteMetaInfo, viteEnvInfo];
1888
2215
  const inspectRpcFunctions = [
1889
2216
  viteClearModuleTransform,
@@ -1896,104 +2223,7 @@ const inspectRpcFunctions = [
1896
2223
  viteGetServerMetrics
1897
2224
  ];
1898
2225
  [...viteRpcFunctions, ...inspectRpcFunctions];
1899
- //#endregion
1900
- //#region src/node/inspect/server.ts
1901
- const timestampRE = /\bt=\d{13}&?\b/;
1902
- const trailingSeparatorRE = /[?&]$/;
1903
- const transformRequestStorage = new AsyncLocalStorage();
1904
- const environmentInvalidationStates = /* @__PURE__ */ new WeakMap();
1905
- function trackTransformRequestId(id) {
1906
- const request = transformRequestStorage.getStore();
1907
- if (request?.active) request.ids.add(normalizeRequestId(id));
1908
- return request?.active ? request : void 0;
1909
- }
1910
- function isTransformRequestStale(request) {
1911
- return request?.stale === true;
1912
- }
1913
- function setupEnvironmentInvalidation(server, vite) {
1914
- Object.values(server.environments).forEach((env) => {
1915
- if (!vite.isEnvironmentEnabled(env.name)) return;
1916
- const envContext = vite.getEnvContext(env);
1917
- const existingState = environmentInvalidationStates.get(env);
1918
- if (existingState) {
1919
- existingState.contexts.add(envContext);
1920
- return;
1921
- }
1922
- const state = {
1923
- activeRequests: /* @__PURE__ */ new Set(),
1924
- contexts: /* @__PURE__ */ new Set([envContext])
1925
- };
1926
- environmentInvalidationStates.set(env, state);
1927
- const transformRequest = env.transformRequest;
1928
- env.transformRequest = function(...args) {
1929
- const request = {
1930
- active: true,
1931
- ids: /* @__PURE__ */ new Set([normalizeRequestId(args[0])]),
1932
- stale: false
1933
- };
1934
- state.activeRequests.add(request);
1935
- return transformRequestStorage.run(request, () => transformRequest.apply(this, args)).finally(() => {
1936
- request.active = false;
1937
- request.ids.clear();
1938
- state.activeRequests.delete(request);
1939
- });
1940
- };
1941
- const invalidateModule = env.moduleGraph.invalidateModule;
1942
- env.moduleGraph.invalidateModule = function(...args) {
1943
- const mod = args[0];
1944
- const seen = args[1];
1945
- const softInvalidate = args[4] === true;
1946
- const alreadyHardInvalidated = mod?.invalidationState === "HARD_INVALIDATED";
1947
- const shouldInvalidateInspect = !softInvalidate && (!seen?.has(mod) || !alreadyHardInvalidated);
1948
- if (mod) {
1949
- markStaleRequests(state.activeRequests, mod.id, mod.url);
1950
- if (mod.id && shouldInvalidateInspect) for (const context of state.contexts) context.invalidate(mod.id);
1951
- }
1952
- return invalidateModule.apply(this, args);
1953
- };
1954
- });
1955
- }
1956
- function markStaleRequests(requests, ...ids) {
1957
- const normalizedIds = ids.filter((id) => id != null).map(normalizeRequestId);
1958
- for (const request of requests) if (normalizedIds.some((id) => request.ids.has(id))) request.stale = true;
1959
- }
1960
- function normalizeRequestId(id) {
1961
- return id.replace(timestampRE, "").replace(trailingSeparatorRE, "");
1962
- }
1963
- function setupMiddlewarePerformance(vite, middlewares) {
1964
- let firstMiddlewareIndex = -1;
1965
- middlewares.forEach((middleware, index) => {
1966
- const originalHandle = middleware.handle;
1967
- if (typeof originalHandle !== "function" || !originalHandle.name) return;
1968
- middleware.handle = function(...middlewareArgs) {
1969
- let req;
1970
- if (middlewareArgs.length === 4) req = middlewareArgs[1];
1971
- else req = middlewareArgs[0];
1972
- const start = Date.now();
1973
- const url = req.url?.replace(timestampRE, "").replace(trailingSeparatorRE, "") || "/";
1974
- const metrics = vite.data.serverMetrics.middleware[url] ||= [];
1975
- if (firstMiddlewareIndex < 0) firstMiddlewareIndex = index;
1976
- if (index === firstMiddlewareIndex) metrics.length = 0;
1977
- const result = originalHandle.apply(this, middlewareArgs);
1978
- Promise.resolve(result).then(() => {
1979
- const total = Date.now() - start;
1980
- metrics.push({
1981
- self: metrics.length ? Math.max(total - (metrics.at(-1)?.total || 0), 0) : total,
1982
- total,
1983
- name: originalHandle.name
1984
- });
1985
- });
1986
- return result;
1987
- };
1988
- Object.defineProperty(middleware.handle, "name", {
1989
- value: originalHandle.name,
1990
- configurable: true,
1991
- enumerable: true
1992
- });
1993
- });
1994
- }
1995
- //#endregion
1996
- //#region src/node/inspect/hijack.ts
2226
+ // src/node/inspect/hijack.ts
1997
2227
  function hijackHook(plugin, name, wrapper) {
1998
2228
  const hook = plugin[name];
1999
2229
  if (!hook) return;
@@ -2018,118 +2248,105 @@ function hijackHook(plugin, name, wrapper) {
2018
2248
  };
2019
2249
  }
2020
2250
  }
2251
+ function now() {
2252
+ return performance.timeOrigin + performance.now();
2253
+ }
2254
+ function isThenable(value) {
2255
+ return value != null && typeof value.then === "function";
2256
+ }
2257
+ function measureHook(fn, context, args, settled) {
2258
+ const start = now();
2259
+ let resultValue;
2260
+ try {
2261
+ resultValue = fn.apply(context, args);
2262
+ } catch (error) {
2263
+ settled(void 0, error, start, now());
2264
+ throw error;
2265
+ }
2266
+ if (!isThenable(resultValue)) {
2267
+ settled(resultValue, void 0, start, now());
2268
+ return resultValue;
2269
+ }
2270
+ return Promise.resolve(resultValue).then((value) => {
2271
+ settled(value, void 0, start, now());
2272
+ return value;
2273
+ }, (error) => {
2274
+ settled(void 0, error, start, now());
2275
+ throw error;
2276
+ });
2277
+ }
2021
2278
  const hijackedPlugins = /* @__PURE__ */ new WeakSet();
2022
2279
  function hijackPlugin(plugin, ctx) {
2023
2280
  if (hijackedPlugins.has(plugin)) return;
2024
2281
  hijackedPlugins.add(plugin);
2025
- hijackHook(plugin, "transform", async (fn, context, args, order) => {
2282
+ hijackHook(plugin, "transform", (fn, context, args, order) => {
2026
2283
  const code = args[0];
2027
2284
  const id = args[1];
2028
2285
  const transformRequest = trackTransformRequestId(id);
2029
2286
  const envContext = ctx.filter(id) ? ctx.getEnvContext(context?.environment) : void 0;
2030
- let resultValue;
2031
- let error;
2032
- const start = Date.now();
2033
- try {
2034
- resultValue = await fn.apply(context, args);
2035
- } catch (err) {
2036
- error = err;
2037
- }
2038
- const end = Date.now();
2039
- const result = error ? "[Error]" : typeof resultValue === "string" ? resultValue : resultValue?.code?.toString();
2040
- if (envContext && !isTransformRequestStale(transformRequest)) {
2041
- const sourcemaps = typeof resultValue === "string" ? null : resultValue?.map;
2287
+ if (!envContext) return fn.apply(context, args);
2288
+ return measureHook(fn, context, args, (resultValue, error, start, end) => {
2289
+ if (isTransformRequestStale(transformRequest)) return;
2290
+ const result = error ? "[Error]" : typeof resultValue === "string" ? resultValue : resultValue?.code?.toString();
2042
2291
  envContext.recordTransform(id, {
2043
2292
  name: plugin.name,
2044
2293
  result,
2045
2294
  start,
2046
2295
  end,
2047
2296
  order,
2048
- sourcemaps,
2049
2297
  error: error ? parseError(error) : void 0
2050
2298
  }, code, plugin);
2051
- }
2052
- if (error) throw error;
2053
- return resultValue;
2299
+ });
2054
2300
  });
2055
- hijackHook(plugin, "load", async (fn, context, args) => {
2301
+ hijackHook(plugin, "load", (fn, context, args) => {
2056
2302
  const id = args[0];
2057
2303
  const transformRequest = trackTransformRequestId(id);
2058
2304
  const envContext = ctx.getEnvContext(context?.environment);
2059
- let resultValue;
2060
- let error;
2061
- const start = Date.now();
2062
- try {
2063
- resultValue = await fn.apply(context, args);
2064
- } catch (err) {
2065
- error = err;
2066
- }
2067
- const end = Date.now();
2068
- const result = error ? "[Error]" : typeof resultValue === "string" ? resultValue : resultValue?.code;
2069
- const sourcemaps = typeof resultValue === "string" ? null : resultValue?.map;
2070
- const info = {
2071
- name: plugin.name,
2072
- result,
2073
- start,
2074
- end,
2075
- sourcemaps,
2076
- error: error ? parseError(error) : void 0
2077
- };
2078
- if (result != null && envContext && !isTransformRequestStale(transformRequest)) envContext.recordLoad(id, {
2079
- ...info,
2080
- result
2081
- }, plugin);
2082
- else if (envContext && !isTransformRequestStale(transformRequest)) envContext.recordLoadCall(id, {
2083
- name: plugin.name,
2084
- start,
2085
- end,
2086
- error: error ? parseError(error) : void 0
2087
- }, plugin);
2088
- if (error) throw error;
2089
- return resultValue;
2305
+ if (!envContext) return fn.apply(context, args);
2306
+ return measureHook(fn, context, args, (resultValue, error, start, end) => {
2307
+ if (isTransformRequestStale(transformRequest)) return;
2308
+ const result = error ? "[Error]" : typeof resultValue === "string" ? resultValue : resultValue?.code;
2309
+ if (result != null) envContext.recordLoad(id, {
2310
+ name: plugin.name,
2311
+ result,
2312
+ start,
2313
+ end,
2314
+ error: error ? parseError(error) : void 0
2315
+ }, plugin);
2316
+ else envContext.recordLoadCall(id, {
2317
+ name: plugin.name,
2318
+ start,
2319
+ end,
2320
+ error: error ? parseError(error) : void 0
2321
+ }, plugin);
2322
+ });
2090
2323
  });
2091
- hijackHook(plugin, "resolveId", async (fn, context, args) => {
2324
+ hijackHook(plugin, "resolveId", (fn, context, args) => {
2092
2325
  const id = args[0];
2093
2326
  const transformRequest = trackTransformRequestId(id);
2094
2327
  const envContext = ctx.filter(id) ? ctx.getEnvContext(context?.environment) : void 0;
2095
- let resultValue;
2096
- let error;
2097
- const start = Date.now();
2098
- try {
2099
- resultValue = await fn.apply(context, args);
2100
- } catch (err) {
2101
- error = err;
2102
- }
2103
- const end = Date.now();
2104
- if (!envContext) {
2105
- if (error) throw error;
2106
- return resultValue;
2107
- }
2108
- if (isTransformRequestStale(transformRequest)) {
2109
- if (error) throw error;
2110
- return resultValue;
2111
- }
2112
- const result = error ? stringifyError(error) : typeof resultValue === "object" ? resultValue?.id : resultValue;
2113
- if (result && result !== id) envContext.recordResolveId(id, {
2114
- name: plugin.name,
2115
- result,
2116
- start,
2117
- end,
2118
- error
2119
- }, plugin);
2120
- else envContext.recordResolveIdCall(id, {
2121
- name: plugin.name,
2122
- result,
2123
- start,
2124
- end,
2125
- error
2126
- }, plugin);
2127
- if (error) throw error;
2128
- return resultValue;
2328
+ if (!envContext) return fn.apply(context, args);
2329
+ return measureHook(fn, context, args, (resultValue, error, start, end) => {
2330
+ if (isTransformRequestStale(transformRequest)) return;
2331
+ const result = error ? stringifyError(error) : typeof resultValue === "object" ? resultValue?.id : resultValue;
2332
+ if (result) envContext.recordResolveId(id, {
2333
+ name: plugin.name,
2334
+ result,
2335
+ start,
2336
+ end,
2337
+ error
2338
+ }, plugin);
2339
+ else envContext.recordResolveIdCall(id, {
2340
+ name: plugin.name,
2341
+ result,
2342
+ start,
2343
+ end,
2344
+ error
2345
+ }, plugin);
2346
+ });
2129
2347
  });
2130
2348
  }
2131
- //#endregion
2132
- //#region src/node/inspect/plugin.ts
2349
+ // src/node/inspect/plugin.ts
2133
2350
  function DevToolsViteInspect() {
2134
2351
  let inspectContext;
2135
2352
  let inspectContextPromise;
@@ -2257,8 +2474,7 @@ function DevToolsViteInspect() {
2257
2474
  sharedDuringBuild: true
2258
2475
  };
2259
2476
  }
2260
- //#endregion
2261
- //#region src/node/plugin.ts
2477
+ // src/node/plugin.ts
2262
2478
  const VITE_DEVTOOLS_BASE = "/__devtools-vite/";
2263
2479
  function DevToolsViteUI() {
2264
2480
  return [DevToolsViteInspect(), DevToolsViteUIPlugin()];
@@ -2280,5 +2496,4 @@ function DevToolsViteUIPlugin() {
2280
2496
  } }
2281
2497
  };
2282
2498
  }
2283
- //#endregion
2284
2499
  export { DevToolsViteInspect, DevToolsViteUI, DevToolsViteUIPlugin };