From 792ee7e516d659c2dd4e30bbd9322337e7b022fd Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Fri, 26 May 2023 20:21:35 +0300 Subject: [PATCH] refactor(types): more --- lib/ContextModuleFactory.js | 2 +- lib/DllModuleFactory.js | 2 +- lib/HookWebpackError.js | 2 +- lib/IgnoreErrorModuleFactory.js | 2 +- lib/Module.js | 30 +++- lib/ModuleFactory.js | 2 +- lib/NormalModuleFactory.js | 2 +- lib/NullFactory.js | 2 +- lib/container/ContainerEntryModule.js | 4 + lib/container/ContainerEntryModuleFactory.js | 2 +- lib/container/FallbackDependency.js | 7 + lib/container/FallbackItemDependency.js | 3 + lib/container/FallbackModule.js | 4 + lib/container/FallbackModuleFactory.js | 2 +- lib/container/RemoteModule.js | 4 + lib/container/RemoteRuntimeModule.js | 1 + lib/container/RemoteToExternalDependency.js | 3 + lib/esm/ModuleChunkFormatPlugin.js | 16 +- lib/esm/ModuleChunkLoadingPlugin.js | 9 + lib/esm/ModuleChunkLoadingRuntimeModule.js | 18 +- lib/hmr/LazyCompilationPlugin.js | 2 +- lib/ids/ChunkModuleIdRangePlugin.js | 11 ++ lib/ids/DeterministicChunkIdsPlugin.js | 13 +- lib/ids/DeterministicModuleIdsPlugin.js | 18 +- lib/ids/HashedModuleIdsPlugin.js | 9 +- lib/ids/IdHelpers.js | 6 + lib/ids/NamedChunkIdsPlugin.js | 14 +- lib/ids/NamedModuleIdsPlugin.js | 17 +- lib/ids/OccurrenceChunkIdsPlugin.js | 8 +- lib/ids/SyncModuleIdsPlugin.js | 4 +- lib/javascript/ChunkHelpers.js | 4 +- lib/javascript/JavascriptModulesPlugin.js | 4 +- lib/sharing/ProvideSharedModuleFactory.js | 2 +- lib/util/createHash.js | 2 +- types.d.ts | 178 +++++++++++-------- 35 files changed, 282 insertions(+), 127 deletions(-) diff --git a/lib/ContextModuleFactory.js b/lib/ContextModuleFactory.js index 6acab513d..bf635a0e7 100644 --- a/lib/ContextModuleFactory.js +++ b/lib/ContextModuleFactory.js @@ -80,7 +80,7 @@ module.exports = class ContextModuleFactory extends ModuleFactory { /** * @param {ModuleFactoryCreateData} data data object - * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback * @returns {void} */ create(data, callback) { diff --git a/lib/DllModuleFactory.js b/lib/DllModuleFactory.js index dc59d517a..fa8adddeb 100644 --- a/lib/DllModuleFactory.js +++ b/lib/DllModuleFactory.js @@ -19,7 +19,7 @@ class DllModuleFactory extends ModuleFactory { } /** * @param {ModuleFactoryCreateData} data data object - * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback * @returns {void} */ create(data, callback) { diff --git a/lib/HookWebpackError.js b/lib/HookWebpackError.js index dfb5e9358..b3a38292f 100644 --- a/lib/HookWebpackError.js +++ b/lib/HookWebpackError.js @@ -85,7 +85,7 @@ const tryRunOrWebpackError = (fn, hook) => { if (err instanceof WebpackError) { throw err; } - throw new HookWebpackError(err, hook); + throw new HookWebpackError(/** @type {Error} */ (err), hook); } return r; }; diff --git a/lib/IgnoreErrorModuleFactory.js b/lib/IgnoreErrorModuleFactory.js index ceae85f62..e6d9a51dd 100644 --- a/lib/IgnoreErrorModuleFactory.js +++ b/lib/IgnoreErrorModuleFactory.js @@ -26,7 +26,7 @@ class IgnoreErrorModuleFactory extends ModuleFactory { /** * @param {ModuleFactoryCreateData} data data object - * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback * @returns {void} */ create(data, callback) { diff --git a/lib/Module.js b/lib/Module.js index bce5f34fa..9edbd31d4 100644 --- a/lib/Module.js +++ b/lib/Module.js @@ -130,9 +130,9 @@ const deprecatedNeedRebuild = util.deprecate( class Module extends DependenciesBlock { /** - * @param {ModuleTypes} type the module type, when deserializing the type is not known and is an empty string - * @param {string=} context an optional context - * @param {string=} layer an optional layer in which the module is + * @param {ModuleTypes | ""} type the module type, when deserializing the type is not known and is an empty string + * @param {(string | null)=} context an optional context + * @param {(string | null)=} layer an optional layer in which the module is */ constructor(type, context = null, layer = null) { super(); @@ -151,7 +151,7 @@ class Module extends DependenciesBlock { this.debugId = debugId++; // Info from Factory - /** @type {ResolveOptions} */ + /** @type {ResolveOptions | undefined} */ this.resolveOptions = EMPTY_RESOLVE_OPTIONS; /** @type {object | undefined} */ this.factoryMeta = undefined; @@ -167,9 +167,9 @@ class Module extends DependenciesBlock { this._warnings = undefined; /** @type {WebpackError[] | undefined} */ this._errors = undefined; - /** @type {BuildMeta} */ + /** @type {BuildMeta | undefined} */ this.buildMeta = undefined; - /** @type {Record} */ + /** @type {Record | undefined} */ this.buildInfo = undefined; /** @type {Dependency[] | undefined} */ this.presentationalDependencies = undefined; @@ -331,6 +331,10 @@ class Module extends DependenciesBlock { ); } + /** + * @param {Chunk} chunk the chunk + * @returns {boolean} true, when the module was added + */ addChunk(chunk) { const chunkGraph = ChunkGraph.getChunkGraphForModule( this, @@ -342,6 +346,10 @@ class Module extends DependenciesBlock { return true; } + /** + * @param {Chunk} chunk the chunk + * @returns {void} + */ removeChunk(chunk) { return ChunkGraph.getChunkGraphForModule( this, @@ -350,6 +358,10 @@ class Module extends DependenciesBlock { ).disconnectChunkAndModule(chunk, this); } + /** + * @param {Chunk} chunk the chunk + * @returns {boolean} true, when the module is in the chunk + */ isInChunk(chunk) { return ChunkGraph.getChunkGraphForModule( this, @@ -435,7 +447,7 @@ class Module extends DependenciesBlock { case "namespace": return "namespace"; case "default": - switch (this.buildMeta.defaultObject) { + switch (/** @type {BuildMeta} */ (this.buildMeta).defaultObject) { case "redirect": return "default-with-named"; case "redirect-warn": @@ -447,7 +459,7 @@ class Module extends DependenciesBlock { if (strict) return "default-with-named"; // Try to figure out value of __esModule by following reexports const handleDefault = () => { - switch (this.buildMeta.defaultObject) { + switch (/** @type {BuildMeta} */ (this.buildMeta).defaultObject) { case "redirect": case "redirect-warn": return "default-with-named"; @@ -664,7 +676,7 @@ class Module extends DependenciesBlock { ] of moduleGraph.getIncomingConnectionsByOriginModule(this)) { if (!connections.some(c => c.isTargetActive(chunk.runtime))) continue; for (const originChunk of chunkGraph.getModuleChunksIterable( - fromModule + /** @type {Module} */ (fromModule) )) { // return true if module this is not reachable from originChunk when ignoring chunk if (!this.isAccessibleInChunk(chunkGraph, originChunk, chunk)) diff --git a/lib/ModuleFactory.js b/lib/ModuleFactory.js index 0cc084c06..1d889c818 100644 --- a/lib/ModuleFactory.js +++ b/lib/ModuleFactory.js @@ -38,7 +38,7 @@ class ModuleFactory { /** * @abstract * @param {ModuleFactoryCreateData} data data object - * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback * @returns {void} */ create(data, callback) { diff --git a/lib/NormalModuleFactory.js b/lib/NormalModuleFactory.js index b04dd55b5..a0d8b767f 100644 --- a/lib/NormalModuleFactory.js +++ b/lib/NormalModuleFactory.js @@ -753,7 +753,7 @@ class NormalModuleFactory extends ModuleFactory { /** * @param {ModuleFactoryCreateData} data data object - * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback * @returns {void} */ create(data, callback) { diff --git a/lib/NullFactory.js b/lib/NullFactory.js index be86ccf85..50f3471be 100644 --- a/lib/NullFactory.js +++ b/lib/NullFactory.js @@ -13,7 +13,7 @@ const ModuleFactory = require("./ModuleFactory"); class NullFactory extends ModuleFactory { /** * @param {ModuleFactoryCreateData} data data object - * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback * @returns {void} */ create(data, callback) { diff --git a/lib/container/ContainerEntryModule.js b/lib/container/ContainerEntryModule.js index ae251eb76..af8055030 100644 --- a/lib/container/ContainerEntryModule.js +++ b/lib/container/ContainerEntryModule.js @@ -273,6 +273,10 @@ class ContainerEntryModule extends Module { super.serialize(context); } + /** + * @param {ObjectDeserializerContext} context context + * @returns {ContainerEntryModule} deserialized container entry module + */ static deserialize(context) { const { read } = context; const obj = new ContainerEntryModule(read(), read(), read()); diff --git a/lib/container/ContainerEntryModuleFactory.js b/lib/container/ContainerEntryModuleFactory.js index 1a1a7af89..4febfebe0 100644 --- a/lib/container/ContainerEntryModuleFactory.js +++ b/lib/container/ContainerEntryModuleFactory.js @@ -15,7 +15,7 @@ const ContainerEntryModule = require("./ContainerEntryModule"); module.exports = class ContainerEntryModuleFactory extends ModuleFactory { /** * @param {ModuleFactoryCreateData} data data object - * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback * @returns {void} */ create({ dependencies: [dependency] }, callback) { diff --git a/lib/container/FallbackDependency.js b/lib/container/FallbackDependency.js index 60d1fdd59..088720f5e 100644 --- a/lib/container/FallbackDependency.js +++ b/lib/container/FallbackDependency.js @@ -12,6 +12,9 @@ const makeSerializable = require("../util/makeSerializable"); /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ class FallbackDependency extends Dependency { + /** + * @param {string[]} requests requests + */ constructor(requests) { super(); this.requests = requests; @@ -41,6 +44,10 @@ class FallbackDependency extends Dependency { super.serialize(context); } + /** + * @param {ObjectDeserializerContext} context context + * @returns {FallbackDependency} deserialize fallback dependency + */ static deserialize(context) { const { read } = context; const obj = new FallbackDependency(read()); diff --git a/lib/container/FallbackItemDependency.js b/lib/container/FallbackItemDependency.js index 3995a9e79..f09f8cf8c 100644 --- a/lib/container/FallbackItemDependency.js +++ b/lib/container/FallbackItemDependency.js @@ -9,6 +9,9 @@ const ModuleDependency = require("../dependencies/ModuleDependency"); const makeSerializable = require("../util/makeSerializable"); class FallbackItemDependency extends ModuleDependency { + /** + * @param {string} request request + */ constructor(request) { super(request); } diff --git a/lib/container/FallbackModule.js b/lib/container/FallbackModule.js index 3c8b692fe..b2ed81b07 100644 --- a/lib/container/FallbackModule.js +++ b/lib/container/FallbackModule.js @@ -166,6 +166,10 @@ class FallbackModule extends Module { super.serialize(context); } + /** + * @param {ObjectDeserializerContext} context context + * @returns {FallbackModule} deserialized fallback module + */ static deserialize(context) { const { read } = context; const obj = new FallbackModule(read()); diff --git a/lib/container/FallbackModuleFactory.js b/lib/container/FallbackModuleFactory.js index 350e910b9..6a9eaeca0 100644 --- a/lib/container/FallbackModuleFactory.js +++ b/lib/container/FallbackModuleFactory.js @@ -15,7 +15,7 @@ const FallbackModule = require("./FallbackModule"); module.exports = class FallbackModuleFactory extends ModuleFactory { /** * @param {ModuleFactoryCreateData} data data object - * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback * @returns {void} */ create({ dependencies: [dependency] }, callback) { diff --git a/lib/container/RemoteModule.js b/lib/container/RemoteModule.js index d59a0fadb..ade329b99 100644 --- a/lib/container/RemoteModule.js +++ b/lib/container/RemoteModule.js @@ -164,6 +164,10 @@ class RemoteModule extends Module { super.serialize(context); } + /** + * @param {ObjectDeserializerContext} context context + * @returns {RemoteModule} deserialized module + */ static deserialize(context) { const { read } = context; const obj = new RemoteModule(read(), read(), read(), read()); diff --git a/lib/container/RemoteRuntimeModule.js b/lib/container/RemoteRuntimeModule.js index 48d9d9d96..d23a9343c 100644 --- a/lib/container/RemoteRuntimeModule.js +++ b/lib/container/RemoteRuntimeModule.js @@ -9,6 +9,7 @@ const RuntimeGlobals = require("../RuntimeGlobals"); const RuntimeModule = require("../RuntimeModule"); const Template = require("../Template"); +/** @typedef {import("../Chunk")} Chunk */ /** @typedef {import("./RemoteModule")} RemoteModule */ class RemoteRuntimeModule extends RuntimeModule { diff --git a/lib/container/RemoteToExternalDependency.js b/lib/container/RemoteToExternalDependency.js index 28a52f771..df7fd1f81 100644 --- a/lib/container/RemoteToExternalDependency.js +++ b/lib/container/RemoteToExternalDependency.js @@ -9,6 +9,9 @@ const ModuleDependency = require("../dependencies/ModuleDependency"); const makeSerializable = require("../util/makeSerializable"); class RemoteToExternalDependency extends ModuleDependency { + /** + * @param {string} request request + */ constructor(request) { super(request); } diff --git a/lib/esm/ModuleChunkFormatPlugin.js b/lib/esm/ModuleChunkFormatPlugin.js index 84415a112..6f00ae75f 100644 --- a/lib/esm/ModuleChunkFormatPlugin.js +++ b/lib/esm/ModuleChunkFormatPlugin.js @@ -17,7 +17,9 @@ const { } = require("../javascript/JavascriptModulesPlugin"); const { updateHashForEntryStartup } = require("../javascript/StartupHelpers"); +/** @typedef {import("../Chunk")} Chunk */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../Entrypoint")} Entrypoint */ class ModuleChunkFormatPlugin { /** @@ -73,7 +75,9 @@ class ModuleChunkFormatPlugin { chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk) ); if (entries.length > 0) { - const runtimeChunk = entries[0][1].getRuntimeChunk(); + const runtimeChunk = + /** @type {Entrypoint[][]} */ + (entries)[0][1].getRuntimeChunk(); const currentOutputName = compilation .getPath( getChunkFilenameTemplate(chunk, compilation.outputOptions), @@ -87,6 +91,10 @@ class ModuleChunkFormatPlugin { // remove filename, we only need the directory currentOutputName.pop(); + /** + * @param {Chunk} chunk the chunk + * @returns {string} the relative path + */ const getRelativePath = chunk => { const baseOutputName = currentOutputName.slice(); const chunkOutputName = compilation @@ -124,7 +132,7 @@ class ModuleChunkFormatPlugin { entrySource.add(";\n\n// load runtime\n"); entrySource.add( `import ${RuntimeGlobals.require} from ${JSON.stringify( - getRelativePath(runtimeChunk) + getRelativePath(/** @type {Chunk} */ (runtimeChunk)) )};\n` ); @@ -143,8 +151,8 @@ class ModuleChunkFormatPlugin { const final = i + 1 === entries.length; const moduleId = chunkGraph.getModuleId(module); const chunks = getAllChunks( - entrypoint, - runtimeChunk, + /** @type {Entrypoint} */ (entrypoint), + /** @type {Chunk} */ (runtimeChunk), undefined ); for (const chunk of chunks) { diff --git a/lib/esm/ModuleChunkLoadingPlugin.js b/lib/esm/ModuleChunkLoadingPlugin.js index 5c984a596..b88533a83 100644 --- a/lib/esm/ModuleChunkLoadingPlugin.js +++ b/lib/esm/ModuleChunkLoadingPlugin.js @@ -9,6 +9,7 @@ const RuntimeGlobals = require("../RuntimeGlobals"); const ExportWebpackRequireRuntimeModule = require("./ExportWebpackRequireRuntimeModule"); const ModuleChunkLoadingRuntimeModule = require("./ModuleChunkLoadingRuntimeModule"); +/** @typedef {import("../Chunk")} Chunk */ /** @typedef {import("../Compiler")} Compiler */ class ModuleChunkLoadingPlugin { @@ -22,6 +23,10 @@ class ModuleChunkLoadingPlugin { "ModuleChunkLoadingPlugin", compilation => { const globalChunkLoading = compilation.outputOptions.chunkLoading; + /** + * @param {Chunk} chunk chunk to check + * @returns {boolean} true, when the plugin is enabled for the chunk + */ const isEnabledForChunk = chunk => { const options = chunk.getEntryOptions(); const chunkLoading = @@ -31,6 +36,10 @@ class ModuleChunkLoadingPlugin { return chunkLoading === "import"; }; const onceForChunkSet = new WeakSet(); + /** + * @param {Chunk} chunk chunk to check + * @param {Set} set runtime requirements + */ const handler = (chunk, set) => { if (onceForChunkSet.has(chunk)) return; onceForChunkSet.add(chunk); diff --git a/lib/esm/ModuleChunkLoadingRuntimeModule.js b/lib/esm/ModuleChunkLoadingRuntimeModule.js index b4c957ded..1999e1155 100644 --- a/lib/esm/ModuleChunkLoadingRuntimeModule.js +++ b/lib/esm/ModuleChunkLoadingRuntimeModule.js @@ -18,6 +18,7 @@ const compileBooleanMatcher = require("../util/compileBooleanMatcher"); const { getUndoPath } = require("../util/identifier"); /** @typedef {import("../Chunk")} Chunk */ +/** @typedef {import("../ChunkGraph")} ChunkGraph */ /** * @typedef {Object} JsonpCompilationPluginHooks @@ -69,11 +70,10 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule { if (options && options.baseUri) { return `${RuntimeGlobals.baseURI} = ${JSON.stringify(options.baseUri)};`; } + const compilation = /** @type {Compilation} */ (this.compilation); const { - compilation: { - outputOptions: { importMetaName } - } - } = this; + outputOptions: { importMetaName } + } = compilation; return `${RuntimeGlobals.baseURI} = new URL(${JSON.stringify( rootOutputDir )}, ${importMetaName}.url);`; @@ -83,7 +83,9 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule { * @returns {string} runtime code */ generate() { - const { compilation, chunk, chunkGraph } = this; + const compilation = /** @type {Compilation} */ (this.compilation); + const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph); + const chunk = /** @type {Chunk} */ (this.chunk); const { runtimeTemplate, outputOptions: { importFunctionName } @@ -106,8 +108,8 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule { const hasJsMatcher = compileBooleanMatcher(conditionMap); const initialChunkIds = getInitialChunkIds(chunk, chunkGraph, chunkHasJs); - const outputName = this.compilation.getPath( - getChunkFilenameTemplate(chunk, this.compilation.outputOptions), + const outputName = compilation.getPath( + getChunkFilenameTemplate(chunk, compilation.outputOptions), { chunk, contentHashType: "javascript" @@ -115,7 +117,7 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule { ); const rootOutputDir = getUndoPath( outputName, - /** @type {string} */ (this.compilation.outputOptions.path), + /** @type {string} */ (compilation.outputOptions.path), true ); diff --git a/lib/hmr/LazyCompilationPlugin.js b/lib/hmr/LazyCompilationPlugin.js index 84a28b727..7789a37b9 100644 --- a/lib/hmr/LazyCompilationPlugin.js +++ b/lib/hmr/LazyCompilationPlugin.js @@ -303,7 +303,7 @@ class LazyCompilationDependencyFactory extends ModuleFactory { /** * @param {ModuleFactoryCreateData} data data object - * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback * @returns {void} */ create(data, callback) { diff --git a/lib/ids/ChunkModuleIdRangePlugin.js b/lib/ids/ChunkModuleIdRangePlugin.js index 4040edc55..982ca1b1e 100644 --- a/lib/ids/ChunkModuleIdRangePlugin.js +++ b/lib/ids/ChunkModuleIdRangePlugin.js @@ -13,7 +13,18 @@ const { /** @typedef {import("../Compiler")} Compiler */ +/** + * @typedef {Object} ChunkModuleIdRangePluginOptions + * @property {string} name the chunk name + * @property {("index" | "index2" | "preOrderIndex" | "postOrderIndex")=} order order + * @property {number=} start start id + * @property {number=} end end id + */ + class ChunkModuleIdRangePlugin { + /** + * @param {ChunkModuleIdRangePluginOptions} options options object + */ constructor(options) { this.options = options; } diff --git a/lib/ids/DeterministicChunkIdsPlugin.js b/lib/ids/DeterministicChunkIdsPlugin.js index d0788fff4..4547400c4 100644 --- a/lib/ids/DeterministicChunkIdsPlugin.js +++ b/lib/ids/DeterministicChunkIdsPlugin.js @@ -15,9 +15,18 @@ const { /** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Module")} Module */ +/** + * @typedef {Object} DeterministicChunkIdsPluginOptions + * @property {string=} context context for ids + * @property {number=} maxLength maximum length of ids + */ + class DeterministicChunkIdsPlugin { - constructor(options) { - this.options = options || {}; + /** + * @param {DeterministicChunkIdsPluginOptions} [options] options + */ + constructor(options = {}) { + this.options = options; } /** diff --git a/lib/ids/DeterministicModuleIdsPlugin.js b/lib/ids/DeterministicModuleIdsPlugin.js index ee4f72cd8..6f19f66f2 100644 --- a/lib/ids/DeterministicModuleIdsPlugin.js +++ b/lib/ids/DeterministicModuleIdsPlugin.js @@ -17,15 +17,19 @@ const { /** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Module")} Module */ +/** + * @typedef {Object} DeterministicModuleIdsPluginOptions + * @property {string=} context context relative to which module identifiers are computed + * @property {function(Module): boolean=} test selector function for modules + * @property {number=} maxLength maximum id length in digits (used as starting point) + * @property {number=} salt hash salt for ids + * @property {boolean=} fixedLength do not increase the maxLength to find an optimal id space size + * @property {boolean=} failOnConflict throw an error when id conflicts occur (instead of rehashing) + */ + class DeterministicModuleIdsPlugin { /** - * @param {Object} options options - * @param {string=} options.context context relative to which module identifiers are computed - * @param {function(Module): boolean=} options.test selector function for modules - * @param {number=} options.maxLength maximum id length in digits (used as starting point) - * @param {number=} options.salt hash salt for ids - * @param {boolean=} options.fixedLength do not increase the maxLength to find an optimal id space size - * @param {boolean=} options.failOnConflict throw an error when id conflicts occur (instead of rehashing) + * @param {DeterministicModuleIdsPluginOptions} [options] options */ constructor(options = {}) { this.options = options; diff --git a/lib/ids/HashedModuleIdsPlugin.js b/lib/ids/HashedModuleIdsPlugin.js index 4e8ff4225..fb3f530cb 100644 --- a/lib/ids/HashedModuleIdsPlugin.js +++ b/lib/ids/HashedModuleIdsPlugin.js @@ -16,6 +16,7 @@ const { } = require("./IdHelpers"); /** @typedef {import("../../declarations/plugins/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */ +/** @typedef {import("../Compiler")} Compiler */ const validate = createSchemaValidation( require("../../schemas/plugins/HashedModuleIdsPlugin.check.js"), @@ -43,6 +44,11 @@ class HashedModuleIdsPlugin { }; } + /** + * Apply the plugin + * @param {Compiler} compiler the compiler instance + * @returns {void} + */ apply(compiler) { const options = this.options; compiler.hooks.compilation.tap("HashedModuleIdsPlugin", compilation => { @@ -64,7 +70,8 @@ class HashedModuleIdsPlugin { hash.digest(options.hashDigest) ); let len = options.hashDigestLength; - while (usedIds.has(hashId.slice(0, len))) len++; + while (usedIds.has(hashId.slice(0, len))) + /** @type {number} */ (len)++; const moduleId = hashId.slice(0, len); chunkGraph.setModuleId(module, moduleId); usedIds.add(moduleId); diff --git a/lib/ids/IdHelpers.js b/lib/ids/IdHelpers.js index 57fa481a9..1ca7824ca 100644 --- a/lib/ids/IdHelpers.js +++ b/lib/ids/IdHelpers.js @@ -427,6 +427,9 @@ const assignAscendingModuleIds = (usedIds, modules, compilation) => { let nextId = 0; let assignId; if (usedIds.size > 0) { + /** + * @param {Module} module the module + */ assignId = module => { if (chunkGraph.getModuleId(module) === null) { while (usedIds.has(nextId + "")) nextId++; @@ -434,6 +437,9 @@ const assignAscendingModuleIds = (usedIds, modules, compilation) => { } }; } else { + /** + * @param {Module} module the module + */ assignId = module => { if (chunkGraph.getModuleId(module) === null) { chunkGraph.setModuleId(module, nextId++); diff --git a/lib/ids/NamedChunkIdsPlugin.js b/lib/ids/NamedChunkIdsPlugin.js index 1b5c8752e..6a6689373 100644 --- a/lib/ids/NamedChunkIdsPlugin.js +++ b/lib/ids/NamedChunkIdsPlugin.js @@ -14,11 +14,21 @@ const { assignAscendingChunkIds } = require("./IdHelpers"); +/** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} Output */ /** @typedef {import("../Chunk")} Chunk */ /** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Module")} Module */ +/** + * @typedef {Object} NamedChunkIdsPluginOptions + * @property {string} [context] context + * @property {string} [delimiter] delimiter + */ + class NamedChunkIdsPlugin { + /** + * @param {NamedChunkIdsPluginOptions=} options options + */ constructor(options) { this.delimiter = (options && options.delimiter) || "-"; this.context = options && options.context; @@ -31,7 +41,9 @@ class NamedChunkIdsPlugin { */ apply(compiler) { compiler.hooks.compilation.tap("NamedChunkIdsPlugin", compilation => { - const { hashFunction } = compilation.outputOptions; + const hashFunction = + /** @type {NonNullable} */ + (compilation.outputOptions.hashFunction); compilation.hooks.chunkIds.tap("NamedChunkIdsPlugin", chunks => { const chunkGraph = compilation.chunkGraph; const context = this.context ? this.context : compiler.context; diff --git a/lib/ids/NamedModuleIdsPlugin.js b/lib/ids/NamedModuleIdsPlugin.js index 97120d95d..7ae3c3d83 100644 --- a/lib/ids/NamedModuleIdsPlugin.js +++ b/lib/ids/NamedModuleIdsPlugin.js @@ -14,12 +14,21 @@ const { assignAscendingModuleIds } = require("./IdHelpers"); +/** @typedef {import("../../declarations/WebpackOptions").OutputNormalized} Output */ /** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Module")} Module */ +/** + * @typedef {Object} NamedModuleIdsPluginOptions + * @property {string} [context] context + */ + class NamedModuleIdsPlugin { - constructor(options) { - this.options = options || {}; + /** + * @param {NamedModuleIdsPluginOptions} [options] options + */ + constructor(options = {}) { + this.options = options; } /** @@ -30,7 +39,9 @@ class NamedModuleIdsPlugin { apply(compiler) { const { root } = compiler; compiler.hooks.compilation.tap("NamedModuleIdsPlugin", compilation => { - const { hashFunction } = compilation.outputOptions; + const hashFunction = + /** @type {NonNullable} */ + (compilation.outputOptions.hashFunction); compilation.hooks.moduleIds.tap("NamedModuleIdsPlugin", () => { const chunkGraph = compilation.chunkGraph; const context = this.options.context diff --git a/lib/ids/OccurrenceChunkIdsPlugin.js b/lib/ids/OccurrenceChunkIdsPlugin.js index 432aa6757..b0acac363 100644 --- a/lib/ids/OccurrenceChunkIdsPlugin.js +++ b/lib/ids/OccurrenceChunkIdsPlugin.js @@ -60,8 +60,12 @@ class OccurrenceChunkIdsPlugin { const chunksInOccurrenceOrder = Array.from(chunks).sort((a, b) => { if (prioritiseInitial) { - const aEntryOccurs = occursInInitialChunksMap.get(a); - const bEntryOccurs = occursInInitialChunksMap.get(b); + const aEntryOccurs = + /** @type {number} */ + (occursInInitialChunksMap.get(a)); + const bEntryOccurs = + /** @type {number} */ + (occursInInitialChunksMap.get(b)); if (aEntryOccurs > bEntryOccurs) return -1; if (aEntryOccurs < bEntryOccurs) return 1; } diff --git a/lib/ids/SyncModuleIdsPlugin.js b/lib/ids/SyncModuleIdsPlugin.js index 8ab5ac7ec..14eab7556 100644 --- a/lib/ids/SyncModuleIdsPlugin.js +++ b/lib/ids/SyncModuleIdsPlugin.js @@ -50,7 +50,7 @@ class SyncModuleIdsPlugin { } return callback(); } - const json = JSON.parse(buffer.toString()); + const json = JSON.parse(/** @type {Buffer} */ (buffer).toString()); data = new Map(); for (const key of Object.keys(json)) { data.set(key, json[key]); @@ -98,7 +98,7 @@ class SyncModuleIdsPlugin { err.module = module; compilation.errors.push(err); } - chunkGraph.setModuleId(module, id); + chunkGraph.setModuleId(module, /** @type {string | number} */ (id)); usedIds.add(idAsString); } }); diff --git a/lib/javascript/ChunkHelpers.js b/lib/javascript/ChunkHelpers.js index 8e0570496..f29d8d7a8 100644 --- a/lib/javascript/ChunkHelpers.js +++ b/lib/javascript/ChunkHelpers.js @@ -11,8 +11,8 @@ const Entrypoint = require("../Entrypoint"); /** * @param {Entrypoint} entrypoint a chunk group - * @param {Chunk} excludedChunk1 current chunk which is excluded - * @param {Chunk} excludedChunk2 runtime chunk which is excluded + * @param {Chunk=} excludedChunk1 current chunk which is excluded + * @param {Chunk=} excludedChunk2 runtime chunk which is excluded * @returns {Set} chunks */ const getAllChunks = (entrypoint, excludedChunk1, excludedChunk2) => { diff --git a/lib/javascript/JavascriptModulesPlugin.js b/lib/javascript/JavascriptModulesPlugin.js index 49ee57f4d..45666a9a0 100644 --- a/lib/javascript/JavascriptModulesPlugin.js +++ b/lib/javascript/JavascriptModulesPlugin.js @@ -130,8 +130,8 @@ const printGeneratedCodeForStack = (module, code) => { * @property {SyncWaterfallHook<[Source, Module, StartupRenderContext]>} renderStartup * @property {SyncWaterfallHook<[string, RenderBootstrapContext]>} renderRequire * @property {SyncBailHook<[Module, RenderBootstrapContext], string>} inlineInRuntimeBailout - * @property {SyncBailHook<[Module, RenderContext], string>} embedInRuntimeBailout - * @property {SyncBailHook<[RenderContext], string>} strictRuntimeBailout + * @property {SyncBailHook<[Module, RenderContext], string | void>} embedInRuntimeBailout + * @property {SyncBailHook<[RenderContext], string | void>} strictRuntimeBailout * @property {SyncHook<[Chunk, Hash, ChunkHashContext]>} chunkHash * @property {SyncBailHook<[Chunk, RenderContext], boolean>} useSourceMap */ diff --git a/lib/sharing/ProvideSharedModuleFactory.js b/lib/sharing/ProvideSharedModuleFactory.js index 2b3b19f8f..d5bcc829f 100644 --- a/lib/sharing/ProvideSharedModuleFactory.js +++ b/lib/sharing/ProvideSharedModuleFactory.js @@ -15,7 +15,7 @@ const ProvideSharedModule = require("./ProvideSharedModule"); class ProvideSharedModuleFactory extends ModuleFactory { /** * @param {ModuleFactoryCreateData} data data object - * @param {function(Error=, ModuleFactoryResult=): void} callback callback + * @param {function((Error | null)=, ModuleFactoryResult=): void} callback callback * @returns {void} */ create(data, callback) { diff --git a/lib/util/createHash.js b/lib/util/createHash.js index 8351e4f27..75087d998 100644 --- a/lib/util/createHash.js +++ b/lib/util/createHash.js @@ -132,7 +132,7 @@ let BatchedHash = undefined; /** * Creates a hash by name or function - * @param {string | typeof Hash} algorithm the algorithm name or a constructor creating a hash + * @param {string | typeof Hash | undefined} algorithm the algorithm name or a constructor creating a hash * @returns {Hash} the hash */ module.exports = algorithm => { diff --git a/types.d.ts b/types.d.ts index 61fe95da8..818dc431d 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1187,14 +1187,35 @@ declare interface ChunkMaps { name: Record; } declare class ChunkModuleIdRangePlugin { - constructor(options?: any); - options: any; + constructor(options: ChunkModuleIdRangePluginOptions); + options: ChunkModuleIdRangePluginOptions; /** * Apply the plugin */ apply(compiler: Compiler): void; } +declare interface ChunkModuleIdRangePluginOptions { + /** + * the chunk name + */ + name: string; + + /** + * order + */ + order?: "index" | "index2" | "preOrderIndex" | "postOrderIndex"; + + /** + * start id + */ + start?: number; + + /** + * end id + */ + end?: number; +} declare interface ChunkModuleMaps { id: Record; hash: Record; @@ -2000,8 +2021,8 @@ declare interface CompilationHooksJavascriptModulesPlugin { [Module, RenderBootstrapContext], string >; - embedInRuntimeBailout: SyncBailHook<[Module, RenderContext], string>; - strictRuntimeBailout: SyncBailHook<[RenderContext], string>; + embedInRuntimeBailout: SyncBailHook<[Module, RenderContext], string | void>; + strictRuntimeBailout: SyncBailHook<[RenderContext], string | void>; chunkHash: SyncHook<[Chunk, Hash, ChunkHashContext]>; useSourceMap: SyncBailHook<[Chunk, RenderContext], boolean>; } @@ -2902,73 +2923,65 @@ declare abstract class DependencyTemplates { clone(): DependencyTemplates; } declare class DeterministicChunkIdsPlugin { - constructor(options?: any); - options: any; + constructor(options?: DeterministicChunkIdsPluginOptions); + options: DeterministicChunkIdsPluginOptions; /** * Apply the plugin */ apply(compiler: Compiler): void; } +declare interface DeterministicChunkIdsPluginOptions { + /** + * context for ids + */ + context?: string; + + /** + * maximum length of ids + */ + maxLength?: number; +} declare class DeterministicModuleIdsPlugin { - constructor(options?: { - /** - * context relative to which module identifiers are computed - */ - context?: string; - /** - * selector function for modules - */ - test?: (arg0: Module) => boolean; - /** - * maximum id length in digits (used as starting point) - */ - maxLength?: number; - /** - * hash salt for ids - */ - salt?: number; - /** - * do not increase the maxLength to find an optimal id space size - */ - fixedLength?: boolean; - /** - * throw an error when id conflicts occur (instead of rehashing) - */ - failOnConflict?: boolean; - }); - options: { - /** - * context relative to which module identifiers are computed - */ - context?: string; - /** - * selector function for modules - */ - test?: (arg0: Module) => boolean; - /** - * maximum id length in digits (used as starting point) - */ - maxLength?: number; - /** - * hash salt for ids - */ - salt?: number; - /** - * do not increase the maxLength to find an optimal id space size - */ - fixedLength?: boolean; - /** - * throw an error when id conflicts occur (instead of rehashing) - */ - failOnConflict?: boolean; - }; + constructor(options?: DeterministicModuleIdsPluginOptions); + options: DeterministicModuleIdsPluginOptions; /** * Apply the plugin */ apply(compiler: Compiler): void; } +declare interface DeterministicModuleIdsPluginOptions { + /** + * context relative to which module identifiers are computed + */ + context?: string; + + /** + * selector function for modules + */ + test?: (arg0: Module) => boolean; + + /** + * maximum id length in digits (used as starting point) + */ + maxLength?: number; + + /** + * hash salt for ids + */ + salt?: number; + + /** + * do not increase the maxLength to find an optimal id space size + */ + fixedLength?: boolean; + + /** + * throw an error when id conflicts occur (instead of rehashing) + */ + failOnConflict?: boolean; +} /** * Options for the webpack-dev-server. @@ -4700,7 +4713,11 @@ declare interface HashableObject { declare class HashedModuleIdsPlugin { constructor(options?: HashedModuleIdsPluginOptions); options: HashedModuleIdsPluginOptions; - apply(compiler?: any): void; + + /** + * Apply the plugin + */ + apply(compiler: Compiler): void; } declare interface HashedModuleIdsPluginOptions { /** @@ -7218,12 +7235,12 @@ declare class Module extends DependenciesBlock { layer: null | string; needId: boolean; debugId: number; - resolveOptions: ResolveOptionsWebpackOptions; + resolveOptions?: ResolveOptionsWebpackOptions; factoryMeta?: object; useSourceMap: boolean; useSimpleSourceMap: boolean; - buildMeta: BuildMeta; - buildInfo: Record; + buildMeta?: BuildMeta; + buildInfo?: Record; presentationalDependencies?: Dependency[]; codeGenerationDependencies?: Dependency[]; id: string | number; @@ -7240,9 +7257,9 @@ declare class Module extends DependenciesBlock { | ((requestShortener: RequestShortener) => string) )[]; get optional(): boolean; - addChunk(chunk?: any): boolean; - removeChunk(chunk?: any): void; - isInChunk(chunk?: any): boolean; + addChunk(chunk: Chunk): boolean; + removeChunk(chunk: Chunk): void; + isInChunk(chunk: Chunk): boolean; isEntryModule(): boolean; getChunks(): Chunk[]; getNumberOfChunks(): number; @@ -7372,7 +7389,7 @@ declare class ModuleDependency extends Dependency { declare abstract class ModuleFactory { create( data: ModuleFactoryCreateData, - callback: (arg0?: Error, arg1?: ModuleFactoryResult) => void + callback: (arg0?: null | Error, arg1?: ModuleFactoryResult) => void ): void; } declare interface ModuleFactoryCreateData { @@ -7935,24 +7952,41 @@ declare abstract class MultiWatching { close(callback: CallbackFunction): void; } declare class NamedChunkIdsPlugin { - constructor(options?: any); - delimiter: any; - context: any; + constructor(options?: NamedChunkIdsPluginOptions); + delimiter: string; + context?: string; /** * Apply the plugin */ apply(compiler: Compiler): void; } +declare interface NamedChunkIdsPluginOptions { + /** + * context + */ + context?: string; + + /** + * delimiter + */ + delimiter?: string; +} declare class NamedModuleIdsPlugin { - constructor(options?: any); - options: any; + constructor(options?: NamedModuleIdsPluginOptions); + options: NamedModuleIdsPluginOptions; /** * Apply the plugin */ apply(compiler: Compiler): void; } +declare interface NamedModuleIdsPluginOptions { + /** + * context + */ + context?: string; +} declare class NaturalModuleIdsPlugin { constructor(); @@ -13501,7 +13535,7 @@ declare namespace exports { export { ProfilingPlugin }; } export namespace util { - export const createHash: (algorithm: string | typeof Hash) => Hash; + export const createHash: (algorithm?: string | typeof Hash) => Hash; export namespace comparators { export let compareChunksById: (a: Chunk, b: Chunk) => 0 | 1 | -1; export let compareModulesByIdentifier: (