GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor: type more cache serializers by position (#23072) · webpack/webpack@13b3a61

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .changeset
    • 235-typed-small-serializers.md
  • lib
    • cache
      • PackFileCacheStrategy.js
      • ResolverCachePlugin.js
    • dependencies
      • esm
        • TopLevelAwaitDependency.js
      • html
        • HtmlSourceDependency.js
      • import
        • ImportContextDependency.js
    • json
      • JsonData.js
    • optimize
      • InlineExports.js
    • util
      • registerExternalSerializer.js
  • test/unitCases
    • PackFileCacheStrategy.unittest.js
  • types.d.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.changeset/235-typed-small-serializers.md‎

Copy file name to clipboard
+5Lines changed: 5 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Type more cache serializers by position instead of with `any`.
Collapse file

‎lib/cache/PackFileCacheStrategy.js‎

Copy file name to clipboardExpand all lines: lib/cache/PackFileCacheStrategy.js
+26-16Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ const UNREFERENCED_FILE = "unreferenced.json";
5454
// in which case the recorded time describes the previous file and must not be trusted.
5555
const CLEANUP_RECENT_WRITE_PERIOD = 60 * 1000;
5656

57+
/**
58+
* The fields a pack container writes in order; its `data` follows them lazily.
59+
* @typedef {[PackContainer["version"], PackContainer["buildSnapshot"], PackContainer["buildDependencies"], PackContainer["resolveResults"], PackContainer["resolveBuildDependenciesSnapshot"]]} PackContainerFields
60+
*/
61+
5762
class PackContainer {
5863
/**
5964
* Creates an instance of PackContainer.
@@ -88,29 +93,34 @@ class PackContainer {
8893

8994
/**
9095
* Serializes this instance into the provided serializer context.
91-
* @param {ObjectSerializerContext} context context
96+
* @param {ObjectSerializerContext<PackContainerFields>} context context
9297
*/
93-
serialize({ write, writeLazy }) {
94-
write(this.version);
95-
write(this.buildSnapshot);
96-
write(this.buildDependencies);
97-
write(this.resolveResults);
98-
write(this.resolveBuildDependenciesSnapshot);
98+
serialize(context) {
99+
context
100+
.write(this.version)
101+
.write(this.buildSnapshot)
102+
.write(this.buildDependencies)
103+
.write(this.resolveResults)
104+
.write(this.resolveBuildDependenciesSnapshot);
99105
/** @type {NonNullable<ObjectSerializerContext["writeLazy"]>} */
100-
(writeLazy)(this.data);
106+
(context.writeLazy)(this.data);
101107
}
102108

103109
/**
104110
* Restores this instance from the provided deserializer context.
105-
* @param {ObjectDeserializerContext} context context
111+
* @param {ObjectDeserializerContext<[...PackContainerFields, PackContainer["data"]]>} context context
106112
*/
107-
deserialize({ read }) {
108-
this.version = read();
109-
this.buildSnapshot = read();
110-
this.buildDependencies = read();
111-
this.resolveResults = read();
112-
this.resolveBuildDependenciesSnapshot = read();
113-
this.data = read();
113+
deserialize(context) {
114+
this.version = context.read();
115+
const c1 = context.rest;
116+
this.buildSnapshot = c1.read();
117+
const c2 = c1.rest;
118+
this.buildDependencies = c2.read();
119+
const c3 = c2.rest;
120+
this.resolveResults = c3.read();
121+
const c4 = c3.rest;
122+
this.resolveBuildDependenciesSnapshot = c4.read();
123+
this.data = c4.rest.read();
114124
}
115125
}
116126

Collapse file

‎lib/cache/ResolverCachePlugin.js‎

Copy file name to clipboardExpand all lines: lib/cache/ResolverCachePlugin.js
+7-12Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ const makeSerializable = require("../util/makeSerializable");
2525
* SnapshotOptions
2626
* } from "../fs/FileSystemInfo"
2727
*/
28-
/**
29-
* @import {
30-
* ObjectDeserializerContext,
31-
* ObjectSerializerContext
32-
* } from "../serialization/ObjectMiddleware"
33-
*/
28+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[CacheEntry["result"], CacheEntry["snapshot"]]>} ObjectDeserializerContext */
29+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[CacheEntry["result"], CacheEntry["snapshot"]]>} ObjectSerializerContext */
3430

3531
/**
3632
* Defines the sync hook type used by this module.
@@ -57,18 +53,17 @@ class CacheEntry {
5753
* Serializes this instance into the provided serializer context.
5854
* @param {ObjectSerializerContext} context context
5955
*/
60-
serialize({ write }) {
61-
write(this.result);
62-
write(this.snapshot);
56+
serialize(context) {
57+
context.write(this.result).write(this.snapshot);
6358
}
6459

6560
/**
6661
* Restores this instance from the provided deserializer context.
6762
* @param {ObjectDeserializerContext} context context
6863
*/
69-
deserialize({ read }) {
70-
this.result = read();
71-
this.snapshot = read();
64+
deserialize(context) {
65+
this.result = context.read();
66+
this.snapshot = context.rest.read();
7267
}
7368
}
7469

Collapse file

‎lib/dependencies/esm/TopLevelAwaitDependency.js‎

Copy file name to clipboardExpand all lines: lib/dependencies/esm/TopLevelAwaitDependency.js
+7-13Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ const NullDependency = require("../core/NullDependency");
1212
/** @import Dependency from "../../graph/Dependency" */
1313
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
1414
/** @import { Range } from "../../javascript/JavascriptParser" */
15-
/**
16-
* @import {
17-
* ObjectDeserializerContext,
18-
* ObjectSerializerContext
19-
* } from "../../serialization/ObjectMiddleware"
20-
*/
15+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[TopLevelAwaitDependency["range"], TopLevelAwaitDependency["argumentRange"]]>} ObjectDeserializerContext */
16+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[TopLevelAwaitDependency["range"], TopLevelAwaitDependency["argumentRange"]]>} ObjectSerializerContext */
2117

2218
/**
2319
* Marks a top-level `await` expression so it can be rewritten to `(yield …)`
@@ -41,9 +37,7 @@ class TopLevelAwaitDependency extends NullDependency {
4137
* @param {ObjectSerializerContext} context context
4238
*/
4339
serialize(context) {
44-
const { write } = context;
45-
write(this.range);
46-
write(this.argumentRange);
40+
context.write(this.range).write(this.argumentRange);
4741
super.serialize(context);
4842
}
4943

@@ -52,10 +46,10 @@ class TopLevelAwaitDependency extends NullDependency {
5246
* @param {ObjectDeserializerContext} context context
5347
*/
5448
deserialize(context) {
55-
const { read } = context;
56-
this.range = read();
57-
this.argumentRange = read();
58-
super.deserialize(context);
49+
this.range = context.read();
50+
const c1 = context.rest;
51+
this.argumentRange = c1.read();
52+
super.deserialize(c1.rest);
5953
}
6054
}
6155

Collapse file

‎lib/dependencies/html/HtmlSourceDependency.js‎

Copy file name to clipboardExpand all lines: lib/dependencies/html/HtmlSourceDependency.js
+21-21Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@ const ModuleDependency = require("../core/ModuleDependency");
2525
/** @import { NormalModuleBuildInfo } from "../../module/NormalModule" */
2626
/** @import { HtmlModuleBuildInfo } from "../../html/HtmlModule" */
2727
/** @import { Range } from "../../javascript/JavascriptParser" */
28-
/**
29-
* @import {
30-
* ObjectDeserializerContext,
31-
* ObjectSerializerContext
32-
* } from "../../serialization/ObjectMiddleware"
33-
*/
28+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[HtmlSourceDependency["prefetch"], HtmlSourceDependency["preload"], HtmlSourceDependency["fetchPriority"], HtmlSourceDependency["asAttribute"], HtmlSourceDependency["typeAttribute"], HtmlSourceDependency["mediaAttribute"]]>} ObjectDeserializerContext */
29+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[HtmlSourceDependency["prefetch"], HtmlSourceDependency["preload"], HtmlSourceDependency["fetchPriority"], HtmlSourceDependency["asAttribute"], HtmlSourceDependency["typeAttribute"], HtmlSourceDependency["mediaAttribute"]]>} ObjectSerializerContext */
3430
/** @import Hash from "../../util/Hash" */
3531
/** @import { RuntimeSpec } from "../../util/runtime" */
3632

@@ -104,13 +100,13 @@ class HtmlSourceDependency extends ModuleDependency {
104100
* @param {ObjectSerializerContext} context context
105101
*/
106102
serialize(context) {
107-
const { write } = context;
108-
write(this.prefetch);
109-
write(this.preload);
110-
write(this.fetchPriority);
111-
write(this.asAttribute);
112-
write(this.typeAttribute);
113-
write(this.mediaAttribute);
103+
context
104+
.write(this.prefetch)
105+
.write(this.preload)
106+
.write(this.fetchPriority)
107+
.write(this.asAttribute)
108+
.write(this.typeAttribute)
109+
.write(this.mediaAttribute);
114110
super.serialize(context);
115111
}
116112

@@ -119,14 +115,18 @@ class HtmlSourceDependency extends ModuleDependency {
119115
* @param {ObjectDeserializerContext} context context
120116
*/
121117
deserialize(context) {
122-
const { read } = context;
123-
this.prefetch = read();
124-
this.preload = read();
125-
this.fetchPriority = read();
126-
this.asAttribute = read();
127-
this.typeAttribute = read();
128-
this.mediaAttribute = read();
129-
super.deserialize(context);
118+
this.prefetch = context.read();
119+
const c1 = context.rest;
120+
this.preload = c1.read();
121+
const c2 = c1.rest;
122+
this.fetchPriority = c2.read();
123+
const c3 = c2.rest;
124+
this.asAttribute = c3.read();
125+
const c4 = c3.rest;
126+
this.typeAttribute = c4.read();
127+
const c5 = c4.rest;
128+
this.mediaAttribute = c5.read();
129+
super.deserialize(c5.rest);
130130
}
131131
}
132132

Collapse file

‎lib/dependencies/import/ImportContextDependency.js‎

Copy file name to clipboardExpand all lines: lib/dependencies/import/ImportContextDependency.js
+6-7Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const { ImportPhaseUtils } = require("../core/ImportPhase");
1212
const importOptionsCheck = require("./importOptionsCheck");
1313

1414
/** @import { Range } from "../../javascript/JavascriptParser" */
15-
/** @import { ObjectDeserializerContext, ObjectSerializerContext } from "../../serialization/ObjectMiddleware" */
15+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectDeserializerContext<[ImportContextDependency["valueRange"], ImportContextDependency["optionsRange"]]>} ObjectDeserializerContext */
16+
/** @typedef {import("../../serialization/ObjectMiddleware").ObjectSerializerContext<[ImportContextDependency["valueRange"], ImportContextDependency["optionsRange"]]>} ObjectSerializerContext */
1617
/** @import { ContextDependencyOptions } from "../context/ContextDependency" */
1718
/** @import { ReplaceSource } from "webpack-sources" */
1819
/** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */
@@ -63,9 +64,7 @@ class ImportContextDependency extends ContextDependency {
6364
* @param {ObjectSerializerContext} context context
6465
*/
6566
serialize(context) {
66-
context.write(this.valueRange);
67-
context.write(this.optionsRange);
68-
67+
context.write(this.valueRange).write(this.optionsRange);
6968
super.serialize(context);
7069
}
7170

@@ -75,9 +74,9 @@ class ImportContextDependency extends ContextDependency {
7574
*/
7675
deserialize(context) {
7776
this.valueRange = context.read();
78-
this.optionsRange = context.read();
79-
80-
super.deserialize(context.rest);
77+
const c1 = context.rest;
78+
this.optionsRange = c1.read();
79+
super.deserialize(c1.rest);
8180
}
8281
}
8382

Collapse file

‎lib/json/JsonData.js‎

Copy file name to clipboardExpand all lines: lib/json/JsonData.js
+7-11Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@
77

88
const { register } = require("../util/serialization");
99

10-
/**
11-
* @import {
12-
* ObjectDeserializerContext,
13-
* ObjectSerializerContext
14-
* } from "../serialization/ObjectMiddleware"
15-
*/
10+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[JsonData["_buffer"]]>} ObjectDeserializerContext */
11+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[JsonData["_buffer"]]>} ObjectSerializerContext */
1612
/** @import Hash from "../util/Hash" */
1713
/** @import { JsonValue } from "../fs/fs" */
1814

1915
class JsonData {
2016
/**
2117
* Creates an instance of JsonData.
22-
* @param {Buffer | JsonValue} data JSON data
18+
* @param {Buffer | JsonValue | undefined} data JSON data, or nothing when a custom `parse` returned nothing
2319
*/
2420
constructor(data) {
2521
/** @type {Buffer | undefined} */
@@ -64,19 +60,19 @@ register(JsonData, "webpack/lib/json/JsonData", null, {
6460
* @param {JsonData} obj JSONData object
6561
* @param {ObjectSerializerContext} context context
6662
*/
67-
serialize(obj, { write }) {
63+
serialize(obj, context) {
6864
if (obj._buffer === undefined && obj._data !== undefined) {
6965
obj._buffer = Buffer.from(JSON.stringify(obj._data));
7066
}
71-
write(obj._buffer);
67+
context.write(obj._buffer);
7268
},
7369
/**
7470
* Restores this instance from the provided deserializer context.
7571
* @param {ObjectDeserializerContext} context context
7672
* @returns {JsonData} deserialized JSON data
7773
*/
78-
deserialize({ read }) {
79-
return new JsonData(read());
74+
deserialize(context) {
75+
return new JsonData(context.read());
8076
}
8177
});
8278

Collapse file

‎lib/optimize/InlineExports.js‎

Copy file name to clipboardExpand all lines: lib/optimize/InlineExports.js
+7-12Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
const makeSerializable = require("../util/makeSerializable");
99
const { propertyAccess } = require("../util/property");
1010

11-
/**
12-
* @import {
13-
* ObjectDeserializerContext,
14-
* ObjectSerializerContext
15-
* } from "../serialization/ObjectMiddleware"
16-
*/
11+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[InlinedValue["kind"], InlinedValue["value"]]>} ObjectDeserializerContext */
12+
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[InlinedValue["kind"], InlinedValue["value"]]>} ObjectSerializerContext */
1713
/** @import ModuleGraph from "../graph/ModuleGraph" */
1814
/** @import Module from "../module/Module" */
1915
/** @import { JavascriptModuleBuildInfo } from "../javascript/JavascriptModule" */
@@ -73,17 +69,16 @@ class InlinedValue {
7369
/**
7470
* @param {ObjectSerializerContext} context context
7571
*/
76-
serialize({ write }) {
77-
write(this.kind);
78-
write(this.value);
72+
serialize(context) {
73+
context.write(this.kind).write(this.value);
7974
}
8075

8176
/**
8277
* @param {ObjectDeserializerContext} context context
8378
*/
84-
deserialize({ read }) {
85-
this.kind = read();
86-
this.value = read();
79+
deserialize(context) {
80+
this.kind = context.read();
81+
this.value = context.rest.read();
8782
}
8883
}
8984

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.