GeezerVison is showing github.com — refactor: type more cache serializers by position (#23072) · webpack/webpack@13b3a61
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +5Lines changed: 5 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +"webpack": patch | | 3 | +--- | | 4 | + | | 5 | +Type more cache serializers by position instead of with `any`. |
Collapse file Copy file name to clipboardExpand all lines: lib/cache/PackFileCacheStrategy.js +26-16Lines changed: 26 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,6 +54,11 @@ const UNREFERENCED_FILE = "unreferenced.json"; | 54 | 54 | // in which case the recorded time describes the previous file and must not be trusted. | 55 | 55 | const CLEANUP_RECENT_WRITE_PERIOD = 60 * 1000; | 56 | 56 |
| | 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 | + | 57 | 62 | class PackContainer { | 58 | 63 | /** | 59 | 64 | * Creates an instance of PackContainer. | @@ -88,29 +93,34 @@ class PackContainer { | 88 | 93 |
| 89 | 94 | /** | 90 | 95 | * Serializes this instance into the provided serializer context. | 91 | | - * @param {ObjectSerializerContext} context context | | 96 | + * @param {ObjectSerializerContext<PackContainerFields>} context context | 92 | 97 | */ | 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); | 99 | 105 | /** @type {NonNullable<ObjectSerializerContext["writeLazy"]>} */ | 100 | | - (writeLazy)(this.data); | | 106 | + (context.writeLazy)(this.data); | 101 | 107 | } | 102 | 108 |
| 103 | 109 | /** | 104 | 110 | * Restores this instance from the provided deserializer context. | 105 | | - * @param {ObjectDeserializerContext} context context | | 111 | + * @param {ObjectDeserializerContext<[...PackContainerFields, PackContainer["data"]]>} context context | 106 | 112 | */ | 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(); | 114 | 124 | } | 115 | 125 | } | 116 | 126 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/cache/ResolverCachePlugin.js +7-12Lines changed: 7 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,12 +25,8 @@ const makeSerializable = require("../util/makeSerializable"); | 25 | 25 | * SnapshotOptions | 26 | 26 | * } from "../fs/FileSystemInfo" | 27 | 27 | */ | 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 */ | 34 | 30 |
| 35 | 31 | /** | 36 | 32 | * Defines the sync hook type used by this module. | @@ -57,18 +53,17 @@ class CacheEntry { | 57 | 53 | * Serializes this instance into the provided serializer context. | 58 | 54 | * @param {ObjectSerializerContext} context context | 59 | 55 | */ | 60 | | - serialize({ write }) { | 61 | | - write(this.result); | 62 | | - write(this.snapshot); | | 56 | + serialize(context) { | | 57 | + context.write(this.result).write(this.snapshot); | 63 | 58 | } | 64 | 59 |
| 65 | 60 | /** | 66 | 61 | * Restores this instance from the provided deserializer context. | 67 | 62 | * @param {ObjectDeserializerContext} context context | 68 | 63 | */ | 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(); | 72 | 67 | } | 73 | 68 | } | 74 | 69 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/dependencies/esm/TopLevelAwaitDependency.js +7-13Lines changed: 7 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,12 +12,8 @@ const NullDependency = require("../core/NullDependency"); | 12 | 12 | /** @import Dependency from "../../graph/Dependency" */ | 13 | 13 | /** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */ | 14 | 14 | /** @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 */ | 21 | 17 |
| 22 | 18 | /** | 23 | 19 | * Marks a top-level `await` expression so it can be rewritten to `(yield …)` | @@ -41,9 +37,7 @@ class TopLevelAwaitDependency extends NullDependency { | 41 | 37 | * @param {ObjectSerializerContext} context context | 42 | 38 | */ | 43 | 39 | serialize(context) { | 44 | | - const { write } = context; | 45 | | - write(this.range); | 46 | | - write(this.argumentRange); | | 40 | + context.write(this.range).write(this.argumentRange); | 47 | 41 | super.serialize(context); | 48 | 42 | } | 49 | 43 |
| @@ -52,10 +46,10 @@ class TopLevelAwaitDependency extends NullDependency { | 52 | 46 | * @param {ObjectDeserializerContext} context context | 53 | 47 | */ | 54 | 48 | 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); | 59 | 53 | } | 60 | 54 | } | 61 | 55 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/dependencies/html/HtmlSourceDependency.js +21-21Lines changed: 21 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,12 +25,8 @@ const ModuleDependency = require("../core/ModuleDependency"); | 25 | 25 | /** @import { NormalModuleBuildInfo } from "../../module/NormalModule" */ | 26 | 26 | /** @import { HtmlModuleBuildInfo } from "../../html/HtmlModule" */ | 27 | 27 | /** @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 */ | 34 | 30 | /** @import Hash from "../../util/Hash" */ | 35 | 31 | /** @import { RuntimeSpec } from "../../util/runtime" */ | 36 | 32 |
| @@ -104,13 +100,13 @@ class HtmlSourceDependency extends ModuleDependency { | 104 | 100 | * @param {ObjectSerializerContext} context context | 105 | 101 | */ | 106 | 102 | 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); | 114 | 110 | super.serialize(context); | 115 | 111 | } | 116 | 112 |
| @@ -119,14 +115,18 @@ class HtmlSourceDependency extends ModuleDependency { | 119 | 115 | * @param {ObjectDeserializerContext} context context | 120 | 116 | */ | 121 | 117 | 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); | 130 | 130 | } | 131 | 131 | } | 132 | 132 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/dependencies/import/ImportContextDependency.js +6-7Lines changed: 6 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,7 +12,8 @@ const { ImportPhaseUtils } = require("../core/ImportPhase"); | 12 | 12 | const importOptionsCheck = require("./importOptionsCheck"); | 13 | 13 |
| 14 | 14 | /** @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 */ | 16 | 17 | /** @import { ContextDependencyOptions } from "../context/ContextDependency" */ | 17 | 18 | /** @import { ReplaceSource } from "webpack-sources" */ | 18 | 19 | /** @import { DependencyTemplateContext } from "../../template/DependencyTemplate" */ | @@ -63,9 +64,7 @@ class ImportContextDependency extends ContextDependency { | 63 | 64 | * @param {ObjectSerializerContext} context context | 64 | 65 | */ | 65 | 66 | serialize(context) { | 66 | | - context.write(this.valueRange); | 67 | | - context.write(this.optionsRange); | 68 | | - | | 67 | + context.write(this.valueRange).write(this.optionsRange); | 69 | 68 | super.serialize(context); | 70 | 69 | } | 71 | 70 |
| @@ -75,9 +74,9 @@ class ImportContextDependency extends ContextDependency { | 75 | 74 | */ | 76 | 75 | deserialize(context) { | 77 | 76 | 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); | 81 | 80 | } | 82 | 81 | } | 83 | 82 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/json/JsonData.js +7-11Lines changed: 7 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 | const { register } = require("../util/serialization"); | 9 | 9 |
| 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 */ | 16 | 12 | /** @import Hash from "../util/Hash" */ | 17 | 13 | /** @import { JsonValue } from "../fs/fs" */ | 18 | 14 |
| 19 | 15 | class JsonData { | 20 | 16 | /** | 21 | 17 | * 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 | 23 | 19 | */ | 24 | 20 | constructor(data) { | 25 | 21 | /** @type {Buffer | undefined} */ | @@ -64,19 +60,19 @@ register(JsonData, "webpack/lib/json/JsonData", null, { | 64 | 60 | * @param {JsonData} obj JSONData object | 65 | 61 | * @param {ObjectSerializerContext} context context | 66 | 62 | */ | 67 | | - serialize(obj, { write }) { | | 63 | + serialize(obj, context) { | 68 | 64 | if (obj._buffer === undefined && obj._data !== undefined) { | 69 | 65 | obj._buffer = Buffer.from(JSON.stringify(obj._data)); | 70 | 66 | } | 71 | | - write(obj._buffer); | | 67 | + context.write(obj._buffer); | 72 | 68 | }, | 73 | 69 | /** | 74 | 70 | * Restores this instance from the provided deserializer context. | 75 | 71 | * @param {ObjectDeserializerContext} context context | 76 | 72 | * @returns {JsonData} deserialized JSON data | 77 | 73 | */ | 78 | | - deserialize({ read }) { | 79 | | - return new JsonData(read()); | | 74 | + deserialize(context) { | | 75 | + return new JsonData(context.read()); | 80 | 76 | } | 81 | 77 | }); | 82 | 78 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/optimize/InlineExports.js +7-12Lines changed: 7 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | const makeSerializable = require("../util/makeSerializable"); | 9 | 9 | const { propertyAccess } = require("../util/property"); | 10 | 10 |
| 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 */ | 17 | 13 | /** @import ModuleGraph from "../graph/ModuleGraph" */ | 18 | 14 | /** @import Module from "../module/Module" */ | 19 | 15 | /** @import { JavascriptModuleBuildInfo } from "../javascript/JavascriptModule" */ | @@ -73,17 +69,16 @@ class InlinedValue { | 73 | 69 | /** | 74 | 70 | * @param {ObjectSerializerContext} context context | 75 | 71 | */ | 76 | | - serialize({ write }) { | 77 | | - write(this.kind); | 78 | | - write(this.value); | | 72 | + serialize(context) { | | 73 | + context.write(this.kind).write(this.value); | 79 | 74 | } | 80 | 75 |
| 81 | 76 | /** | 82 | 77 | * @param {ObjectDeserializerContext} context context | 83 | 78 | */ | 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(); | 87 | 82 | } | 88 | 83 | } | 89 | 84 |
| |
|