@weapp-vite/hmr 0.2.1 → 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 +12 -1
- package/dist/index.mjs +73 -27
- package/package.json +2 -1
package/dist/index.d.mts
CHANGED
|
@@ -174,6 +174,8 @@ export declare class HmrDeliveryCoordinator {
|
|
|
174
174
|
private revision;
|
|
175
175
|
private pendingBytes;
|
|
176
176
|
private readonly disposals;
|
|
177
|
+
private readonly disposalFailures;
|
|
178
|
+
private closing?;
|
|
177
179
|
constructor(onError: (error: unknown) => void, requestResynchronization?: (() => void) | undefined, maxPending?: number);
|
|
178
180
|
enqueue(task: HmrDeliveryTask): void;
|
|
179
181
|
retry(): void;
|
|
@@ -183,6 +185,10 @@ export declare class HmrDeliveryCoordinator {
|
|
|
183
185
|
close(): Promise<void>;
|
|
184
186
|
private prepare;
|
|
185
187
|
private dispose;
|
|
188
|
+
private reportError;
|
|
189
|
+
private recordDisposalFailure;
|
|
190
|
+
private waitForDisposals;
|
|
191
|
+
private throwDisposalFailures;
|
|
186
192
|
private drain;
|
|
187
193
|
}
|
|
188
194
|
//#endregion
|
|
@@ -191,6 +197,10 @@ export declare class CompilerHmrResyncError extends Error {
|
|
|
191
197
|
readonly files: readonly string[];
|
|
192
198
|
constructor(files: readonly string[], message: string);
|
|
193
199
|
}
|
|
200
|
+
/** @internal */
|
|
201
|
+
export declare class HmrPreparationCleanupError extends AggregateError {
|
|
202
|
+
constructor(failure: unknown, cleanupErrors: readonly unknown[]);
|
|
203
|
+
}
|
|
194
204
|
//#endregion
|
|
195
205
|
//#region src/retention.d.ts
|
|
196
206
|
export declare const maxRetainedDeltaCount = 1000;
|
|
@@ -227,4 +237,5 @@ export declare class HmrTransaction<T> {
|
|
|
227
237
|
private isDisposed;
|
|
228
238
|
private deliver;
|
|
229
239
|
}
|
|
230
|
-
//#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;
|
|
@@ -310,13 +317,15 @@ var HmrDeliveryCoordinator = class {
|
|
|
310
317
|
revision = 0;
|
|
311
318
|
pendingBytes = 0;
|
|
312
319
|
disposals = /* @__PURE__ */ new Set();
|
|
320
|
+
disposalFailures = [];
|
|
321
|
+
closing;
|
|
313
322
|
constructor(onError, requestResynchronization, maxPending = maxRetainedDeltaCount) {
|
|
314
323
|
this.onError = onError;
|
|
315
324
|
this.requestResynchronization = requestResynchronization;
|
|
316
325
|
this.maxPending = maxPending;
|
|
317
326
|
}
|
|
318
327
|
enqueue(task) {
|
|
319
|
-
if (this.closed) return;
|
|
328
|
+
if (this.closed || this.disposalFailures.length) return;
|
|
320
329
|
if (this.failed && this.requestResynchronization) {
|
|
321
330
|
this.requestResynchronization();
|
|
322
331
|
return;
|
|
@@ -336,12 +345,13 @@ var HmrDeliveryCoordinator = class {
|
|
|
336
345
|
this.retry();
|
|
337
346
|
}
|
|
338
347
|
retry() {
|
|
348
|
+
if (this.closed || this.disposalFailures.length) return;
|
|
339
349
|
this.failed = false;
|
|
340
350
|
this.failure = void 0;
|
|
341
351
|
this.revision += 1;
|
|
342
352
|
if (!this.running && !this.closed) this.running = this.drain().finally(() => {
|
|
343
353
|
this.running = void 0;
|
|
344
|
-
if (this.pending.length && !this.failed && !this.closed) this.retry();
|
|
354
|
+
if (this.pending.length && !this.failed && !this.closed && !this.disposalFailures.length) this.retry();
|
|
345
355
|
});
|
|
346
356
|
}
|
|
347
357
|
reset() {
|
|
@@ -359,26 +369,34 @@ var HmrDeliveryCoordinator = class {
|
|
|
359
369
|
const revision = this.revision;
|
|
360
370
|
try {
|
|
361
371
|
await this.running;
|
|
362
|
-
await
|
|
372
|
+
await this.waitForDisposals();
|
|
363
373
|
} catch (error) {
|
|
364
374
|
throw this.failed ? this.failure : error;
|
|
365
375
|
}
|
|
366
|
-
|
|
376
|
+
this.throwDisposalFailures();
|
|
367
377
|
if (this.failed) throw this.failure;
|
|
378
|
+
if (this.closed) throw new Error("HMR delivery coordinator closed before settlement");
|
|
368
379
|
if (revision === this.revision && !this.running && this.pending.length === 0 && this.disposals.size === 0) return;
|
|
369
380
|
}
|
|
370
381
|
}
|
|
371
|
-
|
|
372
|
-
this.
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
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;
|
|
376
393
|
}
|
|
377
394
|
prepare(item) {
|
|
378
395
|
if (!item.preparation) {
|
|
379
396
|
item.preparation = item.task.prepare();
|
|
380
|
-
item.preparation.catch(() => {
|
|
397
|
+
item.preparation.catch((error) => {
|
|
381
398
|
item.preparation = void 0;
|
|
399
|
+
if (error instanceof HmrPreparationCleanupError) this.recordDisposalFailure(error);
|
|
382
400
|
});
|
|
383
401
|
}
|
|
384
402
|
return item.preparation;
|
|
@@ -386,25 +404,48 @@ var HmrDeliveryCoordinator = class {
|
|
|
386
404
|
dispose(item) {
|
|
387
405
|
if (item.disposal) return item.disposal;
|
|
388
406
|
item.disposal = (async () => {
|
|
407
|
+
let preparation;
|
|
389
408
|
try {
|
|
390
|
-
|
|
391
|
-
} catch {
|
|
409
|
+
preparation = await item.preparation;
|
|
410
|
+
} catch {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
await preparation?.dispose?.();
|
|
392
414
|
})();
|
|
393
415
|
this.disposals.add(item.disposal);
|
|
394
|
-
item.disposal.then(() => this.disposals.delete(item.disposal))
|
|
416
|
+
item.disposal.then(() => this.disposals.delete(item.disposal), (error) => {
|
|
417
|
+
this.disposals.delete(item.disposal);
|
|
418
|
+
this.recordDisposalFailure(error);
|
|
419
|
+
});
|
|
395
420
|
return item.disposal;
|
|
396
421
|
}
|
|
422
|
+
reportError(error) {
|
|
423
|
+
try {
|
|
424
|
+
this.onError(error);
|
|
425
|
+
} catch {}
|
|
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
|
+
}
|
|
397
438
|
async drain() {
|
|
398
|
-
while (!this.closed && this.pending.length) {
|
|
439
|
+
while (!this.closed && !this.disposalFailures.length && this.pending.length) {
|
|
399
440
|
const item = this.pending[0];
|
|
400
441
|
try {
|
|
401
442
|
const prepared = await this.prepare(item);
|
|
402
|
-
if (item.generation !== this.generation) continue;
|
|
443
|
+
if (item.generation !== this.generation || this.disposalFailures.length) continue;
|
|
403
444
|
if (!item.committed) {
|
|
404
445
|
await prepared.commit();
|
|
405
446
|
item.committed = true;
|
|
406
447
|
}
|
|
407
|
-
if (item.generation !== this.generation) continue;
|
|
448
|
+
if (item.generation !== this.generation || this.disposalFailures.length) continue;
|
|
408
449
|
await prepared.publish();
|
|
409
450
|
if (this.pending[0] === item) {
|
|
410
451
|
this.pending.shift();
|
|
@@ -412,11 +453,16 @@ var HmrDeliveryCoordinator = class {
|
|
|
412
453
|
}
|
|
413
454
|
await this.dispose(item);
|
|
414
455
|
} catch (error) {
|
|
456
|
+
if (this.disposalFailures.includes(error)) return;
|
|
415
457
|
if (item.generation === this.generation && !this.closed) {
|
|
416
458
|
this.failed = true;
|
|
417
459
|
this.failure = error;
|
|
418
|
-
this.
|
|
419
|
-
if (item.generation === this.generation && this.pending.length > 1)
|
|
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
|
+
}
|
|
420
466
|
return;
|
|
421
467
|
}
|
|
422
468
|
}
|
|
@@ -496,4 +542,4 @@ var HmrTransaction = class {
|
|
|
496
542
|
}
|
|
497
543
|
};
|
|
498
544
|
//#endregion
|
|
499
|
-
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,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-vite/hmr",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.2",
|
|
5
5
|
"description": "小程序宿主共享的编译批次、资产提交与交付协调内核",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/weapp-vite/weapp-vite#readme",
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
"build": "tsdown",
|
|
48
48
|
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
49
49
|
"test": "vitest run -c vitest.config.ts",
|
|
50
|
+
"test:pure": "vitest run -c vitest.config.ts --coverage.enabled=false --maxWorkers=1",
|
|
50
51
|
"test:types": "pnpm run test:types:check",
|
|
51
52
|
"test:types:check": "tsd --files \"test-d/**/*.test-d.ts\"",
|
|
52
53
|
"lint": "eslint ."
|