webpack 5.104.1 → 5.105.1
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 +2 -2
- package/bin/webpack.js +6 -3
- package/lib/APIPlugin.js +85 -15
- package/lib/AbstractMethodError.js +1 -0
- package/lib/AsyncDependenciesBlock.js +17 -2
- package/lib/AsyncDependencyToInitialChunkError.js +3 -0
- package/lib/AutomaticPrefetchPlugin.js +1 -1
- package/lib/BannerPlugin.js +5 -1
- package/lib/CacheFacade.js +2 -2
- package/lib/CaseSensitiveModulesWarning.js +1 -0
- package/lib/Chunk.js +9 -5
- package/lib/ChunkGraph.js +30 -9
- package/lib/ChunkGroup.js +6 -3
- package/lib/ChunkRenderError.js +9 -2
- package/lib/CleanPlugin.js +4 -1
- package/lib/CodeGenerationError.js +7 -1
- package/lib/CodeGenerationResults.js +10 -3
- package/lib/CommentCompilationWarning.js +2 -1
- package/lib/Compilation.js +284 -236
- package/lib/Compiler.js +4 -3
- package/lib/ConcatenationScope.js +2 -1
- package/lib/ConcurrentCompilationError.js +3 -3
- package/lib/ContextModule.js +248 -111
- package/lib/ContextModuleFactory.js +14 -2
- package/lib/DefinePlugin.js +15 -8
- package/lib/DelegatedModule.js +6 -0
- package/lib/DelegatedModuleFactoryPlugin.js +2 -0
- package/lib/Dependency.js +6 -1
- package/lib/DependencyTemplates.js +3 -2
- package/lib/DllModule.js +2 -0
- package/lib/DllReferencePlugin.js +1 -0
- package/lib/DotenvPlugin.js +11 -5
- package/lib/DynamicEntryPlugin.js +8 -1
- package/lib/EnvironmentNotSupportAsyncWarning.js +1 -0
- package/lib/EvalSourceMapDevToolPlugin.js +17 -13
- package/lib/ExportsInfo.js +8 -4
- package/lib/ExternalModule.js +16 -0
- package/lib/ExternalModuleFactoryPlugin.js +9 -2
- package/lib/ExternalsPlugin.js +2 -1
- package/lib/FalseIIFEUmdWarning.js +1 -0
- package/lib/FileSystemInfo.js +49 -25
- package/lib/FlagDependencyExportsPlugin.js +8 -1
- package/lib/FlagDependencyUsagePlugin.js +9 -7
- package/lib/HarmonyLinkingError.js +1 -0
- package/lib/HookWebpackError.js +2 -0
- package/lib/HotModuleReplacementPlugin.js +16 -2
- package/lib/IgnoreWarningsPlugin.js +5 -2
- package/lib/InitFragment.js +1 -0
- package/lib/InvalidDependenciesModuleWarning.js +1 -0
- package/lib/LibManifestPlugin.js +1 -0
- package/lib/ManifestPlugin.js +8 -1
- package/lib/Module.js +7 -4
- package/lib/ModuleBuildError.js +3 -1
- package/lib/ModuleDependencyError.js +1 -0
- package/lib/ModuleDependencyWarning.js +1 -0
- package/lib/ModuleError.js +4 -1
- package/lib/ModuleFilenameHelpers.js +4 -2
- package/lib/ModuleGraph.js +3 -0
- package/lib/ModuleGraphConnection.js +10 -1
- package/lib/ModuleHashingError.js +2 -0
- package/lib/ModuleInfoHeaderPlugin.js +6 -1
- package/lib/ModuleNotFoundError.js +1 -0
- package/lib/ModuleParseError.js +5 -1
- package/lib/ModuleProfile.js +1 -1
- package/lib/ModuleRestoreError.js +2 -0
- package/lib/ModuleSourceTypeConstants.js +1 -11
- package/lib/ModuleStoreError.js +2 -0
- package/lib/ModuleWarning.js +3 -1
- package/lib/MultiCompiler.js +11 -7
- package/lib/MultiWatching.js +2 -2
- package/lib/NoModeWarning.js +1 -0
- package/lib/NodeStuffInWebError.js +1 -0
- package/lib/NormalModule.js +28 -5
- package/lib/NormalModuleFactory.js +20 -4
- package/lib/OptionsApply.js +3 -1
- package/lib/ProgressPlugin.js +4 -0
- package/lib/RawModule.js +2 -0
- package/lib/RuntimeModule.js +10 -0
- package/lib/RuntimePlugin.js +7 -9
- package/lib/RuntimeTemplate.js +14 -7
- package/lib/SourceMapDevToolPlugin.js +30 -23
- package/lib/Template.js +10 -5
- package/lib/TemplatedPathPlugin.js +4 -2
- package/lib/UnhandledSchemeError.js +1 -0
- package/lib/UnsupportedFeatureWarning.js +3 -0
- package/lib/WarnDeprecatedOptionPlugin.js +1 -0
- package/lib/WatchIgnorePlugin.js +1 -1
- package/lib/Watching.js +5 -0
- package/lib/WebpackError.js +4 -0
- package/lib/WebpackOptionsApply.js +61 -41
- package/lib/asset/AssetBytesGenerator.js +1 -0
- package/lib/asset/AssetGenerator.js +18 -3
- package/lib/asset/AssetModulesPlugin.js +33 -2
- package/lib/asset/AssetParser.js +4 -0
- package/lib/asset/AssetSourceGenerator.js +1 -0
- package/lib/asset/RawDataUrlModule.js +4 -0
- package/lib/async-modules/AwaitDependenciesInitFragment.js +4 -1
- package/lib/buildChunkGraph.js +31 -2
- package/lib/cache/MemoryWithGcCachePlugin.js +1 -0
- package/lib/cache/PackFileCacheStrategy.js +41 -8
- package/lib/cache/getLazyHashedEtag.js +13 -4
- package/lib/cache/mergeEtags.js +3 -1
- package/lib/cli.js +3 -1
- package/lib/config/defaults.js +65 -44
- package/lib/config/normalization.js +96 -0
- package/lib/container/ContainerEntryDependency.js +3 -0
- package/lib/container/ContainerEntryModule.js +7 -0
- package/lib/container/ContainerExposedDependency.js +1 -0
- package/lib/container/FallbackDependency.js +1 -0
- package/lib/container/FallbackItemDependency.js +1 -0
- package/lib/container/FallbackModule.js +4 -0
- package/lib/container/HoistContainerReferencesPlugin.js +6 -0
- package/lib/container/ModuleFederationPlugin.js +1 -1
- package/lib/container/RemoteModule.js +24 -0
- package/lib/css/CssGenerator.js +31 -4
- package/lib/css/CssModulesPlugin.js +26 -30
- package/lib/css/CssParser.js +12 -9
- package/lib/debug/ProfilingPlugin.js +1 -0
- package/lib/dependencies/AMDDefineDependency.js +3 -1
- package/lib/dependencies/AMDDefineDependencyParserPlugin.js +6 -0
- package/lib/dependencies/AMDPlugin.js +1 -1
- package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +7 -1
- package/lib/dependencies/CachedConstDependency.js +1 -0
- package/lib/dependencies/CommonJsDependencyHelpers.js +2 -0
- package/lib/dependencies/CommonJsExportRequireDependency.js +1 -0
- package/lib/dependencies/CommonJsFullRequireDependency.js +2 -0
- package/lib/dependencies/CommonJsImportsParserPlugin.js +3 -0
- package/lib/dependencies/CommonJsRequireContextDependency.js +2 -2
- package/lib/dependencies/CommonJsSelfReferenceDependency.js +1 -0
- package/lib/dependencies/ConstDependency.js +1 -0
- package/lib/dependencies/ContextDependencyHelpers.js +11 -4
- package/lib/dependencies/ContextElementDependency.js +1 -0
- package/lib/dependencies/CriticalDependencyWarning.js +1 -0
- package/lib/dependencies/CssIcssExportDependency.js +7 -4
- package/lib/dependencies/CssIcssSymbolDependency.js +1 -0
- package/lib/dependencies/CssUrlDependency.js +2 -3
- package/lib/dependencies/ExternalModuleInitFragment.js +4 -1
- package/lib/dependencies/ExternalModuleInitFragmentDependency.js +0 -9
- package/lib/dependencies/HarmonyAcceptDependency.js +3 -0
- package/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js +3 -1
- package/lib/dependencies/HarmonyExportExpressionDependency.js +4 -0
- package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +8 -1
- package/lib/dependencies/HarmonyExportInitFragment.js +17 -3
- package/lib/dependencies/HarmonyExportSpecifierDependency.js +4 -0
- package/lib/dependencies/HarmonyImportDependency.js +1 -1
- package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +269 -5
- package/lib/dependencies/HarmonyImportSpecifierDependency.js +22 -3
- package/lib/dependencies/ImportDependency.js +1 -0
- package/lib/dependencies/ImportMetaContextDependencyParserPlugin.js +1 -0
- package/lib/dependencies/ImportMetaPlugin.js +53 -11
- package/lib/dependencies/ImportParserPlugin.js +7 -12
- package/lib/dependencies/LoaderPlugin.js +3 -0
- package/lib/dependencies/ModuleDecoratorDependency.js +1 -0
- package/lib/dependencies/ModuleDependency.js +1 -0
- package/lib/dependencies/ModuleInitFragmentDependency.js +87 -0
- package/lib/dependencies/ProvidedDependency.js +1 -0
- package/lib/dependencies/RequireContextPlugin.js +1 -0
- package/lib/dependencies/RequireEnsureDependenciesBlock.js +1 -1
- package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +7 -2
- package/lib/dependencies/RuntimeRequirementsDependency.js +1 -0
- package/lib/dependencies/WorkerDependency.js +9 -3
- package/lib/dependencies/WorkerPlugin.js +1 -0
- package/lib/dependencies/getFunctionExpression.js +3 -1
- package/lib/electron/ElectronTargetPlugin.js +3 -1
- package/lib/errors/BuildCycleError.js +4 -1
- package/lib/esm/ModuleChunkFormatPlugin.js +6 -4
- package/lib/esm/ModuleChunkLoadingPlugin.js +1 -0
- package/lib/esm/ModuleChunkLoadingRuntimeModule.js +1 -0
- package/lib/hmr/LazyCompilationPlugin.js +5 -0
- package/lib/hmr/lazyCompilationBackend.js +3 -2
- package/lib/ids/ChunkModuleIdRangePlugin.js +9 -7
- package/lib/ids/DeterministicChunkIdsPlugin.js +1 -0
- package/lib/ids/DeterministicModuleIdsPlugin.js +1 -0
- package/lib/ids/HashedModuleIdsPlugin.js +3 -8
- package/lib/ids/IdHelpers.js +10 -8
- package/lib/ids/NamedChunkIdsPlugin.js +7 -5
- package/lib/ids/NamedModuleIdsPlugin.js +1 -0
- package/lib/ids/OccurrenceChunkIdsPlugin.js +2 -2
- package/lib/ids/OccurrenceModuleIdsPlugin.js +18 -10
- package/lib/ids/SyncModuleIdsPlugin.js +30 -20
- package/lib/index.js +2 -2
- package/lib/javascript/BasicEvaluatedExpression.js +1 -0
- package/lib/javascript/EnableChunkLoadingPlugin.js +5 -2
- package/lib/javascript/JavascriptModulesPlugin.js +29 -8
- package/lib/javascript/JavascriptParser.js +47 -14
- package/lib/json/JsonGenerator.js +1 -0
- package/lib/json/JsonParser.js +9 -2
- package/lib/library/AbstractLibraryPlugin.js +4 -1
- package/lib/library/AmdLibraryPlugin.js +2 -1
- package/lib/library/AssignLibraryPlugin.js +6 -2
- package/lib/library/EnableLibraryPlugin.js +1 -0
- package/lib/library/ExportPropertyLibraryPlugin.js +1 -1
- package/lib/library/JsonpLibraryPlugin.js +1 -1
- package/lib/library/ModuleLibraryPlugin.js +130 -13
- package/lib/library/SystemLibraryPlugin.js +4 -1
- package/lib/library/UmdLibraryPlugin.js +4 -3
- package/lib/logging/Logger.js +9 -5
- package/lib/logging/createConsoleLogger.js +4 -0
- package/lib/logging/truncateArgs.js +1 -1
- package/lib/node/CommonJsChunkLoadingPlugin.js +6 -4
- package/lib/node/NodeEnvironmentPlugin.js +1 -0
- package/lib/node/NodeTargetPlugin.js +10 -1
- package/lib/node/NodeTemplatePlugin.js +1 -0
- package/lib/node/NodeWatchFileSystem.js +6 -0
- package/lib/node/ReadFileChunkLoadingRuntimeModule.js +1 -0
- package/lib/node/ReadFileCompileAsyncWasmPlugin.js +1 -0
- package/lib/node/ReadFileCompileWasmPlugin.js +1 -0
- package/lib/node/RequireChunkLoadingRuntimeModule.js +1 -0
- package/lib/node/nodeConsole.js +18 -1
- package/lib/optimize/AggressiveMergingPlugin.js +2 -1
- package/lib/optimize/AggressiveSplittingPlugin.js +15 -6
- package/lib/optimize/ConcatenatedModule.js +33 -19
- package/lib/optimize/FlagIncludedChunksPlugin.js +11 -1
- package/lib/optimize/InnerGraph.js +5 -3
- package/lib/optimize/InnerGraphPlugin.js +25 -25
- package/lib/optimize/LimitChunkCountPlugin.js +3 -2
- package/lib/optimize/MangleExportsPlugin.js +14 -2
- package/lib/optimize/MergeDuplicateChunksPlugin.js +5 -1
- package/lib/optimize/MinChunkSizePlugin.js +5 -2
- package/lib/optimize/ModuleConcatenationPlugin.js +19 -4
- package/lib/optimize/RealContentHashPlugin.js +37 -27
- package/lib/optimize/RemoveParentModulesPlugin.js +12 -3
- package/lib/optimize/RuntimeChunkPlugin.js +3 -5
- package/lib/optimize/SideEffectsFlagPlugin.js +3 -0
- package/lib/optimize/SplitChunksPlugin.js +23 -5
- package/lib/performance/AssetsOverSizeLimitWarning.js +6 -2
- package/lib/performance/EntrypointsOverSizeLimitWarning.js +6 -2
- package/lib/performance/NoAsyncChunksWarning.js +1 -0
- package/lib/performance/SizeLimitsPlugin.js +6 -6
- package/lib/prefetch/ChunkPrefetchFunctionRuntimeModule.js +2 -0
- package/lib/prefetch/ChunkPrefetchStartupRuntimeModule.js +3 -1
- package/lib/prefetch/ChunkPrefetchTriggerRuntimeModule.js +1 -0
- package/lib/prefetch/ChunkPreloadTriggerRuntimeModule.js +1 -0
- package/lib/rules/BasicEffectRulePlugin.js +2 -0
- package/lib/rules/BasicMatcherRulePlugin.js +3 -0
- package/lib/rules/ObjectMatcherRulePlugin.js +5 -1
- package/lib/rules/RuleSetCompiler.js +6 -0
- package/lib/runtime/AsyncModuleRuntimeModule.js +1 -0
- package/lib/runtime/ChunkNameRuntimeModule.js +1 -0
- package/lib/runtime/CompatRuntimeModule.js +1 -0
- package/lib/runtime/EnsureChunkRuntimeModule.js +1 -0
- package/lib/runtime/GetChunkFilenameRuntimeModule.js +5 -0
- package/lib/runtime/GetFullHashRuntimeModule.js +1 -0
- package/lib/runtime/GetMainFilenameRuntimeModule.js +2 -0
- package/lib/runtime/GetTrustedTypesPolicyRuntimeModule.js +1 -0
- package/lib/runtime/LoadScriptRuntimeModule.js +2 -0
- package/lib/runtime/MakeDeferredNamespaceObjectRuntime.js +16 -32
- package/lib/runtime/PublicPathRuntimeModule.js +1 -0
- package/lib/runtime/StartupChunkDependenciesPlugin.js +2 -0
- package/lib/runtime/StartupChunkDependenciesRuntimeModule.js +1 -0
- package/lib/runtime/StartupEntrypointRuntimeModule.js +1 -0
- package/lib/schemes/HttpUriPlugin.js +39 -20
- package/lib/schemes/VirtualUrlPlugin.js +28 -2
- package/lib/serialization/BinaryMiddleware.js +4 -0
- package/lib/serialization/FileMiddleware.js +9 -4
- package/lib/serialization/ObjectMiddleware.js +34 -11
- package/lib/serialization/PlainObjectSerializer.js +2 -0
- package/lib/sharing/ConsumeSharedModule.js +60 -0
- package/lib/sharing/ConsumeSharedPlugin.js +5 -4
- package/lib/sharing/ProvideSharedModule.js +4 -0
- package/lib/sharing/ProvideSharedPlugin.js +24 -25
- package/lib/sharing/resolveMatchedConfigs.js +2 -2
- package/lib/sharing/utils.js +8 -6
- package/lib/stats/DefaultStatsFactoryPlugin.js +20 -12
- package/lib/stats/DefaultStatsPrinterPlugin.js +6 -5
- package/lib/stats/StatsFactory.js +2 -2
- package/lib/url/URLParserPlugin.js +5 -3
- package/lib/util/AppendOnlyStackedSet.js +57 -0
- package/lib/util/Hash.js +1 -0
- package/lib/util/IterableHelpers.js +1 -0
- package/lib/util/LazyBucketSortedSet.js +1 -0
- package/lib/util/LazySet.js +1 -0
- package/lib/util/ParallelismFactorCalculator.js +1 -0
- package/lib/util/SortableSet.js +1 -0
- package/lib/util/StringXor.js +1 -0
- package/lib/util/binarySearchBounds.js +15 -8
- package/lib/util/chainedImports.js +1 -1
- package/lib/util/cleverMerge.js +6 -1
- package/lib/util/comparators.js +21 -1
- package/lib/util/compileBooleanMatcher.js +3 -3
- package/lib/util/concatenate.js +3 -2
- package/lib/util/conventions.js +3 -2
- package/lib/util/deterministicGrouping.js +13 -1
- package/lib/util/extractSourceMap.js +12 -13
- package/lib/util/fs.js +15 -19
- package/lib/util/hash/BatchedHash.js +4 -0
- package/lib/util/hash/BulkUpdateHash.js +8 -1
- package/lib/util/hash/hash-digest.js +1 -0
- package/lib/util/hash/wasm-hash.js +18 -2
- package/lib/util/identifier.js +7 -2
- package/lib/util/magicComment.js +1 -1
- package/lib/util/propertyName.js +1 -1
- package/lib/util/registerExternalSerializer.js +1 -0
- package/lib/util/runtime.js +15 -1
- package/lib/util/semver.js +1 -0
- package/lib/util/serialization.js +2 -2
- package/lib/wasm/EnableWasmLoadingPlugin.js +2 -0
- package/lib/wasm-async/AsyncWasmLoadingRuntimeModule.js +11 -3
- package/lib/wasm-async/AsyncWebAssemblyGenerator.js +1 -0
- package/lib/wasm-async/AsyncWebAssemblyModulesPlugin.js +3 -2
- package/lib/wasm-sync/UnsupportedWebAssemblyFeatureError.js +5 -1
- package/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +3 -0
- package/lib/wasm-sync/WebAssemblyInInitialChunkError.js +2 -0
- package/lib/wasm-sync/WebAssemblyJavascriptGenerator.js +7 -3
- package/lib/wasm-sync/WebAssemblyModulesPlugin.js +2 -2
- package/lib/wasm-sync/WebAssemblyParser.js +3 -1
- package/lib/web/FetchCompileWasmPlugin.js +1 -0
- package/lib/web/JsonpChunkLoadingPlugin.js +1 -0
- package/lib/web/JsonpChunkLoadingRuntimeModule.js +1 -0
- package/lib/webpack.js +16 -4
- package/lib/webworker/ImportScriptsChunkLoadingPlugin.js +1 -0
- package/lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js +2 -0
- package/package.json +33 -36
- package/schemas/WebpackOptions.check.js +1 -1
- package/schemas/WebpackOptions.json +121 -55
- package/schemas/plugins/BannerPlugin.json +2 -2
- package/schemas/plugins/IgnorePlugin.json +1 -1
- package/schemas/plugins/ManifestPlugin.json +3 -3
- package/schemas/plugins/ProgressPlugin.json +1 -1
- package/schemas/plugins/SourceMapDevToolPlugin.json +2 -2
- package/schemas/plugins/schemes/HttpUriPlugin.json +1 -1
- package/schemas/plugins/schemes/VirtualUrlPlugin.json +3 -3
- package/types.d.ts +440 -137
|
@@ -17,11 +17,12 @@ const HelperRuntimeModule = require("./HelperRuntimeModule");
|
|
|
17
17
|
* @returns {string} mode
|
|
18
18
|
*/
|
|
19
19
|
function getMakeDeferredNamespaceModeFromExportsType(exportsType) {
|
|
20
|
-
|
|
21
|
-
if (exportsType === "
|
|
20
|
+
// number is from createFakeNamespaceObject mode ^ 1
|
|
21
|
+
if (exportsType === "namespace") return `/* ${exportsType} */ 8`;
|
|
22
|
+
if (exportsType === "default-only") return `/* ${exportsType} */ 0`;
|
|
22
23
|
if (exportsType === "default-with-named") return `/* ${exportsType} */ 2`;
|
|
23
|
-
if (exportsType === "dynamic") return `/* ${exportsType} */
|
|
24
|
-
|
|
24
|
+
if (exportsType === "dynamic") return `/* ${exportsType} */ 6`;
|
|
25
|
+
throw new Error(`Unknown exports type: ${exportsType}`);
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
/**
|
|
@@ -52,6 +53,7 @@ class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeMod
|
|
|
52
53
|
*/
|
|
53
54
|
constructor(hasAsyncRuntime) {
|
|
54
55
|
super("make optimized deferred namespace object");
|
|
56
|
+
/** @type {boolean} */
|
|
55
57
|
this.hasAsyncRuntime = hasAsyncRuntime;
|
|
56
58
|
}
|
|
57
59
|
|
|
@@ -66,10 +68,6 @@ class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeMod
|
|
|
66
68
|
// Note: must be a function (not arrow), because this is used in body!
|
|
67
69
|
`${fn} = function(moduleId, mode${hasAsync ? ", asyncDeps" : ""}) {`,
|
|
68
70
|
Template.indent([
|
|
69
|
-
"// mode: 0 => namespace (esm)",
|
|
70
|
-
"// mode: 1 => default-only (esm strict cjs)",
|
|
71
|
-
"// mode: 2 => default-with-named (esm-cjs compat)",
|
|
72
|
-
"// mode: 3 => dynamic (if exports has __esModule, then esm, otherwise default-with-named)",
|
|
73
71
|
"var r = this;",
|
|
74
72
|
hasAsync ? "var isAsync = asyncDeps && asyncDeps.length;" : "",
|
|
75
73
|
"var obj = {",
|
|
@@ -83,7 +81,7 @@ class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeMod
|
|
|
83
81
|
// if exportsType is "namespace" we can generate the most optimized code,
|
|
84
82
|
// on the second access, we can avoid trigger the getter.
|
|
85
83
|
// we can also do this if exportsType is "dynamic" and there is a "__esModule" property on it.
|
|
86
|
-
'if(mode
|
|
84
|
+
'if(mode & 8 || (mode & 4 && exports.__esModule)) Object.defineProperty(this, "a", { value: exports });',
|
|
87
85
|
"return exports;"
|
|
88
86
|
]),
|
|
89
87
|
"}"
|
|
@@ -105,6 +103,7 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
|
|
|
105
103
|
*/
|
|
106
104
|
constructor(hasAsyncRuntime) {
|
|
107
105
|
super("make deferred namespace object");
|
|
106
|
+
/** @type {boolean} */
|
|
108
107
|
this.hasAsyncRuntime = hasAsyncRuntime;
|
|
109
108
|
}
|
|
110
109
|
|
|
@@ -120,11 +119,6 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
|
|
|
120
119
|
? "init?.();"
|
|
121
120
|
: "if (init) init();";
|
|
122
121
|
return `${fn} = ${runtimeTemplate.basicFunction("moduleId, mode", [
|
|
123
|
-
"// mode: 0 => namespace (esm)",
|
|
124
|
-
"// mode: 1 => default-only (esm strict cjs)",
|
|
125
|
-
"// mode: 2 => default-with-named (esm-cjs compat)",
|
|
126
|
-
"// mode: 3 => dynamic (if exports has __esModule, then esm, otherwise default-with-named)",
|
|
127
|
-
"",
|
|
128
122
|
"var cachedModule = __webpack_module_cache__[moduleId];",
|
|
129
123
|
"if (cachedModule && cachedModule.error === undefined) {",
|
|
130
124
|
Template.indent([
|
|
@@ -132,10 +126,8 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
|
|
|
132
126
|
hasAsync
|
|
133
127
|
? `if (${RuntimeGlobals.asyncModuleExportSymbol} in exports) exports = exports[${RuntimeGlobals.asyncModuleExportSymbol}];`
|
|
134
128
|
: "",
|
|
135
|
-
"if (mode
|
|
136
|
-
`
|
|
137
|
-
`if (mode == 2) return ${RuntimeGlobals.createFakeNamespaceObject}(exports, 2);`,
|
|
138
|
-
`if (mode == 3) return ${RuntimeGlobals.createFakeNamespaceObject}(exports, 6);` // 2 | 4
|
|
129
|
+
"if (mode & 8) return exports;",
|
|
130
|
+
`return ${RuntimeGlobals.createFakeNamespaceObject}(exports, mode);`
|
|
139
131
|
]),
|
|
140
132
|
"}",
|
|
141
133
|
"",
|
|
@@ -145,7 +137,7 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
|
|
|
145
137
|
? `if (${RuntimeGlobals.asyncModuleExportSymbol} in ns) ns = ns[${RuntimeGlobals.asyncModuleExportSymbol}];`
|
|
146
138
|
: "",
|
|
147
139
|
"init = null;",
|
|
148
|
-
"if (mode
|
|
140
|
+
"if (mode & 8 || mode & 4 && ns.__esModule && typeof ns === 'object') {",
|
|
149
141
|
Template.indent([
|
|
150
142
|
"delete handler.defineProperty;",
|
|
151
143
|
"delete handler.deleteProperty;",
|
|
@@ -155,17 +147,9 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
|
|
|
155
147
|
"delete handler.ownKeys;",
|
|
156
148
|
"delete handler.getOwnPropertyDescriptor;"
|
|
157
149
|
]),
|
|
158
|
-
"} else
|
|
159
|
-
Template.indent([
|
|
160
|
-
`ns = ${RuntimeGlobals.createFakeNamespaceObject}(ns);`
|
|
161
|
-
]),
|
|
162
|
-
"} else if (mode == 2) {",
|
|
163
|
-
Template.indent([
|
|
164
|
-
`ns = ${RuntimeGlobals.createFakeNamespaceObject}(ns, 2);`
|
|
165
|
-
]),
|
|
166
|
-
"} else if (mode == 3) {",
|
|
150
|
+
"} else {",
|
|
167
151
|
Template.indent([
|
|
168
|
-
`ns = ${RuntimeGlobals.createFakeNamespaceObject}(ns,
|
|
152
|
+
`ns = ${RuntimeGlobals.createFakeNamespaceObject}(ns, mode);`
|
|
169
153
|
]),
|
|
170
154
|
"}"
|
|
171
155
|
])};`,
|
|
@@ -211,14 +195,14 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
|
|
|
211
195
|
`getOwnPropertyDescriptor: ${runtimeTemplate.basicFunction("_, name", [
|
|
212
196
|
"switch (name) {",
|
|
213
197
|
Template.indent([
|
|
214
|
-
'case "__esModule": return { value: true, configurable:
|
|
215
|
-
'case Symbol.toStringTag: return { value: "Deferred Module", configurable:
|
|
198
|
+
'case "__esModule": return { value: true, configurable: !(mode & 8) };',
|
|
199
|
+
'case Symbol.toStringTag: return { value: "Deferred Module", configurable: !(mode & 8) };',
|
|
216
200
|
'case "then": return undefined;'
|
|
217
201
|
]),
|
|
218
202
|
"}",
|
|
219
203
|
init,
|
|
220
204
|
"var desc = Reflect.getOwnPropertyDescriptor(ns, name);",
|
|
221
|
-
'if (mode
|
|
205
|
+
'if (mode & 2 && name == "default" && !desc) {',
|
|
222
206
|
Template.indent("desc = { value: ns, configurable: true };"),
|
|
223
207
|
"}",
|
|
224
208
|
"return desc;"
|
|
@@ -25,7 +25,9 @@ class StartupChunkDependenciesPlugin {
|
|
|
25
25
|
* @param {Options} options options
|
|
26
26
|
*/
|
|
27
27
|
constructor(options) {
|
|
28
|
+
/** @type {ChunkLoadingType} */
|
|
28
29
|
this.chunkLoading = options.chunkLoading;
|
|
30
|
+
/** @type {boolean} */
|
|
29
31
|
this.asyncChunkLoading =
|
|
30
32
|
typeof options.asyncChunkLoading === "boolean"
|
|
31
33
|
? options.asyncChunkLoading
|
|
@@ -36,12 +36,20 @@ const getHttps = memoize(() => require("https"));
|
|
|
36
36
|
|
|
37
37
|
const MAX_REDIRECTS = 5;
|
|
38
38
|
|
|
39
|
+
/** @typedef {(url: URL, requestOptions: RequestOptions, callback: (incomingMessage: IncomingMessage) => void) => EventEmitter} Fetch */
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @typedef {object} EventsMap
|
|
43
|
+
* @property {[Error]} error
|
|
44
|
+
*/
|
|
45
|
+
|
|
39
46
|
/**
|
|
40
47
|
* @param {typeof import("http") | typeof import("https")} request request
|
|
41
48
|
* @param {string | URL | undefined} proxy proxy
|
|
42
|
-
* @returns {
|
|
49
|
+
* @returns {Fetch} fn
|
|
43
50
|
*/
|
|
44
51
|
const proxyFetch = (request, proxy) => (url, options, callback) => {
|
|
52
|
+
/** @type {EventEmitter<EventsMap>} */
|
|
45
53
|
const eventEmitter = new EventEmitter();
|
|
46
54
|
|
|
47
55
|
/**
|
|
@@ -104,9 +112,7 @@ const validate = createSchemaValidation(
|
|
|
104
112
|
* @returns {string} safe path
|
|
105
113
|
*/
|
|
106
114
|
const toSafePath = (str) =>
|
|
107
|
-
str
|
|
108
|
-
.replace(/^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$/g, "")
|
|
109
|
-
.replace(/[^a-zA-Z0-9._-]+/g, "_");
|
|
115
|
+
str.replace(/^[^a-z0-9]+|[^a-z0-9]+$/gi, "").replace(/[^a-z0-9._-]+/gi, "_");
|
|
110
116
|
|
|
111
117
|
/**
|
|
112
118
|
* @param {Buffer} content content
|
|
@@ -220,6 +226,7 @@ const sanitizeUrlForError = (href) => {
|
|
|
220
226
|
|
|
221
227
|
class Lockfile {
|
|
222
228
|
constructor() {
|
|
229
|
+
/** @type {number} */
|
|
223
230
|
this.version = 1;
|
|
224
231
|
/** @type {Map<string, LockfileEntry | "ignore" | "no-cache">} */
|
|
225
232
|
this.entries = new Map();
|
|
@@ -430,6 +437,8 @@ const cachedWithKey = (fn, forceFn = fn) => {
|
|
|
430
437
|
/** @typedef {FetchResultMeta & { entry: LockfileEntry, content: Buffer }} ContentFetchResult */
|
|
431
438
|
/** @typedef {RedirectFetchResult | ContentFetchResult} FetchResult */
|
|
432
439
|
|
|
440
|
+
/** @typedef {(uri: string) => boolean} AllowedUriFn */
|
|
441
|
+
|
|
433
442
|
const PLUGIN_NAME = "HttpUriPlugin";
|
|
434
443
|
|
|
435
444
|
class HttpUriPlugin {
|
|
@@ -438,12 +447,8 @@ class HttpUriPlugin {
|
|
|
438
447
|
*/
|
|
439
448
|
constructor(options) {
|
|
440
449
|
validate(options);
|
|
441
|
-
|
|
442
|
-
this.
|
|
443
|
-
this._upgrade = options.upgrade;
|
|
444
|
-
this._frozen = options.frozen;
|
|
445
|
-
this._allowedUris = options.allowedUris;
|
|
446
|
-
this._proxy = options.proxy;
|
|
450
|
+
/** @type {HttpUriPluginOptions} */
|
|
451
|
+
this.options = options;
|
|
447
452
|
}
|
|
448
453
|
|
|
449
454
|
/**
|
|
@@ -453,7 +458,10 @@ class HttpUriPlugin {
|
|
|
453
458
|
*/
|
|
454
459
|
apply(compiler) {
|
|
455
460
|
const proxy =
|
|
456
|
-
this.
|
|
461
|
+
this.options.proxy || process.env.http_proxy || process.env.HTTP_PROXY;
|
|
462
|
+
/**
|
|
463
|
+
* @type {{ scheme: "http" | "https", fetch: Fetch }[]}
|
|
464
|
+
*/
|
|
457
465
|
const schemes = [
|
|
458
466
|
{
|
|
459
467
|
scheme: "http",
|
|
@@ -477,7 +485,7 @@ class HttpUriPlugin {
|
|
|
477
485
|
const logger = compilation.getLogger(`webpack.${PLUGIN_NAME}`);
|
|
478
486
|
/** @type {string} */
|
|
479
487
|
const lockfileLocation =
|
|
480
|
-
this.
|
|
488
|
+
this.options.lockfileLocation ||
|
|
481
489
|
join(
|
|
482
490
|
intermediateFs,
|
|
483
491
|
compiler.context,
|
|
@@ -487,15 +495,15 @@ class HttpUriPlugin {
|
|
|
487
495
|
);
|
|
488
496
|
/** @type {string | false} */
|
|
489
497
|
const cacheLocation =
|
|
490
|
-
this.
|
|
491
|
-
? this.
|
|
498
|
+
this.options.cacheLocation !== undefined
|
|
499
|
+
? this.options.cacheLocation
|
|
492
500
|
: `${lockfileLocation}.data`;
|
|
493
|
-
const upgrade = this.
|
|
494
|
-
const frozen = this.
|
|
501
|
+
const upgrade = this.options.upgrade || false;
|
|
502
|
+
const frozen = this.options.frozen || false;
|
|
495
503
|
const hashFunction = "sha512";
|
|
496
504
|
const hashDigest = "hex";
|
|
497
505
|
const hashDigestLength = 20;
|
|
498
|
-
const allowedUris = this.
|
|
506
|
+
const allowedUris = this.options.allowedUris;
|
|
499
507
|
|
|
500
508
|
let warnedAboutEol = false;
|
|
501
509
|
|
|
@@ -660,6 +668,7 @@ class HttpUriPlugin {
|
|
|
660
668
|
* @returns {string} absolute, validated redirect target
|
|
661
669
|
*/
|
|
662
670
|
const validateRedirectLocation = (location, base) => {
|
|
671
|
+
/** @type {URL} */
|
|
663
672
|
let nextUrl;
|
|
664
673
|
try {
|
|
665
674
|
nextUrl = new URL(location, base);
|
|
@@ -707,6 +716,7 @@ class HttpUriPlugin {
|
|
|
707
716
|
|
|
708
717
|
if ("location" in result) {
|
|
709
718
|
// Validate redirect target before following
|
|
719
|
+
/** @type {string} */
|
|
710
720
|
let absolute;
|
|
711
721
|
try {
|
|
712
722
|
absolute = validateRedirectLocation(result.location, url);
|
|
@@ -845,6 +855,7 @@ class HttpUriPlugin {
|
|
|
845
855
|
res.statusCode >= 301 &&
|
|
846
856
|
res.statusCode <= 308
|
|
847
857
|
) {
|
|
858
|
+
/** @type {string} */
|
|
848
859
|
let absolute;
|
|
849
860
|
try {
|
|
850
861
|
absolute = validateRedirectLocation(location, url);
|
|
@@ -891,9 +902,15 @@ class HttpUriPlugin {
|
|
|
891
902
|
stream = stream.pipe(createInflate());
|
|
892
903
|
}
|
|
893
904
|
|
|
894
|
-
stream.on(
|
|
895
|
-
|
|
896
|
-
|
|
905
|
+
stream.on(
|
|
906
|
+
"data",
|
|
907
|
+
/**
|
|
908
|
+
* @param {Buffer} chunk chunk
|
|
909
|
+
*/
|
|
910
|
+
(chunk) => {
|
|
911
|
+
bufferArr.push(chunk);
|
|
912
|
+
}
|
|
913
|
+
);
|
|
897
914
|
|
|
898
915
|
stream.on("end", () => {
|
|
899
916
|
if (!res.complete) {
|
|
@@ -953,6 +970,7 @@ class HttpUriPlugin {
|
|
|
953
970
|
* @returns {boolean} true when allowed, otherwise false
|
|
954
971
|
*/
|
|
955
972
|
const isAllowed = (uri) => {
|
|
973
|
+
/** @type {URL} */
|
|
956
974
|
let parsedUri;
|
|
957
975
|
try {
|
|
958
976
|
// Parse the URI to prevent userinfo bypass attacks
|
|
@@ -963,6 +981,7 @@ class HttpUriPlugin {
|
|
|
963
981
|
}
|
|
964
982
|
for (const allowed of allowedUris) {
|
|
965
983
|
if (typeof allowed === "string") {
|
|
984
|
+
/** @type {URL} */
|
|
966
985
|
let parsedAllowed;
|
|
967
986
|
try {
|
|
968
987
|
parsedAllowed = new URL(allowed);
|
|
@@ -58,17 +58,19 @@ function normalizeModule(virtualConfig) {
|
|
|
58
58
|
return virtualConfig;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
/** @typedef {{ [key: string]: VirtualModuleConfig }} NormalizedModules */
|
|
62
|
+
|
|
61
63
|
/**
|
|
62
64
|
* Normalizes all virtual modules with the given scheme
|
|
63
65
|
* @param {VirtualModules} virtualConfigs The virtual modules to normalize
|
|
64
66
|
* @param {string} scheme The URL scheme to use
|
|
65
|
-
* @returns {
|
|
67
|
+
* @returns {NormalizedModules} The normalized virtual modules
|
|
66
68
|
*/
|
|
67
69
|
function normalizeModules(virtualConfigs, scheme) {
|
|
68
70
|
return Object.keys(virtualConfigs).reduce((pre, id) => {
|
|
69
71
|
pre[toVid(id, scheme)] = normalizeModule(virtualConfigs[id]);
|
|
70
72
|
return pre;
|
|
71
|
-
}, /** @type {
|
|
73
|
+
}, /** @type {NormalizedModules} */ ({}));
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
/**
|
|
@@ -105,7 +107,9 @@ class VirtualUrlPlugin {
|
|
|
105
107
|
* @param {string=} scheme The URL scheme to use
|
|
106
108
|
*/
|
|
107
109
|
constructor(modules, scheme) {
|
|
110
|
+
/** @type {string} */
|
|
108
111
|
this.scheme = scheme || DEFAULT_SCHEME;
|
|
112
|
+
/** @type {NormalizedModules} */
|
|
109
113
|
this.modules = normalizeModules(modules, this.scheme);
|
|
110
114
|
}
|
|
111
115
|
|
|
@@ -122,6 +126,27 @@ class VirtualUrlPlugin {
|
|
|
122
126
|
compiler.hooks.compilation.tap(
|
|
123
127
|
PLUGIN_NAME,
|
|
124
128
|
(compilation, { normalModuleFactory }) => {
|
|
129
|
+
compilation.hooks.assetPath.tap(
|
|
130
|
+
{ name: PLUGIN_NAME, before: "TemplatedPathPlugin" },
|
|
131
|
+
(path, data) => {
|
|
132
|
+
if (data.filename && this.modules[data.filename]) {
|
|
133
|
+
/**
|
|
134
|
+
* @param {string} str path
|
|
135
|
+
* @returns {string} safe path
|
|
136
|
+
*/
|
|
137
|
+
const toSafePath = (str) =>
|
|
138
|
+
`__${str
|
|
139
|
+
.replace(/:/g, "__")
|
|
140
|
+
.replace(/^[^a-z0-9]+|[^a-z0-9]+$/gi, "")
|
|
141
|
+
.replace(/[^a-z0-9._-]+/gi, "_")}`;
|
|
142
|
+
|
|
143
|
+
// filename: virtual:logo.svg -> __virtual__logo.svg
|
|
144
|
+
data.filename = toSafePath(data.filename);
|
|
145
|
+
}
|
|
146
|
+
return path;
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
|
|
125
150
|
normalModuleFactory.hooks.resolveForScheme
|
|
126
151
|
.for(scheme)
|
|
127
152
|
.tap(PLUGIN_NAME, (resourceData) => {
|
|
@@ -135,6 +160,7 @@ class VirtualUrlPlugin {
|
|
|
135
160
|
const type = virtualConfig.type;
|
|
136
161
|
resourceData.path = path + type;
|
|
137
162
|
resourceData.resource = path;
|
|
163
|
+
resourceData.context = compiler.context;
|
|
138
164
|
|
|
139
165
|
if (virtualConfig.version) {
|
|
140
166
|
const cacheKey = toCacheKey(resourceData.resource, scheme);
|
|
@@ -313,6 +313,7 @@ class BinaryMiddleware extends SerializerMiddleware {
|
|
|
313
313
|
/** @type {number[]} */
|
|
314
314
|
const lengths = [];
|
|
315
315
|
for (const item of serializedData) {
|
|
316
|
+
/** @type {undefined | number} */
|
|
316
317
|
let last;
|
|
317
318
|
if (typeof item === "function") {
|
|
318
319
|
lengths.push(0);
|
|
@@ -490,8 +491,10 @@ class BinaryMiddleware extends SerializerMiddleware {
|
|
|
490
491
|
}
|
|
491
492
|
case "boolean": {
|
|
492
493
|
let lastByte = thing === true ? 1 : 0;
|
|
494
|
+
/** @type {number[]} */
|
|
493
495
|
const bytes = [];
|
|
494
496
|
let count = 1;
|
|
497
|
+
/** @type {undefined | number} */
|
|
495
498
|
let n;
|
|
496
499
|
for (n = 1; n < 0xffffffff && i + n < data.length; n++) {
|
|
497
500
|
const item = data[i + n];
|
|
@@ -541,6 +544,7 @@ class BinaryMiddleware extends SerializerMiddleware {
|
|
|
541
544
|
}
|
|
542
545
|
case "object": {
|
|
543
546
|
if (thing === null) {
|
|
547
|
+
/** @type {number} */
|
|
544
548
|
let n;
|
|
545
549
|
for (n = 1; n < 0x100000104 && i + n < data.length; n++) {
|
|
546
550
|
const item = data[i + n];
|
|
@@ -21,7 +21,7 @@ const { dirname, join, mkdirp } = require("../util/fs");
|
|
|
21
21
|
const memoize = require("../util/memoize");
|
|
22
22
|
const SerializerMiddleware = require("./SerializerMiddleware");
|
|
23
23
|
|
|
24
|
-
/** @typedef {
|
|
24
|
+
/** @typedef {import("../util/Hash").HashFunction} HashFunction */
|
|
25
25
|
/** @typedef {import("../util/fs").IStats} IStats */
|
|
26
26
|
/** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */
|
|
27
27
|
/** @typedef {import("./types").BufferSerializableType} BufferSerializableType */
|
|
@@ -49,7 +49,7 @@ const WRITE_LIMIT_CHUNK = 511 * 1024 * 1024;
|
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
51
|
* @param {Buffer[]} buffers buffers
|
|
52
|
-
* @param {
|
|
52
|
+
* @param {HashFunction} hashFunction hash function to use
|
|
53
53
|
* @returns {string} hash
|
|
54
54
|
*/
|
|
55
55
|
const hashForName = (buffers, hashFunction) => {
|
|
@@ -101,7 +101,7 @@ const readUInt64LE = Buffer.prototype.readBigUInt64LE
|
|
|
101
101
|
* @param {(BufferSerializableType | LazyFunction)[]} data data to be serialized
|
|
102
102
|
* @param {string | boolean} name file base name
|
|
103
103
|
* @param {(name: string | false, buffers: Buffer[], size: number) => Promise<void>} writeFile writes a file
|
|
104
|
-
* @param {
|
|
104
|
+
* @param {HashFunction=} hashFunction hash function to use
|
|
105
105
|
* @returns {Promise<SerializeResult>} resulting file pointer and promise
|
|
106
106
|
*/
|
|
107
107
|
const serialize = async (
|
|
@@ -336,6 +336,7 @@ const deserialize = async (middleware, name, readFile) => {
|
|
|
336
336
|
throw new Error("Invalid file version");
|
|
337
337
|
}
|
|
338
338
|
const sectionCount = readUInt32LE();
|
|
339
|
+
/** @type {number[]} */
|
|
339
340
|
const lengths = [];
|
|
340
341
|
let lastLengthPositive = false;
|
|
341
342
|
for (let i = 0; i < sectionCount; i++) {
|
|
@@ -433,11 +434,13 @@ const deserialize = async (middleware, name, readFile) => {
|
|
|
433
434
|
class FileMiddleware extends SerializerMiddleware {
|
|
434
435
|
/**
|
|
435
436
|
* @param {IntermediateFileSystem} fs filesystem
|
|
436
|
-
* @param {
|
|
437
|
+
* @param {HashFunction} hashFunction hash function to use
|
|
437
438
|
*/
|
|
438
439
|
constructor(fs, hashFunction = DEFAULTS.HASH_FUNCTION) {
|
|
439
440
|
super();
|
|
441
|
+
/** @type {IntermediateFileSystem} */
|
|
440
442
|
this.fs = fs;
|
|
443
|
+
/** @type {HashFunction} */
|
|
441
444
|
this._hashFunction = hashFunction;
|
|
442
445
|
}
|
|
443
446
|
|
|
@@ -454,6 +457,7 @@ class FileMiddleware extends SerializerMiddleware {
|
|
|
454
457
|
|
|
455
458
|
// It's important that we don't touch existing files during serialization
|
|
456
459
|
// because serialize may read existing files (when deserializing)
|
|
460
|
+
/** @type {Set<string>} */
|
|
457
461
|
const allWrittenFiles = new Set();
|
|
458
462
|
/**
|
|
459
463
|
* @param {string | false} name name
|
|
@@ -472,6 +476,7 @@ class FileMiddleware extends SerializerMiddleware {
|
|
|
472
476
|
*/
|
|
473
477
|
(resolve, reject) => {
|
|
474
478
|
let stream = this.fs.createWriteStream(`${file}_`);
|
|
479
|
+
/** @type {undefined | import("zlib").Gzip | import("zlib").BrotliCompress} */
|
|
475
480
|
let compression;
|
|
476
481
|
if (file.endsWith(".gz")) {
|
|
477
482
|
compression = createGzip({
|
|
@@ -18,7 +18,7 @@ const SerializerMiddleware = require("./SerializerMiddleware");
|
|
|
18
18
|
const SetObjectSerializer = require("./SetObjectSerializer");
|
|
19
19
|
|
|
20
20
|
/** @typedef {import("../logging/Logger").Logger} Logger */
|
|
21
|
-
/** @typedef {
|
|
21
|
+
/** @typedef {import("../util/Hash").HashFunction} HashFunction */
|
|
22
22
|
/** @typedef {import("./SerializerMiddleware").LazyOptions} LazyOptions */
|
|
23
23
|
/** @typedef {import("./types").ComplexSerializableType} ComplexSerializableType */
|
|
24
24
|
/** @typedef {import("./types").PrimitiveSerializableType} PrimitiveSerializableType */
|
|
@@ -111,7 +111,7 @@ const setMapSize = (map, size) => {
|
|
|
111
111
|
|
|
112
112
|
/**
|
|
113
113
|
* @param {Buffer} buffer buffer
|
|
114
|
-
* @param {
|
|
114
|
+
* @param {HashFunction} hashFunction hash function to use
|
|
115
115
|
* @returns {string} hash
|
|
116
116
|
*/
|
|
117
117
|
const toHash = (buffer, hashFunction) => {
|
|
@@ -207,17 +207,21 @@ const loaders = new Map();
|
|
|
207
207
|
/** @typedef {PrimitiveSerializableType[]} SerializedType */
|
|
208
208
|
/** @typedef {{ logger: Logger }} Context */
|
|
209
209
|
|
|
210
|
+
/** @typedef {(context: ObjectSerializerContext | ObjectDeserializerContext) => void} ExtendContext */
|
|
211
|
+
|
|
210
212
|
/**
|
|
211
213
|
* @extends {SerializerMiddleware<DeserializedType, SerializedType, Context>}
|
|
212
214
|
*/
|
|
213
215
|
class ObjectMiddleware extends SerializerMiddleware {
|
|
214
216
|
/**
|
|
215
|
-
* @param {
|
|
216
|
-
* @param {
|
|
217
|
+
* @param {ExtendContext} extendContext context extensions
|
|
218
|
+
* @param {HashFunction} hashFunction hash function to use
|
|
217
219
|
*/
|
|
218
220
|
constructor(extendContext, hashFunction = DEFAULTS.HASH_FUNCTION) {
|
|
219
221
|
super();
|
|
222
|
+
/** @type {ExtendContext} */
|
|
220
223
|
this.extendContext = extendContext;
|
|
224
|
+
/** @type {HashFunction} */
|
|
221
225
|
this._hashFunction = hashFunction;
|
|
222
226
|
}
|
|
223
227
|
|
|
@@ -276,11 +280,12 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
|
276
280
|
}
|
|
277
281
|
|
|
278
282
|
/**
|
|
279
|
-
* @param {
|
|
283
|
+
* @param {EXPECTED_ANY} object for serialization
|
|
280
284
|
* @returns {SerializerConfigWithSerializer} Serializer config
|
|
281
285
|
*/
|
|
282
286
|
static getSerializerFor(object) {
|
|
283
287
|
const proto = Object.getPrototypeOf(object);
|
|
288
|
+
/** @type {null | Constructor} */
|
|
284
289
|
let c;
|
|
285
290
|
if (proto === null) {
|
|
286
291
|
// Object created with Object.create(null)
|
|
@@ -295,7 +300,11 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
|
295
300
|
}
|
|
296
301
|
const config = serializers.get(c);
|
|
297
302
|
|
|
298
|
-
if (!config)
|
|
303
|
+
if (!config) {
|
|
304
|
+
throw new Error(
|
|
305
|
+
`No serializer registered for ${/** @type {Constructor} */ (c).name}`
|
|
306
|
+
);
|
|
307
|
+
}
|
|
299
308
|
if (config === NOT_SERIALIZABLE) throw NOT_SERIALIZABLE;
|
|
300
309
|
|
|
301
310
|
return /** @type {SerializerConfigWithSerializer} */ (config);
|
|
@@ -345,6 +354,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
|
345
354
|
const addReferenceable = (item) => {
|
|
346
355
|
referenceable.set(item, currentPos++);
|
|
347
356
|
};
|
|
357
|
+
/** @type {Map<number, Buffer | [Buffer, Buffer] | Map<string, Buffer>>} */
|
|
348
358
|
let bufferDedupeMap = new Map();
|
|
349
359
|
/**
|
|
350
360
|
* @param {Buffer} buf buffer
|
|
@@ -366,6 +376,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
|
366
376
|
return buf;
|
|
367
377
|
}
|
|
368
378
|
const hash = toHash(entry, this._hashFunction);
|
|
379
|
+
/** @type {Map<string, Buffer>} */
|
|
369
380
|
const newMap = new Map();
|
|
370
381
|
newMap.set(hash, entry);
|
|
371
382
|
bufferDedupeMap.set(len, newMap);
|
|
@@ -384,8 +395,10 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
|
384
395
|
entry.push(buf);
|
|
385
396
|
return buf;
|
|
386
397
|
}
|
|
398
|
+
/** @type {Map<string, Buffer>} */
|
|
387
399
|
const newMap = new Map();
|
|
388
400
|
const hash = toHash(buf, this._hashFunction);
|
|
401
|
+
/** @type {undefined | Buffer} */
|
|
389
402
|
let found;
|
|
390
403
|
for (const item of entry) {
|
|
391
404
|
const itemHash = toHash(item, this._hashFunction);
|
|
@@ -408,7 +421,9 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
|
408
421
|
return buf;
|
|
409
422
|
};
|
|
410
423
|
let currentPosTypeLookup = 0;
|
|
424
|
+
/** @type {Map<ComplexSerializableType, number>} */
|
|
411
425
|
let objectTypeLookup = new Map();
|
|
426
|
+
/** @type {Set<ComplexSerializableType>} */
|
|
412
427
|
const cycleStack = new Set();
|
|
413
428
|
/**
|
|
414
429
|
* @param {ComplexSerializableType} item item to stack
|
|
@@ -441,12 +456,18 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
|
441
456
|
if (item.constructor === Object) {
|
|
442
457
|
return `Object { ${Object.keys(item).join(", ")} }`;
|
|
443
458
|
}
|
|
444
|
-
if (item.constructor === Map)
|
|
459
|
+
if (item.constructor === Map) {
|
|
460
|
+
return `Map { ${/** @type {Map<EXPECTED_ANY, EXPECTED_ANY>} */ (item).size} items }`;
|
|
461
|
+
}
|
|
445
462
|
if (item.constructor === Array) {
|
|
446
|
-
return `Array { ${item.length} items }`;
|
|
463
|
+
return `Array { ${/** @type {EXPECTED_ANY[]} */ (item).length} items }`;
|
|
464
|
+
}
|
|
465
|
+
if (item.constructor === Set) {
|
|
466
|
+
return `Set { ${/** @type {Set<EXPECTED_ANY>} */ (item).size} items }`;
|
|
467
|
+
}
|
|
468
|
+
if (item.constructor === RegExp) {
|
|
469
|
+
return /** @type {RegExp} */ (item).toString();
|
|
447
470
|
}
|
|
448
|
-
if (item.constructor === Set) return `Set { ${item.size} items }`;
|
|
449
|
-
if (item.constructor === RegExp) return item.toString();
|
|
450
471
|
return `${item.constructor.name}`;
|
|
451
472
|
}
|
|
452
473
|
return `Object [null prototype] { ${Object.keys(item).join(
|
|
@@ -464,7 +485,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
|
464
485
|
})
|
|
465
486
|
.join(" -> ");
|
|
466
487
|
};
|
|
467
|
-
/** @type {WeakSet<Error>} */
|
|
488
|
+
/** @type {undefined | WeakSet<Error>} */
|
|
468
489
|
let hasDebugInfoAttached;
|
|
469
490
|
/** @type {ObjectSerializerContext} */
|
|
470
491
|
let ctx = {
|
|
@@ -723,6 +744,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
|
723
744
|
);
|
|
724
745
|
} else {
|
|
725
746
|
const request = nextItem;
|
|
747
|
+
/** @type {undefined | ObjectSerializer} */
|
|
726
748
|
let serializer;
|
|
727
749
|
|
|
728
750
|
if (typeof request === "number") {
|
|
@@ -788,6 +810,7 @@ class ObjectMiddleware extends SerializerMiddleware {
|
|
|
788
810
|
// As this is only for error handling, we omit creating a Map for
|
|
789
811
|
// faster access to this information, as this would affect performance
|
|
790
812
|
// in the good case
|
|
813
|
+
/** @type {undefined | [Constructor | null, SerializerConfig]} */
|
|
791
814
|
let serializerEntry;
|
|
792
815
|
for (const entry of serializers) {
|
|
793
816
|
if (entry[1].serializer === serializer) {
|