webpack 5.99.5 → 5.99.7
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/README.md +1 -1
- package/lib/APIPlugin.js +2 -2
- package/lib/AutomaticPrefetchPlugin.js +21 -22
- package/lib/BannerPlugin.js +31 -35
- package/lib/Cache.js +1 -1
- package/lib/Chunk.js +12 -9
- package/lib/CleanPlugin.js +5 -3
- package/lib/Compilation.js +45 -35
- package/lib/Compiler.js +6 -4
- package/lib/ContextExclusionPlugin.js +4 -2
- package/lib/ContextModule.js +2 -1
- package/lib/ContextReplacementPlugin.js +5 -3
- package/lib/DelegatedPlugin.js +4 -2
- package/lib/DllEntryPlugin.js +4 -2
- package/lib/DllPlugin.js +5 -3
- package/lib/DllReferencePlugin.js +56 -60
- package/lib/DynamicEntryPlugin.js +4 -2
- package/lib/EntryOptionPlugin.js +3 -1
- package/lib/EntryPlugin.js +4 -2
- package/lib/EnvironmentPlugin.js +4 -2
- package/lib/EvalDevToolModulePlugin.js +9 -7
- package/lib/EvalSourceMapDevToolPlugin.js +137 -138
- package/lib/ExternalsPlugin.js +3 -1
- package/lib/FlagDependencyExportsPlugin.js +2 -1
- package/lib/HotModuleReplacementPlugin.js +3 -3
- package/lib/IgnorePlugin.js +6 -4
- package/lib/IgnoreWarningsPlugin.js +4 -2
- package/lib/LibManifestPlugin.js +3 -4
- package/lib/LoaderOptionsPlugin.js +4 -2
- package/lib/LoaderTargetPlugin.js +4 -2
- package/lib/Module.js +29 -14
- package/lib/ModuleFilenameHelpers.js +1 -1
- package/lib/ModuleGraph.js +15 -10
- package/lib/ModuleInfoHeaderPlugin.js +11 -12
- package/lib/MultiCompiler.js +5 -3
- package/lib/NoEmitOnErrorsPlugin.js +5 -3
- package/lib/NormalModule.js +6 -2
- package/lib/NormalModuleReplacementPlugin.js +33 -36
- package/lib/PlatformPlugin.js +3 -1
- package/lib/PrefetchPlugin.js +5 -3
- package/lib/ProgressPlugin.js +23 -26
- package/lib/RecordIdsPlugin.js +73 -103
- package/lib/RuntimePlugin.js +34 -32
- package/lib/SourceMapDevToolPlugin.js +8 -6
- package/lib/Template.js +1 -1
- package/lib/WarnCaseSensitiveModulesPlugin.js +36 -37
- package/lib/WarnNoModeSetPlugin.js +3 -1
- package/lib/WatchIgnorePlugin.js +3 -1
- package/lib/WebpackError.js +11 -3
- package/lib/WebpackOptionsApply.js +22 -5
- package/lib/async-modules/InferAsyncModulesPlugin.js +25 -26
- package/lib/buildChunkGraph.js +7 -2
- package/lib/cache/IdleFileCachePlugin.js +12 -13
- package/lib/cache/MemoryCachePlugin.js +2 -1
- package/lib/cache/MemoryWithGcCachePlugin.js +10 -7
- package/lib/cache/PackFileCacheStrategy.js +13 -21
- package/lib/cache/ResolverCachePlugin.js +22 -22
- package/lib/cli.js +8 -4
- package/lib/config/defaults.js +2 -2
- package/lib/config/normalization.js +9 -3
- package/lib/config/target.js +6 -6
- package/lib/container/ContainerReferencePlugin.js +24 -26
- package/lib/container/ModuleFederationPlugin.js +2 -1
- package/lib/css/CssGenerator.js +1 -1
- package/lib/css/CssModulesPlugin.js +6 -4
- package/lib/css/CssParser.js +1 -1
- package/lib/debug/ProfilingPlugin.js +1 -1
- package/lib/dependencies/ContextDependencyHelpers.js +1 -1
- package/lib/dependencies/CssIcssImportDependency.js +3 -2
- package/lib/dependencies/HarmonyDetectionParserPlugin.js +9 -15
- package/lib/dependencies/HarmonyExportExpressionDependency.js +1 -1
- package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +2 -1
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +1 -1
- package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +15 -15
- package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +4 -2
- package/lib/dependencies/JsonExportsDependency.js +1 -1
- package/lib/dependencies/LoaderPlugin.js +5 -3
- package/lib/dependencies/LocalModulesHelpers.js +1 -1
- package/lib/dependencies/RequireContextDependencyParserPlugin.js +46 -46
- package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +94 -100
- package/lib/dependencies/RequireResolveDependency.js +1 -1
- package/lib/dependencies/WorkerPlugin.js +2 -2
- package/lib/esm/ModuleChunkLoadingPlugin.js +76 -57
- package/lib/esm/ModuleChunkLoadingRuntimeModule.js +11 -9
- package/lib/hmr/HotModuleReplacement.runtime.js +0 -1
- package/lib/hmr/JavascriptHotModuleReplacement.runtime.js +0 -1
- package/lib/hmr/LazyCompilationPlugin.js +17 -18
- package/lib/ids/ChunkModuleIdRangePlugin.js +6 -6
- package/lib/ids/DeterministicChunkIdsPlugin.js +1 -1
- package/lib/ids/DeterministicModuleIdsPlugin.js +48 -49
- package/lib/ids/HashedModuleIdsPlugin.js +4 -2
- package/lib/ids/NamedChunkIdsPlugin.js +6 -4
- package/lib/ids/NamedModuleIdsPlugin.js +6 -4
- package/lib/ids/NaturalChunkIdsPlugin.js +4 -2
- package/lib/ids/NaturalModuleIdsPlugin.js +4 -2
- package/lib/ids/OccurrenceChunkIdsPlugin.js +4 -2
- package/lib/ids/OccurrenceModuleIdsPlugin.js +4 -2
- package/lib/index.js +1 -1
- package/lib/javascript/BasicEvaluatedExpression.js +2 -2
- package/lib/javascript/JavascriptParser.js +670 -688
- package/lib/library/EnableLibraryPlugin.js +15 -2
- package/lib/library/ModuleLibraryPlugin.js +66 -43
- package/lib/logging/createConsoleLogger.js +0 -1
- package/lib/node/CommonJsChunkLoadingPlugin.js +71 -75
- package/lib/node/NodeEnvironmentPlugin.js +3 -1
- package/lib/node/NodeTemplatePlugin.js +2 -2
- package/lib/node/ReadFileCompileAsyncWasmPlugin.js +2 -2
- package/lib/node/ReadFileCompileWasmPlugin.js +3 -3
- package/lib/optimize/AggressiveMergingPlugin.js +1 -1
- package/lib/optimize/AggressiveSplittingPlugin.js +224 -232
- package/lib/optimize/ConcatenatedModule.js +12 -15
- package/lib/optimize/FlagIncludedChunksPlugin.js +92 -97
- package/lib/optimize/LimitChunkCountPlugin.js +4 -2
- package/lib/optimize/MangleExportsPlugin.js +15 -16
- package/lib/optimize/MinChunkSizePlugin.js +4 -2
- package/lib/optimize/ModuleConcatenationPlugin.js +4 -2
- package/lib/optimize/RealContentHashPlugin.js +4 -2
- package/lib/optimize/RemoveEmptyChunksPlugin.js +5 -3
- package/lib/optimize/RemoveParentModulesPlugin.js +4 -2
- package/lib/optimize/RuntimeChunkPlugin.js +17 -18
- package/lib/optimize/SplitChunksPlugin.js +9 -6
- package/lib/performance/SizeLimitsPlugin.js +3 -1
- package/lib/prefetch/ChunkPrefetchPreloadPlugin.js +61 -62
- package/lib/runtime/GetChunkFilenameRuntimeModule.js +3 -4
- package/lib/runtime/StartupChunkDependenciesPlugin.js +39 -42
- package/lib/schemes/DataUriPlugin.js +5 -3
- package/lib/schemes/FileUriPlugin.js +5 -3
- package/lib/schemes/HttpUriPlugin.js +32 -39
- package/lib/serialization/AggregateErrorSerializer.js +42 -0
- package/lib/serialization/BinaryMiddleware.js +22 -38
- package/lib/serialization/ErrorObjectSerializer.js +7 -2
- package/lib/serialization/FileMiddleware.js +29 -33
- package/lib/serialization/ObjectMiddleware.js +42 -30
- package/lib/serialization/Serializer.js +29 -18
- package/lib/serialization/SerializerMiddleware.js +105 -72
- package/lib/serialization/SingleItemMiddleware.js +4 -5
- package/lib/sharing/ProvideSharedPlugin.js +6 -4
- package/lib/stats/DefaultStatsFactoryPlugin.js +128 -57
- package/lib/stats/DefaultStatsPresetPlugin.js +25 -20
- package/lib/stats/DefaultStatsPrinterPlugin.js +486 -334
- package/lib/stats/StatsFactory.js +47 -10
- package/lib/stats/StatsPrinter.js +52 -31
- package/lib/util/ArrayQueue.js +1 -1
- package/lib/util/AsyncQueue.js +1 -1
- package/lib/util/TupleQueue.js +9 -7
- package/lib/util/TupleSet.js +37 -18
- package/lib/util/WeakTupleMap.js +50 -37
- package/lib/util/cleverMerge.js +2 -2
- package/lib/util/comparators.js +1 -1
- package/lib/util/concatenate.js +4 -2
- package/lib/util/createHash.js +1 -1
- package/lib/util/fs.js +1 -1
- package/lib/util/makeSerializable.js +1 -1
- package/lib/util/runtime.js +1 -0
- package/lib/util/serialization.js +50 -42
- package/lib/wasm-async/AsyncWebAssemblyGenerator.js +1 -1
- package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +1 -1
- package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +2 -2
- package/lib/wasm-sync/WasmFinalizeExportsPlugin.js +55 -57
- package/lib/wasm-sync/WebAssemblyGenerator.js +1 -1
- package/lib/wasm-sync/WebAssemblyModulesPlugin.js +1 -1
- package/lib/web/FetchCompileWasmPlugin.js +2 -2
- package/lib/web/JsonpChunkLoadingPlugin.js +73 -74
- package/lib/webpack.js +1 -1
- package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +77 -78
- package/package.json +4 -3
- package/schemas/WebpackOptions.check.js +1 -1
- package/schemas/WebpackOptions.json +24 -2
- package/types.d.ts +273 -128
@@ -22,14 +22,8 @@ const SerializerMiddleware = require("./SerializerMiddleware");
|
|
22
22
|
/** @typedef {typeof import("../util/Hash")} Hash */
|
23
23
|
/** @typedef {import("../util/fs").IStats} IStats */
|
24
24
|
/** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */
|
25
|
-
/** @typedef {import("./SerializerMiddleware").Context} Context */
|
26
25
|
/** @typedef {import("./types").BufferSerializableType} BufferSerializableType */
|
27
26
|
|
28
|
-
/**
|
29
|
-
* @template LAZY_RESULT
|
30
|
-
* @typedef {import("./SerializerMiddleware").LazyFunction<LAZY_RESULT>} LazyFunction
|
31
|
-
*/
|
32
|
-
|
33
27
|
/*
|
34
28
|
Format:
|
35
29
|
|
@@ -95,9 +89,14 @@ const readUInt64LE = Buffer.prototype.readBigUInt64LE
|
|
95
89
|
* @property {BackgroundJob=} backgroundJob
|
96
90
|
*/
|
97
91
|
|
92
|
+
/** @typedef {{ name: string, size: number }} LazyOptions */
|
93
|
+
/**
|
94
|
+
* @typedef {import("./SerializerMiddleware").LazyFunction<BufferSerializableType[], Buffer, FileMiddleware, LazyOptions>} LazyFunction
|
95
|
+
*/
|
96
|
+
|
98
97
|
/**
|
99
98
|
* @param {FileMiddleware} middleware this
|
100
|
-
* @param {BufferSerializableType
|
99
|
+
* @param {(BufferSerializableType | LazyFunction)[]} data data to be serialized
|
101
100
|
* @param {string | boolean} name file base name
|
102
101
|
* @param {(name: string | false, buffers: Buffer[], size: number) => Promise<void>} writeFile writes a file
|
103
102
|
* @param {string | Hash} hashFunction hash function to use
|
@@ -112,7 +111,7 @@ const serialize = async (
|
|
112
111
|
) => {
|
113
112
|
/** @type {(Buffer[] | Buffer | Promise<SerializeResult>)[]} */
|
114
113
|
const processedData = [];
|
115
|
-
/** @type {WeakMap<SerializeResult, LazyFunction
|
114
|
+
/** @type {WeakMap<SerializeResult, LazyFunction>} */
|
116
115
|
const resultToLazy = new WeakMap();
|
117
116
|
/** @type {Buffer[] | undefined} */
|
118
117
|
let lastBuffers;
|
@@ -138,25 +137,19 @@ const serialize = async (
|
|
138
137
|
} else {
|
139
138
|
const content = item();
|
140
139
|
if (content) {
|
141
|
-
const options = SerializerMiddleware.getLazyOptions(
|
142
|
-
/** @type {LazyFunction<Buffer[]>} */
|
143
|
-
(item)
|
144
|
-
);
|
140
|
+
const options = SerializerMiddleware.getLazyOptions(item);
|
145
141
|
processedData.push(
|
146
142
|
serialize(
|
147
143
|
middleware,
|
148
|
-
|
144
|
+
/** @type {BufferSerializableType[]} */
|
145
|
+
(content),
|
149
146
|
(options && options.name) || true,
|
150
147
|
writeFile,
|
151
148
|
hashFunction
|
152
149
|
).then(result => {
|
153
|
-
/** @type {
|
154
|
-
(item
|
155
|
-
resultToLazy.set(
|
156
|
-
result,
|
157
|
-
/** @type {LazyFunction<Buffer[]>} */
|
158
|
-
(item)
|
159
|
-
);
|
150
|
+
/** @type {LazyOptions} */
|
151
|
+
(item.options).size = result.size;
|
152
|
+
resultToLazy.set(result, item);
|
160
153
|
return result;
|
161
154
|
})
|
162
155
|
);
|
@@ -193,7 +186,7 @@ const serialize = async (
|
|
193
186
|
writeUInt64LE(buf, item.size, 0);
|
194
187
|
nameBuffer.copy(buf, 8, 0);
|
195
188
|
const lazy =
|
196
|
-
/** @type {LazyFunction
|
189
|
+
/** @type {LazyFunction} */
|
197
190
|
(resultToLazy.get(item));
|
198
191
|
SerializerMiddleware.setLazySerializedValue(lazy, buf);
|
199
192
|
return buf;
|
@@ -351,7 +344,7 @@ const deserialize = async (middleware, name, readFile) => {
|
|
351
344
|
lastLengthPositive = valuePositive;
|
352
345
|
}
|
353
346
|
}
|
354
|
-
/** @type {
|
347
|
+
/** @type {BufferSerializableType[]} */
|
355
348
|
const result = [];
|
356
349
|
for (let length of lengths) {
|
357
350
|
if (length < 0) {
|
@@ -359,13 +352,16 @@ const deserialize = async (middleware, name, readFile) => {
|
|
359
352
|
const size = Number(readUInt64LE(slice, 0));
|
360
353
|
const nameBuffer = slice.slice(8);
|
361
354
|
const name = nameBuffer.toString();
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
355
|
+
const lazy =
|
356
|
+
/** @type {LazyFunction} */
|
357
|
+
(
|
358
|
+
SerializerMiddleware.createLazy(
|
359
|
+
memoize(() => deserialize(middleware, name, readFile)),
|
360
|
+
middleware,
|
361
|
+
{ name, size },
|
362
|
+
slice
|
363
|
+
)
|
364
|
+
);
|
369
365
|
result.push(lazy);
|
370
366
|
} else {
|
371
367
|
if (contentPosition === contentItemLength) {
|
@@ -423,12 +419,12 @@ const deserialize = async (middleware, name, readFile) => {
|
|
423
419
|
return result;
|
424
420
|
};
|
425
421
|
|
426
|
-
/** @typedef {
|
422
|
+
/** @typedef {BufferSerializableType[]} DeserializedType */
|
423
|
+
/** @typedef {true} SerializedType */
|
424
|
+
/** @typedef {{ filename: string, extension?: string }} Context */
|
427
425
|
|
428
426
|
/**
|
429
|
-
* @
|
430
|
-
* @typedef {true} SerializedType
|
431
|
-
* @extends {SerializerMiddleware<DeserializedType, SerializedType>}
|
427
|
+
* @extends {SerializerMiddleware<DeserializedType, SerializedType, Context>}
|
432
428
|
*/
|
433
429
|
class FileMiddleware extends SerializerMiddleware {
|
434
430
|
/**
|
@@ -6,6 +6,7 @@
|
|
6
6
|
|
7
7
|
const { DEFAULTS } = require("../config/defaults");
|
8
8
|
const createHash = require("../util/createHash");
|
9
|
+
const AggregateErrorSerializer = require("./AggregateErrorSerializer");
|
9
10
|
const ArraySerializer = require("./ArraySerializer");
|
10
11
|
const DateObjectSerializer = require("./DateObjectSerializer");
|
11
12
|
const ErrorObjectSerializer = require("./ErrorObjectSerializer");
|
@@ -16,18 +17,14 @@ const RegExpObjectSerializer = require("./RegExpObjectSerializer");
|
|
16
17
|
const SerializerMiddleware = require("./SerializerMiddleware");
|
17
18
|
const SetObjectSerializer = require("./SetObjectSerializer");
|
18
19
|
|
20
|
+
/** @typedef {import("../logging/Logger").Logger} Logger */
|
19
21
|
/** @typedef {typeof import("../util/Hash")} Hash */
|
20
|
-
/** @typedef {import("./SerializerMiddleware").
|
22
|
+
/** @typedef {import("./SerializerMiddleware").LazyOptions} LazyOptions */
|
21
23
|
/** @typedef {import("./types").ComplexSerializableType} ComplexSerializableType */
|
22
24
|
/** @typedef {import("./types").PrimitiveSerializableType} PrimitiveSerializableType */
|
23
25
|
|
24
26
|
/** @typedef {new (...params: EXPECTED_ANY[]) => EXPECTED_ANY} Constructor */
|
25
27
|
|
26
|
-
/**
|
27
|
-
* @template LAZY_RESULT
|
28
|
-
* @typedef {import("./SerializerMiddleware").LazyFunction<LAZY_RESULT>} LazyFunction
|
29
|
-
*/
|
30
|
-
|
31
28
|
/*
|
32
29
|
|
33
30
|
Format:
|
@@ -59,27 +56,29 @@ Technically any value can be used.
|
|
59
56
|
* @property {number} objectTypeLookupSize
|
60
57
|
* @property {number} currentPosTypeLookup
|
61
58
|
*/
|
59
|
+
/** @typedef {TODO} Value */
|
60
|
+
/** @typedef {EXPECTED_OBJECT | string} ReferenceableItem */
|
62
61
|
|
63
62
|
/**
|
64
63
|
* @typedef {object} ObjectSerializerContext
|
65
|
-
* @property {(value:
|
66
|
-
* @property {(value:
|
64
|
+
* @property {(value: Value) => void} write
|
65
|
+
* @property {(value: ReferenceableItem) => void} setCircularReference
|
67
66
|
* @property {() => ObjectSerializerSnapshot} snapshot
|
68
67
|
* @property {(snapshot: ObjectSerializerSnapshot) => void} rollback
|
69
|
-
* @property {((item:
|
70
|
-
* @property {((item:
|
68
|
+
* @property {((item: Value | (() => Value)) => void)=} writeLazy
|
69
|
+
* @property {((item: (Value | (() => Value)), obj: LazyOptions | undefined) => import("./SerializerMiddleware").LazyFunction<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY, LazyOptions>)=} writeSeparate
|
71
70
|
*/
|
72
71
|
|
73
72
|
/**
|
74
73
|
* @typedef {object} ObjectDeserializerContext
|
75
|
-
* @property {() =>
|
76
|
-
* @property {(value:
|
74
|
+
* @property {() => Value} read
|
75
|
+
* @property {(value: ReferenceableItem) => void} setCircularReference
|
77
76
|
*/
|
78
77
|
|
79
78
|
/**
|
80
79
|
* @typedef {object} ObjectSerializer
|
81
|
-
* @property {(value:
|
82
|
-
* @property {(context: ObjectDeserializerContext) =>
|
80
|
+
* @property {(value: Value, context: ObjectSerializerContext) => void} serialize
|
81
|
+
* @property {(context: ObjectDeserializerContext) => Value} deserialize
|
83
82
|
*/
|
84
83
|
|
85
84
|
/**
|
@@ -141,9 +140,8 @@ const loadedRequests = new Set();
|
|
141
140
|
|
142
141
|
const NOT_SERIALIZABLE = {};
|
143
142
|
|
144
|
-
/** @typedef {TODO} Item */
|
145
|
-
|
146
143
|
const jsTypes = new Map();
|
144
|
+
|
147
145
|
jsTypes.set(Object, new PlainObjectSerializer());
|
148
146
|
jsTypes.set(Array, new ArraySerializer());
|
149
147
|
jsTypes.set(null, new NullPrototypeObjectSerializer());
|
@@ -158,6 +156,17 @@ jsTypes.set(ReferenceError, new ErrorObjectSerializer(ReferenceError));
|
|
158
156
|
jsTypes.set(SyntaxError, new ErrorObjectSerializer(SyntaxError));
|
159
157
|
jsTypes.set(TypeError, new ErrorObjectSerializer(TypeError));
|
160
158
|
|
159
|
+
// @ts-expect-error ES2018 doesn't `AggregateError`, but it can be used by developers
|
160
|
+
// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax
|
161
|
+
if (typeof AggregateError !== "undefined") {
|
162
|
+
jsTypes.set(
|
163
|
+
// @ts-expect-error ES2018 doesn't `AggregateError`, but it can be used by developers
|
164
|
+
// eslint-disable-next-line n/no-unsupported-features/es-builtins, n/no-unsupported-features/es-syntax
|
165
|
+
AggregateError,
|
166
|
+
new AggregateErrorSerializer()
|
167
|
+
);
|
168
|
+
}
|
169
|
+
|
161
170
|
// If in a sandboxed environment (e.g. jest), this escapes the sandbox and registers
|
162
171
|
// real Object and Array types to. These types may occur in the wild too, e.g. when
|
163
172
|
// using Structured Clone in postMessage.
|
@@ -195,10 +204,12 @@ for (const { request, name, serializer } of serializers.values()) {
|
|
195
204
|
/** @type {Map<RegExp, (request: string) => boolean>} */
|
196
205
|
const loaders = new Map();
|
197
206
|
|
207
|
+
/** @typedef {ComplexSerializableType[]} DeserializedType */
|
208
|
+
/** @typedef {PrimitiveSerializableType[]} SerializedType */
|
209
|
+
/** @typedef {{ logger: Logger }} Context */
|
210
|
+
|
198
211
|
/**
|
199
|
-
* @
|
200
|
-
* @typedef {PrimitiveSerializableType[]} SerializedType
|
201
|
-
* @extends {SerializerMiddleware<DeserializedType, SerializedType>}
|
212
|
+
* @extends {SerializerMiddleware<DeserializedType, SerializedType, Context>}
|
202
213
|
*/
|
203
214
|
class ObjectMiddleware extends SerializerMiddleware {
|
204
215
|
/**
|
@@ -266,7 +277,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
266
277
|
}
|
267
278
|
|
268
279
|
/**
|
269
|
-
* @param {
|
280
|
+
* @param {Constructor} object for serialization
|
270
281
|
* @returns {SerializerConfigWithSerializer} Serializer config
|
271
282
|
*/
|
272
283
|
static getSerializerFor(object) {
|
@@ -324,13 +335,13 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
324
335
|
* @returns {SerializedType | Promise<SerializedType> | null} serialized data
|
325
336
|
*/
|
326
337
|
serialize(data, context) {
|
327
|
-
/** @type {
|
338
|
+
/** @type {Value[]} */
|
328
339
|
let result = [CURRENT_VERSION];
|
329
340
|
let currentPos = 0;
|
330
|
-
/** @type {Map<
|
341
|
+
/** @type {Map<ReferenceableItem, number>} */
|
331
342
|
let referenceable = new Map();
|
332
343
|
/**
|
333
|
-
* @param {
|
344
|
+
* @param {ReferenceableItem} item referenceable item
|
334
345
|
*/
|
335
346
|
const addReferenceable = item => {
|
336
347
|
referenceable.set(item, currentPos++);
|
@@ -401,7 +412,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
401
412
|
let objectTypeLookup = new Map();
|
402
413
|
const cycleStack = new Set();
|
403
414
|
/**
|
404
|
-
* @param {
|
415
|
+
* @param {Value} item item to stack
|
405
416
|
* @returns {string} stack
|
406
417
|
*/
|
407
418
|
const stackToString = item => {
|
@@ -497,7 +508,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
497
508
|
};
|
498
509
|
this.extendContext(ctx);
|
499
510
|
/**
|
500
|
-
* @param {
|
511
|
+
* @param {Value} item item to serialize
|
501
512
|
*/
|
502
513
|
const process = item => {
|
503
514
|
if (Buffer.isBuffer(item)) {
|
@@ -587,9 +598,11 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
587
598
|
} else if (typeof item === "function") {
|
588
599
|
if (!SerializerMiddleware.isLazy(item))
|
589
600
|
throw new Error(`Unexpected function ${item}`);
|
590
|
-
|
601
|
+
|
602
|
+
/** @type {SerializedType | undefined} */
|
591
603
|
const serializedData =
|
592
604
|
SerializerMiddleware.getLazySerializedValue(item);
|
605
|
+
|
593
606
|
if (serializedData !== undefined) {
|
594
607
|
if (typeof serializedData === "function") {
|
595
608
|
result.push(serializedData);
|
@@ -655,10 +668,10 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
655
668
|
throw new Error("Version mismatch, serializer changed");
|
656
669
|
|
657
670
|
let currentPos = 0;
|
658
|
-
/** @type {
|
671
|
+
/** @type {ReferenceableItem[]} */
|
659
672
|
let referenceable = [];
|
660
673
|
/**
|
661
|
-
* @param {
|
674
|
+
* @param {Value} item referenceable item
|
662
675
|
*/
|
663
676
|
const addReferenceable = item => {
|
664
677
|
referenceable.push(item);
|
@@ -791,8 +804,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
791
804
|
return item;
|
792
805
|
} else if (typeof item === "function") {
|
793
806
|
return SerializerMiddleware.deserializeLazy(
|
794
|
-
|
795
|
-
(item),
|
807
|
+
item,
|
796
808
|
data =>
|
797
809
|
/** @type {[DeserializedType]} */
|
798
810
|
(this.deserialize(data, context))[0]
|
@@ -4,17 +4,20 @@
|
|
4
4
|
|
5
5
|
"use strict";
|
6
6
|
|
7
|
-
/** @typedef {import("./SerializerMiddleware").Context} Context */
|
8
|
-
|
9
7
|
/**
|
10
|
-
* @template T, K
|
11
|
-
* @typedef {import("./SerializerMiddleware")<T, K>} SerializerMiddleware
|
8
|
+
* @template T, K, C
|
9
|
+
* @typedef {import("./SerializerMiddleware")<T, K, C>} SerializerMiddleware
|
12
10
|
*/
|
13
11
|
|
12
|
+
/**
|
13
|
+
* @template DeserializedValue
|
14
|
+
* @template SerializedValue
|
15
|
+
* @template Context
|
16
|
+
*/
|
14
17
|
class Serializer {
|
15
18
|
/**
|
16
|
-
* @param {SerializerMiddleware<
|
17
|
-
* @param {Context}
|
19
|
+
* @param {SerializerMiddleware<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>[]} middlewares serializer middlewares
|
20
|
+
* @param {Context=} context context
|
18
21
|
*/
|
19
22
|
constructor(middlewares, context) {
|
20
23
|
this.serializeMiddlewares = middlewares.slice();
|
@@ -23,17 +26,22 @@ class Serializer {
|
|
23
26
|
}
|
24
27
|
|
25
28
|
/**
|
26
|
-
* @
|
27
|
-
* @param {
|
28
|
-
* @
|
29
|
+
* @template ExtendedContext
|
30
|
+
* @param {DeserializedValue | Promise<DeserializedValue>} obj object
|
31
|
+
* @param {Context & ExtendedContext} context context object
|
32
|
+
* @returns {Promise<SerializedValue>} result
|
29
33
|
*/
|
30
34
|
serialize(obj, context) {
|
31
35
|
const ctx = { ...context, ...this.context };
|
32
36
|
let current = obj;
|
33
37
|
for (const middleware of this.serializeMiddlewares) {
|
34
|
-
if (
|
38
|
+
if (
|
39
|
+
current &&
|
40
|
+
typeof (/** @type {Promise<DeserializedValue>} */ (current).then) ===
|
41
|
+
"function"
|
42
|
+
) {
|
35
43
|
current =
|
36
|
-
/** @type {Promise<
|
44
|
+
/** @type {Promise<DeserializedValue>} */
|
37
45
|
(current).then(data => data && middleware.serialize(data, ctx));
|
38
46
|
} else if (current) {
|
39
47
|
try {
|
@@ -43,26 +51,29 @@ class Serializer {
|
|
43
51
|
}
|
44
52
|
} else break;
|
45
53
|
}
|
46
|
-
return current;
|
54
|
+
return /** @type {Promise<SerializedValue>} */ (current);
|
47
55
|
}
|
48
56
|
|
49
57
|
/**
|
50
|
-
* @
|
51
|
-
* @param {
|
52
|
-
* @
|
58
|
+
* @template ExtendedContext
|
59
|
+
* @param {SerializedValue | Promise<SerializedValue>} value value
|
60
|
+
* @param {Context & ExtendedContext} context object
|
61
|
+
* @returns {Promise<DeserializedValue>} result
|
53
62
|
*/
|
54
63
|
deserialize(value, context) {
|
55
64
|
const ctx = { ...context, ...this.context };
|
56
65
|
let current = value;
|
57
66
|
for (const middleware of this.deserializeMiddlewares) {
|
58
67
|
current =
|
59
|
-
current &&
|
60
|
-
|
68
|
+
current &&
|
69
|
+
typeof (/** @type {Promise<SerializedValue>} */ (current).then) ===
|
70
|
+
"function"
|
71
|
+
? /** @type {Promise<SerializedValue>} */ (current).then(data =>
|
61
72
|
middleware.deserialize(data, ctx)
|
62
73
|
)
|
63
74
|
: middleware.deserialize(current, ctx);
|
64
75
|
}
|
65
|
-
return current;
|
76
|
+
return /** @type {Promise<DeserializedValue>} */ (current);
|
66
77
|
}
|
67
78
|
}
|
68
79
|
|
@@ -9,23 +9,21 @@ const memoize = require("../util/memoize");
|
|
9
9
|
const LAZY_TARGET = Symbol("lazy serialization target");
|
10
10
|
const LAZY_SERIALIZED_VALUE = Symbol("lazy serialization data");
|
11
11
|
|
12
|
-
/** @typedef {
|
12
|
+
/** @typedef {SerializerMiddleware<EXPECTED_ANY, EXPECTED_ANY, Record<string, EXPECTED_ANY>>} LazyTarget */
|
13
|
+
/** @typedef {Record<string, EXPECTED_ANY>} LazyOptions */
|
13
14
|
|
14
15
|
/**
|
15
|
-
* @template
|
16
|
-
* @
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
/**
|
22
|
-
* @template LazyResult
|
23
|
-
* @typedef {InternalLazyFunction<LazyResult> & { [LAZY_TARGET]: TODO, [LAZY_SERIALIZED_VALUE]?: TODO, options: LazyOptions }} LazyFunction
|
16
|
+
* @template InputValue
|
17
|
+
* @template OutputValue
|
18
|
+
* @template {LazyTarget} InternalLazyTarget
|
19
|
+
* @template {LazyOptions | undefined} InternalLazyOptions
|
20
|
+
* @typedef {(() => InputValue | Promise<InputValue>) & Partial<{ [LAZY_TARGET]: InternalLazyTarget, options: InternalLazyOptions, [LAZY_SERIALIZED_VALUE]?: OutputValue | LazyFunction<OutputValue, InputValue, InternalLazyTarget, InternalLazyOptions> | undefined }>} LazyFunction
|
24
21
|
*/
|
25
22
|
|
26
23
|
/**
|
27
24
|
* @template DeserializedType
|
28
25
|
* @template SerializedType
|
26
|
+
* @template Context
|
29
27
|
*/
|
30
28
|
class SerializerMiddleware {
|
31
29
|
/* istanbul ignore next */
|
@@ -53,17 +51,25 @@ class SerializerMiddleware {
|
|
53
51
|
}
|
54
52
|
|
55
53
|
/**
|
56
|
-
* @template
|
57
|
-
* @
|
58
|
-
* @
|
59
|
-
* @
|
60
|
-
* @param {
|
61
|
-
* @
|
54
|
+
* @template TLazyInputValue
|
55
|
+
* @template TLazyOutputValue
|
56
|
+
* @template {LazyTarget} TLazyTarget
|
57
|
+
* @template {LazyOptions | undefined} TLazyOptions
|
58
|
+
* @param {TLazyInputValue | (() => TLazyInputValue)} value contained value or function to value
|
59
|
+
* @param {TLazyTarget} target target middleware
|
60
|
+
* @param {TLazyOptions=} options lazy options
|
61
|
+
* @param {TLazyOutputValue=} serializedValue serialized value
|
62
|
+
* @returns {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} lazy function
|
62
63
|
*/
|
63
|
-
static createLazy(
|
64
|
+
static createLazy(
|
65
|
+
value,
|
66
|
+
target,
|
67
|
+
options = /** @type {TLazyOptions} */ ({}),
|
68
|
+
serializedValue = undefined
|
69
|
+
) {
|
64
70
|
if (SerializerMiddleware.isLazy(value, target)) return value;
|
65
71
|
const fn =
|
66
|
-
/** @type {LazyFunction<
|
72
|
+
/** @type {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} */
|
67
73
|
(typeof value === "function" ? value : () => value);
|
68
74
|
fn[LAZY_TARGET] = target;
|
69
75
|
fn.options = options;
|
@@ -72,9 +78,10 @@ class SerializerMiddleware {
|
|
72
78
|
}
|
73
79
|
|
74
80
|
/**
|
81
|
+
* @template {LazyTarget} TLazyTarget
|
75
82
|
* @param {EXPECTED_ANY} fn lazy function
|
76
|
-
* @param {
|
77
|
-
* @returns {
|
83
|
+
* @param {TLazyTarget=} target target middleware
|
84
|
+
* @returns {fn is LazyFunction<EXPECTED_ANY, EXPECTED_ANY, TLazyTarget, EXPECTED_ANY>} true, when fn is a lazy function (optionally of that target)
|
78
85
|
*/
|
79
86
|
static isLazy(fn, target) {
|
80
87
|
if (typeof fn !== "function") return false;
|
@@ -83,19 +90,26 @@ class SerializerMiddleware {
|
|
83
90
|
}
|
84
91
|
|
85
92
|
/**
|
86
|
-
* @template
|
87
|
-
* @
|
93
|
+
* @template TLazyInputValue
|
94
|
+
* @template TLazyOutputValue
|
95
|
+
* @template {LazyTarget} TLazyTarget
|
96
|
+
* @template {Record<string, EXPECTED_ANY>} TLazyOptions
|
97
|
+
* @template TLazySerializedValue
|
98
|
+
* @param {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} fn lazy function
|
88
99
|
* @returns {LazyOptions | undefined} options
|
89
100
|
*/
|
90
101
|
static getLazyOptions(fn) {
|
91
102
|
if (typeof fn !== "function") return;
|
92
|
-
return
|
103
|
+
return fn.options;
|
93
104
|
}
|
94
105
|
|
95
106
|
/**
|
96
|
-
* @template
|
97
|
-
* @
|
98
|
-
* @
|
107
|
+
* @template TLazyInputValue
|
108
|
+
* @template TLazyOutputValue
|
109
|
+
* @template {LazyTarget} TLazyTarget
|
110
|
+
* @template {LazyOptions} TLazyOptions
|
111
|
+
* @param {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions> | EXPECTED_ANY} fn lazy function
|
112
|
+
* @returns {TLazyOutputValue | undefined} serialized value
|
99
113
|
*/
|
100
114
|
static getLazySerializedValue(fn) {
|
101
115
|
if (typeof fn !== "function") return;
|
@@ -103,9 +117,12 @@ class SerializerMiddleware {
|
|
103
117
|
}
|
104
118
|
|
105
119
|
/**
|
106
|
-
* @template
|
107
|
-
* @
|
108
|
-
* @
|
120
|
+
* @template TLazyInputValue
|
121
|
+
* @template TLazyOutputValue
|
122
|
+
* @template {LazyTarget} TLazyTarget
|
123
|
+
* @template {LazyOptions} TLazyOptions
|
124
|
+
* @param {LazyFunction<TLazyInputValue, TLazyOutputValue, LazyTarget, LazyOptions>} fn lazy function
|
125
|
+
* @param {TLazyOutputValue} value serialized value
|
109
126
|
* @returns {void}
|
110
127
|
*/
|
111
128
|
static setLazySerializedValue(fn, value) {
|
@@ -113,27 +130,33 @@ class SerializerMiddleware {
|
|
113
130
|
}
|
114
131
|
|
115
132
|
/**
|
116
|
-
* @template
|
117
|
-
* @
|
118
|
-
* @
|
119
|
-
* @
|
133
|
+
* @template TLazyInputValue DeserializedValue
|
134
|
+
* @template TLazyOutputValue SerializedValue
|
135
|
+
* @template {LazyTarget} TLazyTarget
|
136
|
+
* @template {LazyOptions | undefined} TLazyOptions
|
137
|
+
* @param {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} lazy lazy function
|
138
|
+
* @param {(value: TLazyInputValue) => TLazyOutputValue} serialize serialize function
|
139
|
+
* @returns {LazyFunction<TLazyOutputValue, TLazyInputValue, TLazyTarget, TLazyOptions>} new lazy
|
120
140
|
*/
|
121
141
|
static serializeLazy(lazy, serialize) {
|
122
|
-
const fn =
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
r
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
142
|
+
const fn =
|
143
|
+
/** @type {LazyFunction<TLazyOutputValue, TLazyInputValue, TLazyTarget, TLazyOptions>} */
|
144
|
+
(
|
145
|
+
memoize(() => {
|
146
|
+
const r = lazy();
|
147
|
+
if (
|
148
|
+
r &&
|
149
|
+
typeof (/** @type {Promise<TLazyInputValue>} */ (r).then) ===
|
150
|
+
"function"
|
151
|
+
) {
|
152
|
+
return (
|
153
|
+
/** @type {Promise<TLazyInputValue>} */
|
154
|
+
(r).then(data => data && serialize(data))
|
155
|
+
);
|
156
|
+
}
|
157
|
+
return serialize(/** @type {TLazyInputValue} */ (r));
|
158
|
+
})
|
159
|
+
);
|
137
160
|
fn[LAZY_TARGET] = lazy[LAZY_TARGET];
|
138
161
|
fn.options = lazy.options;
|
139
162
|
lazy[LAZY_SERIALIZED_VALUE] = fn;
|
@@ -141,27 +164,33 @@ class SerializerMiddleware {
|
|
141
164
|
}
|
142
165
|
|
143
166
|
/**
|
144
|
-
* @template
|
145
|
-
* @
|
146
|
-
* @
|
147
|
-
* @
|
167
|
+
* @template TLazyInputValue SerializedValue
|
168
|
+
* @template TLazyOutputValue DeserializedValue
|
169
|
+
* @template SerializedValue
|
170
|
+
* @template {LazyTarget} TLazyTarget
|
171
|
+
* @template {LazyOptions | undefined} TLazyOptions
|
172
|
+
* @param {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} lazy lazy function
|
173
|
+
* @param {(data: TLazyInputValue) => TLazyOutputValue} deserialize deserialize function
|
174
|
+
* @returns {LazyFunction<TLazyOutputValue, TLazyInputValue, TLazyTarget, TLazyOptions>} new lazy
|
148
175
|
*/
|
149
176
|
static deserializeLazy(lazy, deserialize) {
|
150
|
-
const fn =
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
177
|
+
const fn =
|
178
|
+
/** @type {LazyFunction<TLazyOutputValue, TLazyInputValue, TLazyTarget, TLazyOptions>} */ (
|
179
|
+
memoize(() => {
|
180
|
+
const r = lazy();
|
181
|
+
if (
|
182
|
+
r &&
|
183
|
+
typeof (/** @type {Promise<TLazyInputValue>} */ (r).then) ===
|
184
|
+
"function"
|
185
|
+
) {
|
186
|
+
return (
|
187
|
+
/** @type {Promise<TLazyInputValue>} */
|
188
|
+
(r).then(data => deserialize(data))
|
189
|
+
);
|
190
|
+
}
|
191
|
+
return deserialize(/** @type {TLazyInputValue} */ (r));
|
192
|
+
})
|
193
|
+
);
|
165
194
|
fn[LAZY_TARGET] = lazy[LAZY_TARGET];
|
166
195
|
fn.options = lazy.options;
|
167
196
|
fn[LAZY_SERIALIZED_VALUE] = lazy;
|
@@ -169,20 +198,24 @@ class SerializerMiddleware {
|
|
169
198
|
}
|
170
199
|
|
171
200
|
/**
|
172
|
-
* @template
|
173
|
-
* @
|
174
|
-
* @
|
201
|
+
* @template TLazyInputValue
|
202
|
+
* @template TLazyOutputValue
|
203
|
+
* @template {LazyTarget} TLazyTarget
|
204
|
+
* @template {LazyOptions} TLazyOptions
|
205
|
+
* @param {LazyFunction<TLazyInputValue | TLazyOutputValue, TLazyInputValue | TLazyOutputValue, TLazyTarget, TLazyOptions> | undefined} lazy lazy function
|
206
|
+
* @returns {LazyFunction<TLazyInputValue | TLazyOutputValue, TLazyInputValue | TLazyOutputValue, TLazyTarget, TLazyOptions> | undefined} new lazy
|
175
207
|
*/
|
176
208
|
static unMemoizeLazy(lazy) {
|
177
209
|
if (!SerializerMiddleware.isLazy(lazy)) return lazy;
|
178
|
-
/** @type {LazyFunction<
|
210
|
+
/** @type {LazyFunction<TLazyInputValue | TLazyOutputValue, TLazyInputValue | TLazyOutputValue, TLazyTarget, TLazyOptions>} */
|
179
211
|
const fn = () => {
|
180
212
|
throw new Error(
|
181
213
|
"A lazy value that has been unmemorized can't be called again"
|
182
214
|
);
|
183
215
|
};
|
184
216
|
fn[LAZY_SERIALIZED_VALUE] = SerializerMiddleware.unMemoizeLazy(
|
185
|
-
|
217
|
+
/** @type {LazyFunction<TLazyInputValue | TLazyOutputValue, TLazyInputValue | TLazyOutputValue, TLazyTarget, TLazyOptions>} */
|
218
|
+
(lazy[LAZY_SERIALIZED_VALUE])
|
186
219
|
);
|
187
220
|
fn[LAZY_TARGET] = lazy[LAZY_TARGET];
|
188
221
|
fn.options = lazy.options;
|