Type serializer read/write contexts with positional tuples#21201
Conversation
🦋 Changeset detectedLatest commit: fa4b2c2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
|
This PR is packaged and the instant preview is available (fa4b2c2). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@fa4b2c2
yarn add -D webpack@https://pkg.pr.new/webpack@fa4b2c2
pnpm add -D webpack@https://pkg.pr.new/webpack@fa4b2c2 |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #21201 +/- ##
==========================================
+ Coverage 92.25% 92.74% +0.49%
==========================================
Files 591 591
Lines 64385 64289 -96
Branches 17858 17858
==========================================
+ Hits 59397 59628 +231
+ Misses 4988 4661 -327
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merging this PR will improve performance by 47.36%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Memory | benchmark "wasm-modules-async", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
330.4 KB | 186.6 KB | +77.07% |
| ⚡ | Memory | benchmark "wasm-modules-sync", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
354.8 KB | 256.4 KB | +38.38% |
| ⚡ | Memory | benchmark "asset-modules-bytes", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' |
322.5 KB | 246.9 KB | +30.59% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing claude/types-coverage-analysis-hwx7ir (fa4b2c2) with main (4fa6844)
This reverts commit 3de9151.
generate-types (webpack/tooling) dropped `infer` in tuple element/rest slots of conditional `extends` clauses, emitting invalid types.d.ts. The patch renders extends-position tuples element-wise. Kept for upstream contribution only; not wired into install. Refs #21191.
Preserves the positional ObjectSerializerContext/ObjectDeserializerContext work (per-position write and read via a no-alloc `rest` accessor), with built-in serializers and ConstDependency converted. Kept as a patch since the generated types only validate after the tooling infer-in-tuple fix ships.
ObjectSerializerContext<[A, B, C]> / ObjectDeserializerContext<[A, B, C]> make each write/read position-checked: write chains and advances by returning the next context; read advances via a no-alloc `rest` accessor. Converts the 51 leaf dependency classes (fixed tuples where the shape is fixed, open-array unions for looped serializers), the built-in JS-type serializers, and ConstDependency. Base classes stay loose so subclass overrides remain compatible. Requires the tooling infer-in-tuple fix to regenerate types.d.ts.
Extends positional tuple typing (ObjectSerializerContext<[A,B,C]> / ObjectDeserializerContext<[A,B,C]>) to the leaf module classes (ContextModule, ExternalModule, RawModule, RawDataUrlModule, container/ sharing modules, CssModule, AsyncWasmModule, ...), the error classes, and container/sharing dependencies. Module/NormalModule/InitFragment stay loose as subclassed bases. ProvideSharedDependency left loose pending a separate fix for its pre-existing serialize/deserialize mismatch.
Its serialize writes shareScope, name, request, version, eager, but the static deserialize passed the reads straight into the constructor (shareScope, name, version, request, eager), swapping version and request, and did an extra read() that misaligned the inherited loc fields. Read into named locals in wire order and reorder for the constructor; also apply positional tuple typing so the slot order is type-checked. Adds a round-trip unit test.
Point the tooling dependency at the merged commit that fixes infer in tuple extends clauses, so types.d.ts regenerates upstream. Removes the local patches/ workaround and its Project Overview entry.
Cover two serializers that used the destructured write/read style and were missed earlier: RestoreProvidedData (fixed positional tuple) and LazySet (open-array union, like SetObjectSerializer). Also syncs the generated types.d.ts/yarn.lock after the rebase onto main.
5b4057d to
fa4b2c2
Compare
Summary
Types webpack's serializer read/write contexts (
ObjectSerializerContext/ObjectDeserializerContext) with positional tuples, so eachwrite/readis type-checked by slot across the dependency and cacheable-module classes:write(value: Head<T>)returns the context forTail<T>(chain to advance), and reads advance via a no-allocationrestaccessor. This catches wrong-order or wrong-type writes/reads at compile time.Typing
ProvideSharedDependencythis way surfaced a real bug — itsdeserializepassed reads straight into the constructor, swappingversion/request, and did an extraread()that misaligned the inherited loc fields. Fixed here with a round-trip test.This relied on a
generate-typesfix (it previously droppedinferinside tupleextendsclauses); that fix is now merged inwebpack/toolingand this PR pins thetoolingdependency to it, sotypes.d.tsregenerates cleanly andlint:special/lint:types-testpass.What kind of change does this PR introduce?
feat (typing) plus a fix.
Did you add tests for your changes?
Yes —
test/ProvideSharedDependency.unittest.jsround-trips the dependency and assertsversion/requestsurvive serialization (fails on the old code, passes on the fix).Does this PR introduce a breaking change?
No runtime or cache break: the serialize wire format is unchanged and
CURRENT_VERSIONis not bumped, so existing persistent caches stay valid. Type-only impact: the exportedObjectSerializerContext/ObjectDeserializerContextgenerics now constrainTto a tuple/array; default usage (no type argument) is unaffected, but third-party code that parameterized them with a non-array type (e.g.ObjectSerializerContext<string>) would need to update.If relevant, what needs to be documented once your changes are merged or what have you already documented?
n/a
Use of AI
AI (Claude) was used to design and implement the positional tuple typing, roll it out across the serializer/dependency/module classes, diagnose and fix the
ProvideSharedDependencybug, and prepare the upstreamtoolingfix. All changes were reviewed and verified (tsc, targeted unit tests, and cache round-trip checks) before committing.https://claude.ai/code/session_015uWCgLZJknabrCqSPGkSam