@weapp-vite/hmr 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -170,14 +170,25 @@ export declare class HmrDeliveryCoordinator {
170
170
  private pending;
171
171
  private running?;
172
172
  private failed;
173
+ private failure;
174
+ private revision;
173
175
  private pendingBytes;
176
+ private readonly disposals;
177
+ private readonly disposalFailures;
178
+ private closing?;
174
179
  constructor(onError: (error: unknown) => void, requestResynchronization?: (() => void) | undefined, maxPending?: number);
175
180
  enqueue(task: HmrDeliveryTask): void;
176
181
  retry(): void;
177
182
  reset(): void;
183
+ /** 等待已入队交付及等待期间的新代次;失败向调用方传播,不重试或改变队列。 */
184
+ whenSettled(): Promise<void>;
178
185
  close(): Promise<void>;
179
186
  private prepare;
180
187
  private dispose;
188
+ private reportError;
189
+ private recordDisposalFailure;
190
+ private waitForDisposals;
191
+ private throwDisposalFailures;
181
192
  private drain;
182
193
  }
183
194
  //#endregion
@@ -186,6 +197,10 @@ export declare class CompilerHmrResyncError extends Error {
186
197
  readonly files: readonly string[];
187
198
  constructor(files: readonly string[], message: string);
188
199
  }
200
+ /** @internal */
201
+ export declare class HmrPreparationCleanupError extends AggregateError {
202
+ constructor(failure: unknown, cleanupErrors: readonly unknown[]);
203
+ }
189
204
  //#endregion
190
205
  //#region src/retention.d.ts
191
206
  export declare const maxRetainedDeltaCount = 1000;
@@ -222,4 +237,5 @@ export declare class HmrTransaction<T> {
222
237
  private isDisposed;
223
238
  private deliver;
224
239
  }
225
- //#endregion
240
+ //#endregion
241
+ export {};
package/dist/index.mjs CHANGED
@@ -174,8 +174,21 @@ var CompilerHmrResyncError = class extends Error {
174
174
  this.name = "CompilerHmrResyncError";
175
175
  }
176
176
  };
177
+ /** @internal */
178
+ var HmrPreparationCleanupError = class extends AggregateError {
179
+ constructor(failure, cleanupErrors) {
180
+ super([failure, ...cleanupErrors], "HMR preparation failed and resource disposal failed", { cause: failure });
181
+ this.name = "HmrPreparationCleanupError";
182
+ }
183
+ };
177
184
  //#endregion
178
185
  //#region src/compilerHost.ts
186
+ /** 部分准备失败后仍尝试全部释放,保留最初的拒绝及每个清理错误。 */
187
+ async function rejectPreparations(results, failure) {
188
+ const failures = (await Promise.allSettled(results.map((result) => Promise.resolve().then(() => result.dispose?.())))).flatMap((result) => result.status === "rejected" ? [result.reason] : []);
189
+ if (failures.length) throw new HmrPreparationCleanupError(failure, failures);
190
+ throw failure;
191
+ }
179
192
  /** 只保存宿主实际提供的输入;模块身份和框架语义由适配器决定。 */
180
193
  var HmrCompilerHost = class {
181
194
  options;
@@ -271,17 +284,11 @@ var HmrCompilerHost = class {
271
284
  const settled = await Promise.allSettled(pending);
272
285
  const results = settled.flatMap((result) => result.status === "fulfilled" ? [result.value] : []);
273
286
  const failure = settled.find((result) => result.status === "rejected");
274
- if (failure?.status === "rejected") {
275
- await Promise.allSettled(results.map((result) => result.dispose?.()));
276
- throw failure.reason;
277
- }
287
+ if (failure?.status === "rejected") return rejectPreparations(results, failure.reason);
278
288
  const assets = /* @__PURE__ */ new Set();
279
289
  for (const result of results) for (const asset of result.assets ?? []) {
280
290
  const file = asset.fileName.replaceAll("\\", "/");
281
- if (assets.has(file)) {
282
- await Promise.allSettled(results.map((result) => result.dispose?.()));
283
- throw new CompilerHmrResyncError(request.changedFiles, `Compiler HMR asset has multiple providers: ${file}`);
284
- }
291
+ if (assets.has(file)) return rejectPreparations(results, new CompilerHmrResyncError(request.changedFiles, `Compiler HMR asset has multiple providers: ${file}`));
285
292
  assets.add(file);
286
293
  }
287
294
  return results;
@@ -306,14 +313,19 @@ var HmrDeliveryCoordinator = class {
306
313
  pending = [];
307
314
  running;
308
315
  failed = false;
316
+ failure;
317
+ revision = 0;
309
318
  pendingBytes = 0;
319
+ disposals = /* @__PURE__ */ new Set();
320
+ disposalFailures = [];
321
+ closing;
310
322
  constructor(onError, requestResynchronization, maxPending = maxRetainedDeltaCount) {
311
323
  this.onError = onError;
312
324
  this.requestResynchronization = requestResynchronization;
313
325
  this.maxPending = maxPending;
314
326
  }
315
327
  enqueue(task) {
316
- if (this.closed) return;
328
+ if (this.closed || this.disposalFailures.length) return;
317
329
  if (this.failed && this.requestResynchronization) {
318
330
  this.requestResynchronization();
319
331
  return;
@@ -326,57 +338,114 @@ var HmrDeliveryCoordinator = class {
326
338
  task,
327
339
  generation: this.generation
328
340
  };
341
+ this.revision += 1;
329
342
  this.prepare(item);
330
343
  this.pending.push(item);
331
344
  this.pendingBytes += task.bytes ?? 0;
332
345
  this.retry();
333
346
  }
334
347
  retry() {
348
+ if (this.closed || this.disposalFailures.length) return;
335
349
  this.failed = false;
350
+ this.failure = void 0;
351
+ this.revision += 1;
336
352
  if (!this.running && !this.closed) this.running = this.drain().finally(() => {
337
353
  this.running = void 0;
338
- if (this.pending.length && !this.failed && !this.closed) this.retry();
354
+ if (this.pending.length && !this.failed && !this.closed && !this.disposalFailures.length) this.retry();
339
355
  });
340
356
  }
341
357
  reset() {
358
+ this.revision += 1;
342
359
  this.generation += 1;
343
360
  for (const item of this.pending) this.dispose(item);
344
361
  this.pending = [];
345
362
  this.pendingBytes = 0;
346
363
  this.failed = false;
364
+ this.failure = void 0;
365
+ }
366
+ /** 等待已入队交付及等待期间的新代次;失败向调用方传播,不重试或改变队列。 */
367
+ async whenSettled() {
368
+ for (;;) {
369
+ const revision = this.revision;
370
+ try {
371
+ await this.running;
372
+ await this.waitForDisposals();
373
+ } catch (error) {
374
+ throw this.failed ? this.failure : error;
375
+ }
376
+ this.throwDisposalFailures();
377
+ if (this.failed) throw this.failure;
378
+ if (this.closed) throw new Error("HMR delivery coordinator closed before settlement");
379
+ if (revision === this.revision && !this.running && this.pending.length === 0 && this.disposals.size === 0) return;
380
+ }
347
381
  }
348
- async close() {
349
- this.closed = true;
350
- this.reset();
351
- await this.running;
382
+ close() {
383
+ if (!this.closing) {
384
+ this.closed = true;
385
+ this.reset();
386
+ this.closing = (async () => {
387
+ await this.running;
388
+ await this.waitForDisposals();
389
+ this.throwDisposalFailures();
390
+ })();
391
+ }
392
+ return this.closing;
352
393
  }
353
394
  prepare(item) {
354
395
  if (!item.preparation) {
355
396
  item.preparation = item.task.prepare();
356
- item.preparation.catch(() => {
397
+ item.preparation.catch((error) => {
357
398
  item.preparation = void 0;
399
+ if (error instanceof HmrPreparationCleanupError) this.recordDisposalFailure(error);
358
400
  });
359
401
  }
360
402
  return item.preparation;
361
403
  }
362
- async dispose(item) {
363
- if (item.disposed) return;
364
- item.disposed = true;
404
+ dispose(item) {
405
+ if (item.disposal) return item.disposal;
406
+ item.disposal = (async () => {
407
+ let preparation;
408
+ try {
409
+ preparation = await item.preparation;
410
+ } catch {
411
+ return;
412
+ }
413
+ await preparation?.dispose?.();
414
+ })();
415
+ this.disposals.add(item.disposal);
416
+ item.disposal.then(() => this.disposals.delete(item.disposal), (error) => {
417
+ this.disposals.delete(item.disposal);
418
+ this.recordDisposalFailure(error);
419
+ });
420
+ return item.disposal;
421
+ }
422
+ reportError(error) {
365
423
  try {
366
- await (await item.preparation)?.dispose?.();
424
+ this.onError(error);
367
425
  } catch {}
368
426
  }
427
+ recordDisposalFailure(error) {
428
+ this.disposalFailures.push(error);
429
+ this.reportError(error);
430
+ }
431
+ async waitForDisposals() {
432
+ while (this.disposals.size) await Promise.allSettled(this.disposals);
433
+ }
434
+ throwDisposalFailures() {
435
+ if (this.disposalFailures.length === 1) throw this.disposalFailures[0];
436
+ if (this.disposalFailures.length > 1) throw new AggregateError(this.disposalFailures, "HMR delivery disposal failed", { cause: this.disposalFailures[0] });
437
+ }
369
438
  async drain() {
370
- while (!this.closed && this.pending.length) {
439
+ while (!this.closed && !this.disposalFailures.length && this.pending.length) {
371
440
  const item = this.pending[0];
372
441
  try {
373
442
  const prepared = await this.prepare(item);
374
- if (item.generation !== this.generation) continue;
443
+ if (item.generation !== this.generation || this.disposalFailures.length) continue;
375
444
  if (!item.committed) {
376
445
  await prepared.commit();
377
446
  item.committed = true;
378
447
  }
379
- if (item.generation !== this.generation) continue;
448
+ if (item.generation !== this.generation || this.disposalFailures.length) continue;
380
449
  await prepared.publish();
381
450
  if (this.pending[0] === item) {
382
451
  this.pending.shift();
@@ -384,10 +453,16 @@ var HmrDeliveryCoordinator = class {
384
453
  }
385
454
  await this.dispose(item);
386
455
  } catch (error) {
456
+ if (this.disposalFailures.includes(error)) return;
387
457
  if (item.generation === this.generation && !this.closed) {
388
458
  this.failed = true;
389
- this.onError(error);
390
- if (item.generation === this.generation && this.pending.length > 1) this.requestResynchronization?.();
459
+ this.failure = error;
460
+ this.reportError(error);
461
+ if (item.generation === this.generation && this.pending.length > 1) try {
462
+ this.requestResynchronization?.();
463
+ } catch (resyncError) {
464
+ this.reportError(resyncError);
465
+ }
391
466
  return;
392
467
  }
393
468
  }
@@ -467,4 +542,4 @@ var HmrTransaction = class {
467
542
  }
468
543
  };
469
544
  //#endregion
470
- export { CompilerHmrResyncError, HmrAssetStore, HmrCompilerHost, HmrDeliveryCoordinator, HmrTransaction, captureHmrBatch, composeHmrSourceMaps, maxRetainedDeltaBytes, maxRetainedDeltaCount, normalizeHmrSourceMap, shouldResetStatefulHmrRetention, transformHmrBatch, transformHmrPatch };
545
+ export { CompilerHmrResyncError, HmrAssetStore, HmrCompilerHost, HmrDeliveryCoordinator, HmrPreparationCleanupError, HmrTransaction, captureHmrBatch, composeHmrSourceMaps, maxRetainedDeltaBytes, maxRetainedDeltaCount, normalizeHmrSourceMap, shouldResetStatefulHmrRetention, transformHmrBatch, transformHmrPatch };
package/package.json CHANGED
@@ -1,14 +1,24 @@
1
1
  {
2
2
  "name": "@weapp-vite/hmr",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.2.2",
5
5
  "description": "小程序宿主共享的编译批次、资产提交与交付协调内核",
6
6
  "license": "MIT",
7
+ "homepage": "https://github.com/weapp-vite/weapp-vite#readme",
7
8
  "repository": {
8
9
  "type": "git",
9
10
  "url": "git+https://github.com/weapp-vite/weapp-vite.git",
10
11
  "directory": "packages/hmr"
11
12
  },
13
+ "bugs": {
14
+ "url": "https://github.com/weapp-vite/weapp-vite/issues"
15
+ },
16
+ "keywords": [
17
+ "weapp-vite",
18
+ "weapp",
19
+ "mini-program",
20
+ "hmr"
21
+ ],
12
22
  "sideEffects": false,
13
23
  "exports": {
14
24
  ".": {
@@ -37,7 +47,9 @@
37
47
  "build": "tsdown",
38
48
  "typecheck": "tsc --noEmit -p tsconfig.json",
39
49
  "test": "vitest run -c vitest.config.ts",
40
- "test:types": "tsd --files \"test-d/**/*.test-d.ts\"",
50
+ "test:pure": "vitest run -c vitest.config.ts --coverage.enabled=false --maxWorkers=1",
51
+ "test:types": "pnpm run test:types:check",
52
+ "test:types:check": "tsd --files \"test-d/**/*.test-d.ts\"",
41
53
  "lint": "eslint ."
42
54
  }
43
55
  }