diff --git a/cspell.json b/cspell.json index aa165fbf3..14086b9e9 100644 --- a/cspell.json +++ b/cspell.json @@ -292,7 +292,8 @@ "xxhashjs", "Yann", "readonly", - "commithash" + "commithash", + "formaters" ], "ignoreRegExpList": [ "/Author.+/", diff --git a/declarations/LoaderContext.d.ts b/declarations/LoaderContext.d.ts index 5e740a2f6..533a60828 100644 --- a/declarations/LoaderContext.d.ts +++ b/declarations/LoaderContext.d.ts @@ -1,4 +1,5 @@ import type { SourceMap } from "../lib/NormalModule"; +import type Module from "../lib/Module"; import type { validate } from "schema-utils"; import type { AssetInfo } from "../lib/Compilation"; import type { ResolveOptionsWithDependencyType } from "../lib/ResolverFactory"; @@ -70,15 +71,15 @@ export interface LoaderPluginLoaderContext { request: string, callback: ( err: Error | null, - source: string, - sourceMap: any, - module: NormalModule + source?: string | Buffer, + sourceMap?: object | null, + module?: Module ) => void ): void; importModule( request: string, - options: ImportModuleOptions, + options: ImportModuleOptions | undefined, callback: ImportModuleCallback ): void; importModule(request: string, options?: ImportModuleOptions): Promise; diff --git a/lib/Chunk.js b/lib/Chunk.js index c6585169b..68a51d794 100644 --- a/lib/Chunk.js +++ b/lib/Chunk.js @@ -22,6 +22,7 @@ const { mergeRuntime } = require("./util/runtime"); /** @typedef {import("./ChunkGraph").ChunkFilterPredicate} ChunkFilterPredicate */ /** @typedef {import("./ChunkGraph").ChunkSizeOptions} ChunkSizeOptions */ /** @typedef {import("./ChunkGraph").ModuleFilterPredicate} ModuleFilterPredicate */ +/** @typedef {import("./ChunkGraph").ModuleId} ModuleId */ /** @typedef {import("./ChunkGroup")} ChunkGroup */ /** @typedef {import("./ChunkGroup").ChunkGroupOptions} ChunkGroupOptions */ /** @typedef {import("./Compilation")} Compilation */ @@ -367,7 +368,9 @@ class Chunk { array = []; chunkModuleIdMap[/** @type {ChunkId} */ (asyncChunk.id)] = array; } - const moduleId = chunkGraph.getModuleId(module); + const moduleId = + /** @type {ModuleId} */ + (chunkGraph.getModuleId(module)); array.push(moduleId); chunkModuleHashMap[moduleId] = chunkGraph.getRenderedModuleHash( module, diff --git a/lib/ChunkGraph.js b/lib/ChunkGraph.js index fe91053bc..c98bdbfbb 100644 --- a/lib/ChunkGraph.js +++ b/lib/ChunkGraph.js @@ -30,6 +30,7 @@ const { /** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */ /** @typedef {import("./Chunk")} Chunk */ +/** @typedef {import("./Chunk").ChunkId} ChunkId */ /** @typedef {import("./ChunkGroup")} ChunkGroup */ /** @typedef {import("./Module")} Module */ /** @typedef {import("./ModuleGraph")} ModuleGraph */ @@ -197,7 +198,7 @@ class ChunkGraphModule { this.runtimeInChunks = undefined; /** @type {RuntimeSpecMap | undefined} */ this.hashes = undefined; - /** @type {string | number} */ + /** @type {ModuleId | null} */ this.id = null; /** @type {RuntimeSpecMap> | undefined} */ this.runtimeRequirements = undefined; @@ -747,9 +748,9 @@ class ChunkGraph { if (filterFn(module)) { if (array === undefined) { array = []; - chunkModuleIdMap[asyncChunk.id] = array; + chunkModuleIdMap[/** @type {ChunkId} */ (asyncChunk.id)] = array; } - const moduleId = this.getModuleId(module); + const moduleId = /** @type {ModuleId} */ (this.getModuleId(module)); array.push(moduleId); } } @@ -771,13 +772,15 @@ class ChunkGraph { hashLength = 0, includeAllChunks = false ) { - /** @type {Record>} */ + /** @type {Record>} */ const chunkModuleHashMap = Object.create(null); + /** @typedef {Record} IdToHashMap */ + for (const asyncChunk of includeAllChunks ? chunk.getAllReferencedChunks() : chunk.getAllAsyncChunks()) { - /** @type {Record | undefined} */ + /** @type {IdToHashMap | undefined} */ let idToHashMap; for (const module of this.getOrderedChunkModulesIterable( asyncChunk, @@ -786,11 +789,15 @@ class ChunkGraph { if (filterFn(module)) { if (idToHashMap === undefined) { idToHashMap = Object.create(null); - chunkModuleHashMap[asyncChunk.id] = idToHashMap; + chunkModuleHashMap[/** @type {ChunkId} */ (asyncChunk.id)] = + /** @type {IdToHashMap} */ (idToHashMap); } const moduleId = this.getModuleId(module); const hash = this.getRenderedModuleHash(module, asyncChunk.runtime); - idToHashMap[moduleId] = hashLength ? hash.slice(0, hashLength) : hash; + /** @type {IdToHashMap} */ + (idToHashMap)[/** @type {ModuleId} */ (moduleId)] = hashLength + ? hash.slice(0, hashLength) + : hash; } } } @@ -806,7 +813,7 @@ class ChunkGraph { getChunkConditionMap(chunk, filterFn) { const map = Object.create(null); for (const c of chunk.getAllReferencedChunks()) { - map[c.id] = filterFn(c, this); + map[/** @type {ChunkId} */ (c.id)] = filterFn(c, this); } return map; } @@ -1109,8 +1116,9 @@ class ChunkGraph { disconnectChunkAndEntryModule(chunk, module) { const cgm = this._getChunkGraphModule(module); const cgc = this._getChunkGraphChunk(chunk); - cgm.entryInChunks.delete(chunk); - if (cgm.entryInChunks.size === 0) { + /** @type {EntryInChunks} */ + (cgm.entryInChunks).delete(chunk); + if (/** @type {EntryInChunks} */ (cgm.entryInChunks).size === 0) { cgm.entryInChunks = undefined; } cgc.entryModules.delete(module); @@ -1124,8 +1132,9 @@ class ChunkGraph { disconnectChunkAndRuntimeModule(chunk, module) { const cgm = this._getChunkGraphModule(module); const cgc = this._getChunkGraphChunk(chunk); - cgm.runtimeInChunks.delete(chunk); - if (cgm.runtimeInChunks.size === 0) { + /** @type {RuntimeInChunks} */ + (cgm.runtimeInChunks).delete(chunk); + if (/** @type {RuntimeInChunks} */ (cgm.runtimeInChunks).size === 0) { cgm.runtimeInChunks = undefined; } cgc.runtimeModules.delete(module); @@ -1152,8 +1161,9 @@ class ChunkGraph { const cgc = this._getChunkGraphChunk(chunk); for (const module of cgc.entryModules.keys()) { const cgm = this._getChunkGraphModule(module); - cgm.entryInChunks.delete(chunk); - if (cgm.entryInChunks.size === 0) { + /** @type {EntryInChunks} */ + (cgm.entryInChunks).delete(chunk); + if (/** @type {EntryInChunks} */ (cgm.entryInChunks).size === 0) { cgm.entryInChunks = undefined; } } @@ -1320,7 +1330,7 @@ class ChunkGraph { /** * @param {Module} module the module - * @returns {ModuleId} the id of the module + * @returns {ModuleId | null} the id of the module */ getModuleId(module) { const cgm = this._getChunkGraphModule(module); @@ -1342,7 +1352,7 @@ class ChunkGraph { * @returns {string | number} the id of the runtime */ getRuntimeId(runtime) { - return this._runtimeIds.get(runtime); + return /** @type {string | number} */ (this._runtimeIds.get(runtime)); } /** @@ -1591,6 +1601,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza if (cgm.graphHashesWithConnections === undefined) { cgm.graphHashesWithConnections = new RuntimeSpecMap(); } + /** * @param {ConnectionState} state state * @returns {"F" | "T" | "O"} result @@ -1613,6 +1624,10 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza const activeNamespaceModules = new Set(); /** @type {Map>} */ const connectedModules = new Map(); + /** + * @param {ModuleGraphConnection} connection connection + * @param {string} stateInfo state info + */ const processConnection = (connection, stateInfo) => { const module = connection.module; stateInfo += module.getExportsType(this.moduleGraph, strict); diff --git a/lib/Compilation.js b/lib/Compilation.js index 92509d98d..34deb2ed5 100644 --- a/lib/Compilation.js +++ b/lib/Compilation.js @@ -350,7 +350,7 @@ const { isSourceEqual } = require("./util/source"); * @property {boolean=} forToString */ -/** @typedef {KnownCreateStatsOptionsContext & Record} CreateStatsOptionsContext */ +/** @typedef {Record & KnownCreateStatsOptionsContext} CreateStatsOptionsContext */ /** @typedef {{module: Module, hash: string, runtime: RuntimeSpec, runtimes: RuntimeSpec[]}[]} CodeGenerationJobs */ @@ -571,6 +571,10 @@ class Compilation { */ const createProcessAssetsHook = (name, stage, getArgs, code) => { if (!this._backCompat && code) return; + /** + * @param {string} reason reason + * @returns {string} error message + */ const errorMessage = reason => `Can't automatically convert plugin using Compilation.hooks.${name} to Compilation.hooks.processAssets because ${reason}. BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a single Compilation.hooks.processAssets hook.`; @@ -661,14 +665,14 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si */ afterChunks: new SyncHook(["chunks"]), - /** @type {SyncBailHook<[Iterable]>} */ + /** @type {SyncBailHook<[Iterable], boolean | void>} */ optimizeDependencies: new SyncBailHook(["modules"]), /** @type {SyncHook<[Iterable]>} */ afterOptimizeDependencies: new SyncHook(["modules"]), /** @type {SyncHook<[]>} */ optimize: new SyncHook([]), - /** @type {SyncBailHook<[Iterable]>} */ + /** @type {SyncBailHook<[Iterable], boolean | void>} */ optimizeModules: new SyncBailHook(["modules"]), /** @type {SyncHook<[Iterable]>} */ afterOptimizeModules: new SyncHook(["modules"]), @@ -706,7 +710,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si "runtimeRequirements", "context" ]), - /** @type {HookMap, RuntimeRequirementsContext]>>} */ + /** @type {HookMap, RuntimeRequirementsContext], void>>} */ runtimeRequirementInModule: new HookMap( () => new SyncBailHook(["module", "runtimeRequirements", "context"]) ), @@ -716,7 +720,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si "runtimeRequirements", "context" ]), - /** @type {HookMap, RuntimeRequirementsContext]>>} */ + /** @type {HookMap, RuntimeRequirementsContext], void>>} */ runtimeRequirementInTree: new HookMap( () => new SyncBailHook(["chunk", "runtimeRequirements", "context"]) ), @@ -1109,14 +1113,15 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si /** * @param {string | boolean | StatsOptions | undefined} optionsOrPreset stats option value - * @param {CreateStatsOptionsContext} context context + * @param {CreateStatsOptionsContext=} context context * @returns {NormalizedStatsOptions} normalized options */ createStatsOptions(optionsOrPreset, context = {}) { - if ( - typeof optionsOrPreset === "boolean" || - typeof optionsOrPreset === "string" - ) { + if (typeof optionsOrPreset === "boolean") { + optionsOrPreset = { + preset: optionsOrPreset === false ? "none" : "normal" + }; + } else if (typeof optionsOrPreset === "string") { optionsOrPreset = { preset: optionsOrPreset }; } if (typeof optionsOrPreset === "object" && optionsOrPreset !== null) { @@ -1126,7 +1131,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si const options = {}; // eslint-disable-next-line guard-for-in for (const key in optionsOrPreset) { - options[key] = optionsOrPreset[key]; + options[key] = optionsOrPreset[/** @type {keyof StatsOptions} */ (key)]; } if (options.preset !== undefined) { this.hooks.statsPreset.for(options.preset).call(options, context); @@ -2680,6 +2685,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si const logger = this.getLogger("webpack.Compilation.ModuleProfile"); // Avoid coverage problems due indirect changes + /** + * @param {number} value value + * @param {string} msg message + */ /* istanbul ignore next */ const logByValue = (value, msg) => { if (value > 1000) { @@ -2891,6 +2900,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si * @returns {void} */ seal(callback) { + /** + * @param {WebpackError=} err err + * @returns {void} + */ const finalCallback = err => { this.factorizeQueue.clear(); this.buildQueue.clear(); @@ -3808,6 +3821,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o const moduleGraph = this.moduleGraph; const queue = new Set([module]); + /** @type {number} */ let depth; moduleGraph.setDepth(module, 0); @@ -3823,7 +3837,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o for (module of queue) { queue.delete(module); - depth = moduleGraph.getDepth(module) + 1; + depth = /** @type {number} */ (moduleGraph.getDepth(module)) + 1; for (const connection of moduleGraph.getOutgoingConnections(module)) { const refModule = connection.module; diff --git a/lib/Compiler.js b/lib/Compiler.js index 3ac621b78..dd722cf28 100644 --- a/lib/Compiler.js +++ b/lib/Compiler.js @@ -47,13 +47,18 @@ const { isSourceEqual } = require("./util/source"); /** @typedef {import("./Module").BuildInfo} BuildInfo */ /** @typedef {import("./config/target").PlatformTargetProperties} PlatformTargetProperties */ /** @typedef {import("./logging/createConsoleLogger").LoggingFunction} LoggingFunction */ -/** @typedef {import("./util/WeakTupleMap")} WeakTupleMap */ /** @typedef {import("./util/fs").IStats} IStats */ /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ /** @typedef {import("./util/fs").IntermediateFileSystem} IntermediateFileSystem */ /** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */ /** @typedef {import("./util/fs").WatchFileSystem} WatchFileSystem */ +/** + * @template {any[]} T + * @template V + * @typedef {import("./util/WeakTupleMap")} WeakTupleMap + */ + /** * @typedef {object} CompilationParams * @property {NormalModuleFactory} normalModuleFactory @@ -283,7 +288,7 @@ class Compiler { this.cache = new Cache(); - /** @type {Map | undefined} */ + /** @type {Map }> | undefined} */ this.moduleMemCaches = undefined; this.compilerPath = ""; diff --git a/lib/DelegatedModule.js b/lib/DelegatedModule.js index 9f669db67..dc4d2bc3a 100644 --- a/lib/DelegatedModule.js +++ b/lib/DelegatedModule.js @@ -36,6 +36,10 @@ const makeSerializable = require("./util/makeSerializable"); /** @typedef {import("./util/Hash")} Hash */ /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ +/** @typedef {string} SourceRequest */ +/** @typedef {"require" | "object"} Type */ +/** @typedef {TODO} Data */ + const TYPES = new Set(["javascript"]); const RUNTIME_REQUIREMENTS = new Set([ RuntimeGlobals.module, @@ -44,9 +48,9 @@ const RUNTIME_REQUIREMENTS = new Set([ class DelegatedModule extends Module { /** - * @param {string} sourceRequest source request - * @param {TODO} data data - * @param {"require" | "object"} type type + * @param {SourceRequest} sourceRequest source request + * @param {Data} data data + * @param {Type} type type * @param {string} userRequest user request * @param {string | Module} originalRequest original request */ diff --git a/lib/DelegatedModuleFactoryPlugin.js b/lib/DelegatedModuleFactoryPlugin.js index 65f7d150f..ae9b79aae 100644 --- a/lib/DelegatedModuleFactoryPlugin.js +++ b/lib/DelegatedModuleFactoryPlugin.js @@ -7,15 +7,28 @@ const DelegatedModule = require("./DelegatedModule"); +/** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */ +/** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsContent} DllReferencePluginOptionsContent */ +/** @typedef {import("./DelegatedModule").Data} Data */ +/** @typedef {import("./DelegatedModule").SourceRequest} SourceRequest */ +/** @typedef {import("./DelegatedModule").Type} Type */ /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */ -// options.source -// options.type -// options.context -// options.scope -// options.content -// options.associatedObjectForCache +/** + * @typedef {object} Options + * @property {SourceRequest} source source + * @property {NonNullable} context absolute context path to which lib ident is relative to + * @property {DllReferencePluginOptionsContent} content content + * @property {DllReferencePluginOptions["type"]} type type + * @property {DllReferencePluginOptions["extensions"]} extensions extensions + * @property {DllReferencePluginOptions["scope"]} scope scope + * @property {object=} associatedObjectForCache object for caching + */ + class DelegatedModuleFactoryPlugin { + /** + * @param {Options} options options + */ constructor(options) { this.options = options; options.type = options.type || "require"; @@ -44,14 +57,17 @@ class DelegatedModuleFactoryPlugin { new DelegatedModule( this.options.source, resolved, - this.options.type, + /** @type {Type} */ (this.options.type), innerRequest, request ) ); } - for (let i = 0; i < this.options.extensions.length; i++) { - const extension = this.options.extensions[i]; + const extensions = + /** @type {string[]} */ + (this.options.extensions); + for (let i = 0; i < extensions.length; i++) { + const extension = extensions[i]; const requestPlusExt = innerRequest + extension; if (requestPlusExt in this.options.content) { resolved = this.options.content[requestPlusExt]; @@ -60,7 +76,7 @@ class DelegatedModuleFactoryPlugin { new DelegatedModule( this.options.source, resolved, - this.options.type, + /** @type {Type} */ (this.options.type), requestPlusExt, request + extension ) @@ -81,7 +97,7 @@ class DelegatedModuleFactoryPlugin { return new DelegatedModule( this.options.source, resolved, - this.options.type, + /** @type {Type} */ (this.options.type), request, module ); diff --git a/lib/DelegatedPlugin.js b/lib/DelegatedPlugin.js index ffcc489c2..735e2f083 100644 --- a/lib/DelegatedPlugin.js +++ b/lib/DelegatedPlugin.js @@ -9,8 +9,12 @@ const DelegatedModuleFactoryPlugin = require("./DelegatedModuleFactoryPlugin"); const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency"); /** @typedef {import("./Compiler")} Compiler */ +/** @typedef {import("./DelegatedModuleFactoryPlugin").Options} Options */ class DelegatedPlugin { + /** + * @param {Options} options options + */ constructor(options) { this.options = options; } diff --git a/lib/Dependency.js b/lib/Dependency.js index 1658f8ae3..691251db6 100644 --- a/lib/Dependency.js +++ b/lib/Dependency.js @@ -328,6 +328,8 @@ Dependency.NO_EXPORTS_REFERENCED = []; /** @type {string[][]} */ Dependency.EXPORTS_OBJECT_REFERENCED = [[]]; +// eslint-disable-next-line no-warning-comments +// @ts-ignore https://github.com/microsoft/TypeScript/issues/42919 Object.defineProperty(Dependency.prototype, "module", { /** * @deprecated @@ -350,6 +352,8 @@ Object.defineProperty(Dependency.prototype, "module", { } }); +// eslint-disable-next-line no-warning-comments +// @ts-ignore https://github.com/microsoft/TypeScript/issues/42919 Object.defineProperty(Dependency.prototype, "disconnect", { get() { throw new Error( diff --git a/lib/DllEntryPlugin.js b/lib/DllEntryPlugin.js index 27c784963..de849fa53 100644 --- a/lib/DllEntryPlugin.js +++ b/lib/DllEntryPlugin.js @@ -10,12 +10,14 @@ const DllEntryDependency = require("./dependencies/DllEntryDependency"); const EntryDependency = require("./dependencies/EntryDependency"); /** @typedef {import("./Compiler")} Compiler */ +/** @typedef {string[]} Entries */ +/** @typedef {{ name: string, filename: TODO }} Options */ class DllEntryPlugin { /** * @param {string} context context - * @param {string[]} entries entry names - * @param {TODO} options options + * @param {Entries} entries entry names + * @param {Options} options options */ constructor(context, entries, options) { this.context = context; diff --git a/lib/DllPlugin.js b/lib/DllPlugin.js index 636567041..25440df04 100644 --- a/lib/DllPlugin.js +++ b/lib/DllPlugin.js @@ -12,6 +12,8 @@ const createSchemaValidation = require("./util/create-schema-validation"); /** @typedef {import("../declarations/plugins/DllPlugin").DllPluginOptions} DllPluginOptions */ /** @typedef {import("./Compiler")} Compiler */ +/** @typedef {import("./DllEntryPlugin").Entries} Entries */ +/** @typedef {import("./DllEntryPlugin").Options} Options */ const validate = createSchemaValidation( require("../schemas/plugins/DllPlugin.check.js"), @@ -43,13 +45,13 @@ class DllPlugin { compiler.hooks.entryOption.tap("DllPlugin", (context, entry) => { if (typeof entry !== "function") { for (const name of Object.keys(entry)) { - const options = { - name, - filename: entry.filename - }; - new DllEntryPlugin(context, entry[name].import, options).apply( - compiler - ); + /** @type {Options} */ + const options = { name, filename: entry.filename }; + new DllEntryPlugin( + context, + /** @type {Entries} */ (entry[name].import), + options + ).apply(compiler); } } else { throw new Error( diff --git a/lib/DllReferencePlugin.js b/lib/DllReferencePlugin.js index f8b8ab890..50b2c5410 100644 --- a/lib/DllReferencePlugin.js +++ b/lib/DllReferencePlugin.js @@ -15,6 +15,7 @@ const makePathsRelative = require("./util/identifier").makePathsRelative; /** @typedef {import("../declarations/WebpackOptions").Externals} Externals */ /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */ +/** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsContent} DllReferencePluginOptionsContent */ /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsManifest} DllReferencePluginOptionsManifest */ /** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ @@ -28,6 +29,8 @@ const validate = createSchemaValidation( } ); +/** @typedef {{ path: string, data: DllReferencePluginOptionsManifest | undefined, error: Error | undefined }} CompilationDataItem */ + class DllReferencePlugin { /** * @param {DllReferencePluginOptions} options options object @@ -35,7 +38,7 @@ class DllReferencePlugin { constructor(options) { validate(options); this.options = options; - /** @type {WeakMap} */ + /** @type {WeakMap} */ this._compilationData = new WeakMap(); } @@ -64,6 +67,7 @@ class DllReferencePlugin { /** @type {InputFileSystem} */ (compiler.inputFileSystem).readFile(manifest, (err, result) => { if (err) return callback(err); + /** @type {CompilationDataItem} */ const data = { path: manifest, data: undefined, @@ -79,13 +83,13 @@ class DllReferencePlugin { // Store the error in the params so that it can // be added as a compilation error later on. const manifestPath = makePathsRelative( - compiler.options.context, + /** @type {string} */ (compiler.options.context), manifest, compiler.root ); data.error = new DllManifestError( manifestPath, - parseErr.message + /** @type {Error} */ (parseErr).message ); } this._compilationData.set(params, data); @@ -101,13 +105,15 @@ class DllReferencePlugin { compiler.hooks.compile.tap("DllReferencePlugin", params => { let name = this.options.name; let sourceType = this.options.sourceType; - let content = + let resolvedContent = "content" in this.options ? this.options.content : undefined; if ("manifest" in this.options) { const manifestParameter = this.options.manifest; let manifest; if (typeof manifestParameter === "string") { - const data = this._compilationData.get(params); + const data = + /** @type {CompilationDataItem} */ + (this._compilationData.get(params)); // If there was an error parsing the manifest // file, exit now because the error will be added // as a compilation error in the "compilation" hook. @@ -121,13 +127,13 @@ class DllReferencePlugin { if (manifest) { if (!name) name = manifest.name; if (!sourceType) sourceType = manifest.type; - if (!content) content = manifest.content; + if (!resolvedContent) resolvedContent = manifest.content; } } /** @type {Externals} */ const externals = {}; const source = `dll-reference ${name}`; - externals[source] = name; + externals[source] = /** @type {string} */ (name); const normalModuleFactory = params.normalModuleFactory; new ExternalModuleFactoryPlugin(sourceType || "var", externals).apply( normalModuleFactory @@ -136,8 +142,12 @@ class DllReferencePlugin { source, type: this.options.type, scope: this.options.scope, - context: this.options.context || compiler.options.context, - content, + context: + /** @type {string} */ + (this.options.context || compiler.options.context), + content: + /** @type {DllReferencePluginOptionsContent} */ + (resolvedContent), extensions: this.options.extensions, associatedObjectForCache: compiler.root }).apply(normalModuleFactory); @@ -149,7 +159,9 @@ class DllReferencePlugin { if ("manifest" in this.options) { const manifest = this.options.manifest; if (typeof manifest === "string") { - const data = this._compilationData.get(params); + const data = /** @type {CompilationDataItem} */ ( + this._compilationData.get(params) + ); // If there was an error parsing the manifest file, add the // error as a compilation error to make the compilation fail. if (data.error) { diff --git a/lib/EnvironmentPlugin.js b/lib/EnvironmentPlugin.js index 3a8d9dcdd..93292cc56 100644 --- a/lib/EnvironmentPlugin.js +++ b/lib/EnvironmentPlugin.js @@ -12,15 +12,18 @@ const WebpackError = require("./WebpackError"); /** @typedef {import("./DefinePlugin").CodeValue} CodeValue */ class EnvironmentPlugin { + /** + * @param {(string | string[] | Record)[]} keys keys + */ constructor(...keys) { if (keys.length === 1 && Array.isArray(keys[0])) { this.keys = keys[0]; this.defaultValues = {}; } else if (keys.length === 1 && keys[0] && typeof keys[0] === "object") { this.keys = Object.keys(keys[0]); - this.defaultValues = keys[0]; + this.defaultValues = /** @type {Record} */ (keys[0]); } else { - this.keys = keys; + this.keys = /** @type {string[]} */ (keys); this.defaultValues = {}; } } diff --git a/lib/EvalDevToolModulePlugin.js b/lib/EvalDevToolModulePlugin.js index 96b71a78e..ba2e5b6ac 100644 --- a/lib/EvalDevToolModulePlugin.js +++ b/lib/EvalDevToolModulePlugin.js @@ -39,7 +39,7 @@ class EvalDevToolModulePlugin { /** * @param {EvalDevToolModulePluginOptions=} options options */ - constructor(options) { + constructor(options = {}) { this.namespace = options.namespace || ""; this.sourceUrlComment = options.sourceUrlComment || "\n//# sourceURL=[url]"; this.moduleFilenameTemplate = diff --git a/lib/EvalSourceMapDevToolPlugin.js b/lib/EvalSourceMapDevToolPlugin.js index eb0edaa52..9619211cc 100644 --- a/lib/EvalSourceMapDevToolPlugin.js +++ b/lib/EvalSourceMapDevToolPlugin.js @@ -17,6 +17,7 @@ const { makePathsAbsolute } = require("./util/identifier"); /** @typedef {import("webpack-sources").Source} Source */ /** @typedef {import("../declarations/WebpackOptions").DevTool} DevToolOptions */ /** @typedef {import("../declarations/plugins/SourceMapDevToolPlugin").SourceMapDevToolPluginOptions} SourceMapDevToolPluginOptions */ +/** @typedef {import("./ChunkGraph").ModuleId} ModuleId */ /** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./NormalModule").SourceMap} SourceMap */ @@ -163,7 +164,9 @@ class EvalSourceMapDevToolPlugin { sourceMap.sourcesContent = undefined; } sourceMap.sourceRoot = options.sourceRoot || ""; - const moduleId = chunkGraph.getModuleId(m); + const moduleId = + /** @type {ModuleId} */ + (chunkGraph.getModuleId(m)); sourceMap.file = typeof moduleId === "number" ? `${moduleId}.js` : moduleId; diff --git a/lib/ExportsInfo.js b/lib/ExportsInfo.js index c4db68ad0..cfe6a2b1c 100644 --- a/lib/ExportsInfo.js +++ b/lib/ExportsInfo.js @@ -71,9 +71,11 @@ makeSerializable( "RestoreProvidedData" ); +/** @typedef {Map} Exports */ + class ExportsInfo { constructor() { - /** @type {Map} */ + /** @type {Exports} */ this._exports = new Map(); this._otherExportsInfo = new ExportInfo(null); this._sideEffectsOnlyInfo = new ExportInfo("*side effects only*"); @@ -144,6 +146,10 @@ class ExportsInfo { return this._otherExportsInfo; } + /** + * @param {Exports} exports exports + * @private + */ _sortExportsMap(exports) { if (exports.size > 1) { const namesInOrder = []; @@ -520,7 +526,7 @@ class ExportsInfo { return false; } } - return new SortableSet(array); + return /** @type {SortableSet} */ (new SortableSet(array)); } /** @@ -796,6 +802,8 @@ class ExportsInfo { } } +/** @typedef {Map} Target */ + class ExportInfo { /** * @param {string} name the original name of the export @@ -811,7 +819,7 @@ class ExportInfo { this._usedName = initFrom ? initFrom._usedName : null; /** * @private - * @type {UsageStateType} + * @type {UsageStateType | undefined} */ this._globalUsed = initFrom ? initFrom._globalUsed : undefined; /** @@ -858,9 +866,9 @@ class ExportInfo { this.canMangleUse = initFrom ? initFrom.canMangleUse : undefined; /** @type {boolean} */ this.exportsInfoOwned = false; - /** @type {ExportsInfo=} */ + /** @type {ExportsInfo | undefined} */ this.exportsInfo = undefined; - /** @type {Map=} */ + /** @type {Target | undefined} */ this._target = undefined; if (initFrom && initFrom._target) { this._target = new Map(); @@ -872,7 +880,7 @@ class ExportInfo { }); } } - /** @type {Map=} */ + /** @type {Target | undefined} */ this._maxTarget = undefined; } @@ -1004,8 +1012,9 @@ class ExportInfo { } else { let changed = false; forEachRuntime(runtime, runtime => { - /** @type {UsageStateType} */ - let oldValue = this._usedInRuntime.get(runtime); + let oldValue = + /** @type {UsageStateType} */ + (this._usedInRuntime.get(runtime)); if (oldValue === undefined) oldValue = UsageState.Unused; if (newValue !== oldValue && condition(oldValue)) { if (newValue === UsageState.Unused) { @@ -1046,8 +1055,9 @@ class ExportInfo { } else { let changed = false; forEachRuntime(runtime, runtime => { - /** @type {UsageStateType} */ - let oldValue = this._usedInRuntime.get(runtime); + let oldValue = + /** @type {UsageStateType} */ + (this._usedInRuntime.get(runtime)); if (oldValue === undefined) oldValue = UsageState.Unused; if (newValue !== oldValue) { if (newValue === UsageState.Unused) { @@ -1108,7 +1118,7 @@ class ExportInfo { : oldTarget.export) ) { oldTarget.connection = connection; - oldTarget.export = exportName; + oldTarget.export = /** @type {string[]} */ (exportName); oldTarget.priority = priority; this._maxTarget = undefined; return true; @@ -1181,7 +1191,7 @@ class ExportInfo { } } if (this._usedName !== null) return this._usedName; - return this.name || fallbackName; + return /** @type {string | false} */ (this.name || fallbackName); } /** @@ -1220,10 +1230,11 @@ class ExportInfo { _getMaxTarget() { if (this._maxTarget !== undefined) return this._maxTarget; - if (this._target.size <= 1) return (this._maxTarget = this._target); + if (/** @type {Target} */ (this._target).size <= 1) + return (this._maxTarget = this._target); let maxPriority = -Infinity; let minPriority = Infinity; - for (const { priority } of this._target.values()) { + for (const { priority } of /** @type {Target} */ (this._target).values()) { if (maxPriority < priority) maxPriority = priority; if (minPriority > priority) minPriority = priority; } @@ -1232,7 +1243,7 @@ class ExportInfo { // This is an edge case const map = new Map(); - for (const [key, value] of this._target) { + for (const [key, value] of /** @type {Target} */ (this._target)) { if (maxPriority === value.priority) { map.set(key, value); } @@ -1244,7 +1255,7 @@ class ExportInfo { /** * @param {ModuleGraph} moduleGraph the module graph * @param {function(Module): boolean} validTargetModuleFilter a valid target module - * @returns {{ module: Module, export: string[] | undefined } | undefined | false} the target, undefined when there is no target, false when no target is valid + * @returns {{ module: Module, export: string[] | undefined } | null | undefined | false} the target, undefined when there is no target, false when no target is valid */ findTarget(moduleGraph, validTargetModuleFilter) { return this._findTarget(moduleGraph, validTargetModuleFilter, new Set()); @@ -1254,11 +1265,13 @@ class ExportInfo { * @param {ModuleGraph} moduleGraph the module graph * @param {function(Module): boolean} validTargetModuleFilter a valid target module * @param {Set} alreadyVisited set of already visited export info to avoid circular references - * @returns {{ module: Module, export: string[] | undefined } | undefined | false} the target, undefined when there is no target, false when no target is valid + * @returns {{ module: Module, export: string[] | undefined } | null | undefined | false} the target, undefined when there is no target, false when no target is valid */ _findTarget(moduleGraph, validTargetModuleFilter, alreadyVisited) { if (!this._target || this._target.size === 0) return; - const rawTarget = this._getMaxTarget().values().next().value; + const rawTarget = + /** @type {Target} */ + (this._getMaxTarget()).values().next().value; if (!rawTarget) return; /** @type {{ module: Module, export: string[] | undefined }} */ let target = { @@ -1366,7 +1379,7 @@ class ExportInfo { if (alreadyVisited && alreadyVisited.has(this)) return CIRCULAR; const newAlreadyVisited = new Set(alreadyVisited); newAlreadyVisited.add(this); - const values = this._getMaxTarget().values(); + const values = /** @type {Target} */ (this._getMaxTarget()).values(); const target = resolveTarget(values.next().value, newAlreadyVisited); if (target === CIRCULAR) return CIRCULAR; if (target === null) return; @@ -1398,15 +1411,19 @@ class ExportInfo { const target = this._getTarget(moduleGraph, resolveTargetFilter, undefined); if (target === CIRCULAR) return; if (!target) return; - const originalTarget = this._getMaxTarget().values().next().value; + const originalTarget = + /** @type {Target} */ + (this._getMaxTarget()).values().next().value; if ( originalTarget.connection === target.connection && originalTarget.export === target.export ) { return; } - this._target.clear(); - this._target.set(undefined, { + /** @type {Target} */ + (this._target).clear(); + /** @type {Target} */ + (this._target).set(undefined, { connection: updateOriginalConnection ? updateOriginalConnection(target) : target.connection, @@ -1432,6 +1449,11 @@ class ExportInfo { return this.exportsInfo; } + /** + * @param {ExportInfo} baseInfo base info + * @param {RuntimeSpec} runtime runtime + * @returns {boolean} true when has info, otherwise false + */ hasInfo(baseInfo, runtime) { return ( (this._usedName && this._usedName !== this.name) || @@ -1441,10 +1463,20 @@ class ExportInfo { ); } + /** + * @param {Hash} hash the hash + * @param {RuntimeSpec} runtime the runtime + * @returns {void} + */ updateHash(hash, runtime) { this._updateHash(hash, runtime, new Set()); } + /** + * @param {Hash} hash the hash + * @param {RuntimeSpec} runtime the runtime + * @param {Set} alreadyVisitedExportsInfo for circular references + */ _updateHash(hash, runtime, alreadyVisitedExportsInfo) { hash.update( `${this._usedName || this.name}${this.getUsed(runtime)}${this.provided}${ diff --git a/lib/ExternalModule.js b/lib/ExternalModule.js index 0ff016efc..cf946fc41 100644 --- a/lib/ExternalModule.js +++ b/lib/ExternalModule.js @@ -719,7 +719,8 @@ class ExternalModule extends Module { request, /** @type {string} */ (runtimeTemplate.outputOptions.importMetaName), - runtimeTemplate.supportNodePrefixForCoreModules() + /** @type {boolean} */ + (runtimeTemplate.supportNodePrefixForCoreModules()) ) : getSourceForCommonJsExternal(request); case "amd": diff --git a/lib/FileSystemInfo.js b/lib/FileSystemInfo.js index f58d09332..9112ca07b 100644 --- a/lib/FileSystemInfo.js +++ b/lib/FileSystemInfo.js @@ -16,6 +16,9 @@ const { join, dirname, relative, lstatReadlinkAbsolute } = require("./util/fs"); const makeSerializable = require("./util/makeSerializable"); const processAsyncTree = require("./util/processAsyncTree"); +/** @typedef {import("enhanced-resolve").Resolver} Resolver */ +/** @typedef {import("enhanced-resolve").ResolveRequest} ResolveRequest */ +/** @typedef {import("enhanced-resolve").ResolveFunctionAsync} ResolveFunctionAsync */ /** @typedef {import("./WebpackError")} WebpackError */ /** @typedef {import("./logging/Logger").Logger} Logger */ /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ @@ -23,9 +26,20 @@ const processAsyncTree = require("./util/processAsyncTree"); /** @typedef {typeof import("./util/Hash")} Hash */ /** @typedef {import("./util/fs").IStats} IStats */ /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ +/** @typedef {import("./util/fs").PathLike} PathLike */ +/** @typedef {import("./util/fs").StringCallback} StringCallback */ +/** + * @template T + * @typedef {import("./util/AsyncQueue").Callback} ProcessorCallback + */ +/** + * @template T, R + * @typedef {import("./util/AsyncQueue").Processor} Processor + */ const supportsEsm = Number(process.versions.modules) >= 83; +/** @type {Set} */ const builtinModules = new Set(nodeModule.builtinModules); let FS_ACCURACY = 2000; @@ -43,6 +57,8 @@ const RBDT_FILE = 7; const RBDT_DIRECTORY_DEPENDENCIES = 8; const RBDT_FILE_DEPENDENCIES = 9; +/** @typedef {RBDT_RESOLVE_CJS | RBDT_RESOLVE_ESM | RBDT_RESOLVE_DIRECTORY | RBDT_RESOLVE_CJS_FILE | RBDT_RESOLVE_CJS_FILE_AS_CHILD | RBDT_RESOLVE_ESM_FILE | RBDT_DIRECTORY | RBDT_FILE | RBDT_DIRECTORY_DEPENDENCIES | RBDT_FILE_DEPENDENCIES} JobType */ + const INVALID = Symbol("invalid"); /** @@ -79,27 +95,31 @@ const INVALID = Symbol("invalid"); * @property {string} hash */ +/** @typedef {Set} Symlinks */ + /** * @typedef {object} ContextTimestampAndHash * @property {number} safeTime * @property {string=} timestampHash * @property {string} hash * @property {ResolvedContextTimestampAndHash=} resolved - * @property {Set=} symlinks + * @property {Symlinks=} symlinks */ /** * @typedef {object} ContextHash * @property {string} hash * @property {string=} resolved - * @property {Set=} symlinks + * @property {Symlinks=} symlinks */ +/** @typedef {Set} SnapshotContent */ + /** * @typedef {object} SnapshotOptimizationEntry * @property {Snapshot} snapshot * @property {number} shared - * @property {Set | undefined} snapshotContent + * @property {SnapshotContent | undefined} snapshotContent * @property {Set | undefined} children */ @@ -108,7 +128,7 @@ const INVALID = Symbol("invalid"); * @property {Set} files list of files * @property {Set} directories list of directories * @property {Set} missing list of missing entries - * @property {Map} resolveResults stored resolve results + * @property {Map} resolveResults stored resolve results * @property {object} resolveDependencies dependencies of the resolving * @property {Set} resolveDependencies.files list of files * @property {Set} resolveDependencies.directories list of directories @@ -128,12 +148,17 @@ const DONE_ITERATOR_RESULT = new Set().keys().next(); // Tshs = Timestamp + Hash combinations class SnapshotIterator { + /** + * @param {() => IteratorResult} next next + */ constructor(next) { this.next = next; } } -/** @typedef {(snapshot: Snapshot) => (Map | Set)[]} GetMapsFunction */ +/** + * @typedef {(snapshot: Snapshot) => (Map | Set | undefined)[]} GetMapsFunction + */ class SnapshotIterable { /** @@ -149,12 +174,13 @@ class SnapshotIterable { let state = 0; /** @type {IterableIterator} */ let it; - /** @type {(snapshot: Snapshot) => (Map | Set)[]} */ + /** @type {(snapshot: Snapshot) => (Map | Set | undefined)[]} */ let getMaps; - /** @type {(Map | Set)[]} */ + /** @type {(Map | Set | undefined)[]} */ let maps; /** @type {Snapshot} */ let snapshot; + /** @type {Snapshot[] | undefined} */ let queue; return new SnapshotIterator(() => { for (;;) { @@ -202,7 +228,7 @@ class SnapshotIterable { } } if (queue !== undefined && queue.length > 0) { - snapshot = queue.pop(); + snapshot = /** @type {Snapshot} */ (queue.pop()); maps = getMaps(snapshot); state = 1; break; @@ -225,6 +251,12 @@ class SnapshotIterable { /** @typedef {Map} ContextTimestamps */ /** @typedef {Map} ContextHashes */ /** @typedef {Map} ContextTshs */ +/** @typedef {Map} MissingExistence */ +/** @typedef {Map} ManagedItemInfo */ +/** @typedef {Set} ManagedFiles */ +/** @typedef {Set} ManagedContexts */ +/** @typedef {Set} ManagedMissing */ +/** @typedef {Set} Children */ class Snapshot { constructor() { @@ -249,17 +281,17 @@ class Snapshot { this.contextHashes = undefined; /** @type {ContextTshs | undefined} */ this.contextTshs = undefined; - /** @type {Map | undefined} */ + /** @type {MissingExistence | undefined} */ this.missingExistence = undefined; - /** @type {Map | undefined} */ + /** @type {ManagedItemInfo | undefined} */ this.managedItemInfo = undefined; - /** @type {Set | undefined} */ + /** @type {ManagedFiles | undefined} */ this.managedFiles = undefined; - /** @type {Set | undefined} */ + /** @type {ManagedContexts | undefined} */ this.managedContexts = undefined; - /** @type {Set | undefined} */ + /** @type {ManagedMissing | undefined} */ this.managedMissing = undefined; - /** @type {Set | undefined} */ + /** @type {Children | undefined} */ this.children = undefined; } @@ -275,20 +307,38 @@ class Snapshot { this.startTime = value; } + /** + * @param {number | undefined} value value + * @param {Snapshot} snapshot snapshot + */ setMergedStartTime(value, snapshot) { if (value) { if (snapshot.hasStartTime()) { - this.setStartTime(Math.min(value, snapshot.startTime)); + this.setStartTime( + Math.min( + value, + /** @type {NonNullable} */ + (snapshot.startTime) + ) + ); } else { this.setStartTime(value); } - } else if (snapshot.hasStartTime()) this.setStartTime(snapshot.startTime); + } else if (snapshot.hasStartTime()) { + this.setStartTime( + /** @type {NonNullable} */ + (snapshot.startTime) + ); + } } hasFileTimestamps() { return (this._flags & 2) !== 0; } + /** + * @param {FileTimestamps} value file timestamps + */ setFileTimestamps(value) { this._flags = this._flags | 2; this.fileTimestamps = value; @@ -298,6 +348,9 @@ class Snapshot { return (this._flags & 4) !== 0; } + /** + * @param {FileHashes} value file hashes + */ setFileHashes(value) { this._flags = this._flags | 4; this.fileHashes = value; @@ -307,6 +360,9 @@ class Snapshot { return (this._flags & 8) !== 0; } + /** + * @param {FileTshs} value file tshs + */ setFileTshs(value) { this._flags = this._flags | 8; this.fileTshs = value; @@ -316,6 +372,9 @@ class Snapshot { return (this._flags & 0x10) !== 0; } + /** + * @param {ContextTimestamps} value context timestamps + */ setContextTimestamps(value) { this._flags = this._flags | 0x10; this.contextTimestamps = value; @@ -325,6 +384,9 @@ class Snapshot { return (this._flags & 0x20) !== 0; } + /** + * @param {ContextHashes} value context hashes + */ setContextHashes(value) { this._flags = this._flags | 0x20; this.contextHashes = value; @@ -334,6 +396,9 @@ class Snapshot { return (this._flags & 0x40) !== 0; } + /** + * @param {ContextTshs} value context tshs + */ setContextTshs(value) { this._flags = this._flags | 0x40; this.contextTshs = value; @@ -343,6 +408,9 @@ class Snapshot { return (this._flags & 0x80) !== 0; } + /** + * @param {MissingExistence} value context tshs + */ setMissingExistence(value) { this._flags = this._flags | 0x80; this.missingExistence = value; @@ -352,6 +420,9 @@ class Snapshot { return (this._flags & 0x100) !== 0; } + /** + * @param {ManagedItemInfo} value managed item info + */ setManagedItemInfo(value) { this._flags = this._flags | 0x100; this.managedItemInfo = value; @@ -361,6 +432,9 @@ class Snapshot { return (this._flags & 0x200) !== 0; } + /** + * @param {ManagedFiles} value managed files + */ setManagedFiles(value) { this._flags = this._flags | 0x200; this.managedFiles = value; @@ -370,6 +444,9 @@ class Snapshot { return (this._flags & 0x400) !== 0; } + /** + * @param {ManagedContexts} value managed contexts + */ setManagedContexts(value) { this._flags = this._flags | 0x400; this.managedContexts = value; @@ -379,6 +456,9 @@ class Snapshot { return (this._flags & 0x800) !== 0; } + /** + * @param {ManagedMissing} value managed missing + */ setManagedMissing(value) { this._flags = this._flags | 0x800; this.managedMissing = value; @@ -388,16 +468,23 @@ class Snapshot { return (this._flags & 0x1000) !== 0; } + /** + * @param {Children} value children + */ setChildren(value) { this._flags = this._flags | 0x1000; this.children = value; } + /** + * @param {Snapshot} child children + */ addChild(child) { if (!this.hasChildren()) { this.setChildren(new Set()); } - this.children.add(child); + /** @type {Children} */ + (this.children).add(child); } /** @@ -496,22 +583,35 @@ makeSerializable(Snapshot, "webpack/lib/FileSystemInfo", "Snapshot"); const MIN_COMMON_SNAPSHOT_SIZE = 3; +/** + * @template U, T + * @typedef {U extends true ? Set : Map} SnapshotOptimizationValue + */ + /** * @template T + * @template {boolean} [U=false] */ class SnapshotOptimization { /** * @param {function(Snapshot): boolean} has has value - * @param {function(Snapshot): Map | Set} get get value - * @param {function(Snapshot, Map | Set): void} set set value + * @param {function(Snapshot): SnapshotOptimizationValue | undefined} get get value + * @param {function(Snapshot, SnapshotOptimizationValue): void} set set value * @param {boolean=} useStartTime use the start time of snapshots - * @param {boolean=} isSet value is an Set instead of a Map + * @param {U=} isSet value is an Set instead of a Map */ - constructor(has, get, set, useStartTime = true, isSet = false) { + constructor( + has, + get, + set, + useStartTime = true, + isSet = /** @type {U} */ (false) + ) { this._has = has; this._get = get; this._set = set; this._useStartTime = useStartTime; + /** @type {U} */ this._isSet = isSet; /** @type {Map} */ this._map = new Map(); @@ -565,8 +665,12 @@ class SnapshotOptimization { * @returns {void} */ const storeOptimizationEntry = entry => { - for (const path of entry.snapshotContent) { - const old = this._map.get(path); + for (const path of /** @type {SnapshotContent} */ ( + entry.snapshotContent + )) { + const old = + /** @type {SnapshotOptimizationEntry} */ + (this._map.get(path)); if (old.shared < entry.shared) { this._map.set(path, entry); } @@ -614,8 +718,12 @@ class SnapshotOptimization { continue; } const nonSharedFiles = new Set(); - const snapshotContent = optimizationEntry.snapshotContent; - const snapshotEntries = this._get(snapshot); + const snapshotContent = + /** @type {NonNullable} */ + (optimizationEntry.snapshotContent); + const snapshotEntries = + /** @type {SnapshotOptimizationValue} */ + (this._get(snapshot)); for (const path of snapshotContent) { if (!capturedFiles.has(path)) { if (!snapshotEntries.has(path)) { @@ -663,7 +771,10 @@ class SnapshotOptimization { if (this._useStartTime) { commonSnapshot.setMergedStartTime(newSnapshot.startTime, snapshot); } - this._set(commonSnapshot, commonMap); + this._set( + commonSnapshot, + /** @type {SnapshotOptimizationValue} */ (commonMap) + ); newSnapshot.addChild(commonSnapshot); snapshot.addChild(commonSnapshot); // Create optimization entry @@ -720,7 +831,11 @@ class SnapshotOptimization { if (this._useStartTime) { commonSnapshot.setMergedStartTime(newSnapshot.startTime, snapshot); } - this._set(commonSnapshot, commonMap); + this._set( + commonSnapshot, + /** @type {SnapshotOptimizationValue} */ + (commonMap) + ); newSnapshot.addChild(commonSnapshot); snapshot.addChild(commonSnapshot); // Remove files from snapshot @@ -746,7 +861,7 @@ class SnapshotOptimization { /** * @param {string} str input - * @returns {TODO} result + * @returns {string} result */ const parseString = str => { if (str[0] === "'" || str[0] === "`") @@ -768,13 +883,14 @@ const applyMtime = mtime => { /** * @template T * @template K - * @param {Map} a source map - * @param {Map} b joining map + * @param {Map | undefined} a source map + * @param {Map | undefined} b joining map * @returns {Map} joined map */ const mergeMaps = (a, b) => { - if (!b || b.size === 0) return a; - if (!a || a.size === 0) return b; + if (!b || b.size === 0) return /** @type {Map} */ (a); + if (!a || a.size === 0) return /** @type {Map} */ (b); + /** @type {Map} */ const map = new Map(a); for (const [key, value] of b) { map.set(key, value); @@ -784,13 +900,14 @@ const mergeMaps = (a, b) => { /** * @template T - * @param {Set} a source map - * @param {Set} b joining map + * @param {Set | undefined} a source map + * @param {Set | undefined} b joining map * @returns {Set} joined map */ const mergeSets = (a, b) => { - if (!b || b.size === 0) return a; - if (!a || a.size === 0) return b; + if (!b || b.size === 0) return /** @type {Set} */ (a); + if (!a || a.size === 0) return /** @type {Set} */ (b); + /** @type {Set} */ const map = new Set(a); for (const item of b) { map.add(item); @@ -912,6 +1029,8 @@ const addAll = (source, target) => { for (const key of source) target.add(key); }; +/** @typedef {Set} LoggedPaths */ + /** * Used to access information about the filesystem in a cached way */ @@ -938,9 +1057,10 @@ class FileSystemInfo { this.fs = fs; this.logger = logger; this._remainingLogs = logger ? 40 : 0; + /** @type {LoggedPaths | undefined} */ this._loggedPaths = logger ? new Set() : undefined; this._hashFunction = hashFunction; - /** @type {WeakMap} */ + /** @type {WeakMap} */ this._snapshotCache = new WeakMap(); this._fileTimestampsOptimization = new SnapshotOptimization( s => s.hasFileTimestamps(), @@ -1021,37 +1141,37 @@ class FileSystemInfo { this._contextTshs = new Map(); /** @type {Map} */ this._managedItems = new Map(); - /** @type {AsyncQueue} */ + /** @type {AsyncQueue} */ this.fileTimestampQueue = new AsyncQueue({ name: "file timestamp", parallelism: 30, processor: this._readFileTimestamp.bind(this) }); - /** @type {AsyncQueue} */ + /** @type {AsyncQueue} */ this.fileHashQueue = new AsyncQueue({ name: "file hash", parallelism: 10, processor: this._readFileHash.bind(this) }); - /** @type {AsyncQueue} */ + /** @type {AsyncQueue} */ this.contextTimestampQueue = new AsyncQueue({ name: "context timestamp", parallelism: 2, processor: this._readContextTimestamp.bind(this) }); - /** @type {AsyncQueue} */ + /** @type {AsyncQueue} */ this.contextHashQueue = new AsyncQueue({ name: "context hash", parallelism: 2, processor: this._readContextHash.bind(this) }); - /** @type {AsyncQueue} */ + /** @type {AsyncQueue} */ this.contextTshQueue = new AsyncQueue({ name: "context hash and timestamp", parallelism: 2, processor: this._readContextTimestampAndHash.bind(this) }); - /** @type {AsyncQueue} */ + /** @type {AsyncQueue} */ this.managedItemQueue = new AsyncQueue({ name: "managed item info", parallelism: 10, @@ -1196,11 +1316,14 @@ class FileSystemInfo { */ _log(path, reason, ...args) { const key = path + reason; - if (this._loggedPaths.has(key)) return; - this._loggedPaths.add(key); - this.logger.debug(`${path} invalidated because ${reason}`, ...args); + const loggedPaths = /** @type {LoggedPaths} */ (this._loggedPaths); + if (loggedPaths.has(key)) return; + loggedPaths.add(key); + /** @type {Logger} */ + (this.logger).debug(`${path} invalidated because ${reason}`, ...args); if (--this._remainingLogs === 0) { - this.logger.debug( + /** @type {Logger} */ + (this.logger).debug( "Logging limit has been reached and no further logging will be emitted by FileSystemInfo" ); } @@ -1284,10 +1407,15 @@ class FileSystemInfo { if (cache === "ignore") return callback(null, "ignore"); const resolved = getResolvedTimestamp(cache); if (resolved !== undefined) return callback(null, resolved); - return this._resolveContextTimestamp(cache, callback); + return this._resolveContextTimestamp( + /** @type {ResolvedContextFileSystemInfoEntry} */ + (cache), + callback + ); } - this.contextTimestampQueue.add(path, (err, entry) => { + this.contextTimestampQueue.add(path, (err, _entry) => { if (err) return callback(err); + const entry = /** @type {ContextFileSystemInfoEntry} */ (_entry); const resolved = getResolvedTimestamp(entry); if (resolved !== undefined) return callback(null, resolved); this._resolveContextTimestamp(entry, callback); @@ -1329,8 +1457,9 @@ class FileSystemInfo { return callback(null, /** @type {string} */ (resolved)); return this._resolveContextHash(cache, callback); } - this.contextHashQueue.add(path, (err, entry) => { + this.contextHashQueue.add(path, (err, _entry) => { if (err) return callback(err); + const entry = /** @type {ContextHash} */ (_entry); const resolved = getResolvedHash(entry); if (resolved !== undefined) return callback(null, /** @type {string} */ (resolved)); @@ -1351,7 +1480,7 @@ class FileSystemInfo { /** * @param {string} path context path - * @param {function((WebpackError | null)=, ResolvedContextTimestampAndHash=): void} callback callback function + * @param {function((WebpackError | null)=, (ResolvedContextTimestampAndHash | null)=): void} callback callback function * @returns {void} */ getContextTsh(path, callback) { @@ -1361,8 +1490,9 @@ class FileSystemInfo { if (resolved !== undefined) return callback(null, resolved); return this._resolveContextTsh(cache, callback); } - this.contextTshQueue.add(path, (err, entry) => { + this.contextTshQueue.add(path, (err, _entry) => { if (err) return callback(err); + const entry = /** @type {ContextTimestampAndHash} */ (_entry); const resolved = getResolvedTimestamp(entry); if (resolved !== undefined) return callback(null, resolved); this._resolveContextTsh(entry, callback); @@ -1443,11 +1573,17 @@ class FileSystemInfo { missingDependencies: resolveMissing }; /** - * @param {string} expected expected result + * @param {undefined | boolean | string} expected expected result * @returns {string} expected result */ const expectedToString = expected => expected ? ` (expected ${expected})` : ""; + /** @typedef {{ type: JobType, context: string | undefined, path: string, issuer: Job | undefined, expected: undefined | boolean | string }} Job */ + + /** + * @param {Job} job job + * @returns {`resolve commonjs file ${string}${string}`|`resolve esm file ${string}${string}`|`resolve esm ${string}${string}`|`resolve directory ${string}`|`file ${string}`|`unknown ${string} ${string}`|`resolve commonjs ${string}${string}`|`directory ${string}`|`file dependencies ${string}`|`directory dependencies ${string}`} result + */ const jobToString = job => { switch (job.type) { case RBDT_RESOLVE_CJS: @@ -1477,99 +1613,129 @@ class FileSystemInfo { } return `unknown ${job.type} ${job.path}`; }; + /** + * @param {Job} job job + * @returns {string} string value + */ const pathToString = job => { let result = ` at ${jobToString(job)}`; - job = job.issuer; + /** @type {Job | undefined} */ + (job) = job.issuer; while (job !== undefined) { result += `\n at ${jobToString(job)}`; - job = job.issuer; + job = /** @type {Job} */ (job.issuer); } return result; }; + const logger = /** @type {Logger} */ (this.logger); processAsyncTree( - Array.from(deps, dep => ({ - type: RBDT_RESOLVE_CJS, - context, - path: dep, - expected: undefined, - issuer: undefined - })), + Array.from( + deps, + dep => + /** @type {Job} */ ({ + type: RBDT_RESOLVE_CJS, + context, + path: dep, + expected: undefined, + issuer: undefined + }) + ), 20, (job, push, callback) => { const { type, context, path, expected } = job; + /** + * @param {string} path path + * @returns {void} + */ const resolveDirectory = path => { const key = `d\n${context}\n${path}`; if (resolveResults.has(key)) { return callback(); } resolveResults.set(key, undefined); - resolveContext(context, path, resolverContext, (err, _, result) => { - if (err) { - if (expected === false) { - resolveResults.set(key, false); - return callback(); - } - invalidResolveResults.add(key); - err.message += `\nwhile resolving '${path}' in ${context} to a directory`; - return callback(err); - } - const resultPath = result.path; - resolveResults.set(key, resultPath); - push({ - type: RBDT_DIRECTORY, - context: undefined, - path: /** @type {string} */ (resultPath), - expected: undefined, - issuer: job - }); - callback(); - }); - }; - const resolveFile = (path, symbol, resolve) => { - const key = `${symbol}\n${context}\n${path}`; - if (resolveResults.has(key)) { - return callback(); - } - resolveResults.set(key, undefined); - resolve(context, path, resolverContext, (err, _, result) => { - if (typeof expected === "string") { - if (!err && result && result.path === expected) { - resolveResults.set(key, result.path); - } else { - invalidResolveResults.add(key); - /** @type {Logger} */ - (this.logger).warn( - `Resolving '${path}' in ${context} for build dependencies doesn't lead to expected result '${expected}', but to '${ - err || (result && result.path) - }' instead. Resolving dependencies are ignored for this path.\n${pathToString( - job - )}` - ); - } - } else { + resolveContext( + /** @type {string} */ (context), + path, + resolverContext, + (err, _, result) => { if (err) { if (expected === false) { resolveResults.set(key, false); return callback(); } invalidResolveResults.add(key); - err.message += `\nwhile resolving '${path}' in ${context} as file\n${pathToString( - job - )}`; + err.message += `\nwhile resolving '${path}' in ${context} to a directory`; return callback(err); } - const resultPath = result.path; + const resultPath = /** @type {ResolveRequest} */ (result).path; resolveResults.set(key, resultPath); push({ - type: RBDT_FILE, + type: RBDT_DIRECTORY, context: undefined, - path: resultPath, + path: /** @type {string} */ (resultPath), expected: undefined, issuer: job }); + callback(); } - callback(); - }); + ); + }; + /** + * @param {string} path path + * @param {("f" | "c" | "e")=} symbol symbol + * @param {(ResolveFunctionAsync)=} resolve resolve fn + * @returns {void} + */ + const resolveFile = (path, symbol, resolve) => { + const key = `${symbol}\n${context}\n${path}`; + if (resolveResults.has(key)) { + return callback(); + } + resolveResults.set(key, undefined); + /** @type {ResolveFunctionAsync} */ + (resolve)( + /** @type {string} */ (context), + path, + resolverContext, + (err, _, result) => { + if (typeof expected === "string") { + if (!err && result && result.path === expected) { + resolveResults.set(key, result.path); + } else { + invalidResolveResults.add(key); + logger.warn( + `Resolving '${path}' in ${context} for build dependencies doesn't lead to expected result '${expected}', but to '${ + err || (result && result.path) + }' instead. Resolving dependencies are ignored for this path.\n${pathToString( + job + )}` + ); + } + } else { + if (err) { + if (expected === false) { + resolveResults.set(key, false); + return callback(); + } + invalidResolveResults.add(key); + err.message += `\nwhile resolving '${path}' in ${context} as file\n${pathToString( + job + )}`; + return callback(err); + } + const resultPath = /** @type {ResolveRequest} */ (result).path; + resolveResults.set(key, resultPath); + push({ + type: RBDT_FILE, + context: undefined, + path: /** @type {string} */ (resultPath), + expected: undefined, + issuer: job + }); + } + callback(); + } + ); }; switch (type) { case RBDT_RESOLVE_CJS: { @@ -1612,7 +1778,8 @@ class FileSystemInfo { break; } files.add(path); - this.fs.realpath(path, (err, _realPath) => { + /** @type {NonNullable} */ + (this.fs.realpath)(path, (err, _realPath) => { if (err) return callback(err); const realPath = /** @type {string} */ (_realPath); if (realPath !== path) { @@ -1638,7 +1805,8 @@ class FileSystemInfo { break; } directories.add(path); - this.fs.realpath(path, (err, _realPath) => { + /** @type {NonNullable} */ + (this.fs.realpath)(path, (err, _realPath) => { if (err) return callback(err); const realPath = /** @type {string} */ (_realPath); if (realPath !== path) { @@ -1729,7 +1897,7 @@ class FileSystemInfo { } } else if (supportsEsm && /\.m?js$/.test(path)) { if (!this._warnAboutExperimentalEsmTracking) { - this.logger.log( + logger.log( "Node.js doesn't offer a (nice) way to introspect the ESM dependency graph yet.\n" + "Until a full solution is available webpack uses an experimental ESM tracking based on parsing.\n" + "As best effort webpack parses the ESM files to guess dependencies. But this can lead to expensive and incorrect tracking." @@ -1742,7 +1910,7 @@ class FileSystemInfo { if (err) return callback(err); try { const context = dirname(this.fs, path); - const source = content.toString(); + const source = /** @type {Buffer} */ (content).toString(); const [imports] = lexer.parse(source); for (const imp of imports) { try { @@ -1773,33 +1941,33 @@ class FileSystemInfo { issuer: job }); } catch (err1) { - this.logger.warn( + logger.warn( `Parsing of ${path} for build dependencies failed at 'import(${source.substring( imp.s, imp.e )})'.\n` + "Build dependencies behind this expression are ignored and might cause incorrect cache invalidation." ); - this.logger.debug(pathToString(job)); - this.logger.debug(err1.stack); + logger.debug(pathToString(job)); + logger.debug(/** @type {Error} */ (err1).stack); } } } catch (err2) { - this.logger.warn( + logger.warn( `Parsing of ${path} for build dependencies failed and all dependencies of this file are ignored, which might cause incorrect cache invalidation..` ); - this.logger.debug(pathToString(job)); - this.logger.debug(err2.stack); + logger.debug(pathToString(job)); + logger.debug(/** @type {Error} */ (err2).stack); } process.nextTick(callback); }); }, callback); break; } else { - this.logger.log( + logger.log( `Assuming ${path} has no dependencies as we were unable to assign it to any module system.` ); - this.logger.debug(pathToString(job)); + logger.debug(pathToString(job)); } process.nextTick(callback); break; @@ -1831,9 +1999,11 @@ class FileSystemInfo { resolveFiles.add(packageJson); let packageData; try { - packageData = JSON.parse(content.toString("utf-8")); + packageData = JSON.parse( + /** @type {Buffer} */ (content).toString("utf-8") + ); } catch (parseErr) { - return callback(parseErr); + return callback(/** @type {Error} */ (parseErr)); } const depsObject = packageData.dependencies; const optionalDepsObject = packageData.optionalDependencies; @@ -1907,7 +2077,7 @@ class FileSystemInfo { if (expectedResult === false) return callback(err ? undefined : INVALID); if (err) return callback(err); - const resultPath = result.path; + const resultPath = /** @type {ResolveRequest} */ (result).path; if (resultPath !== expectedResult) return callback(INVALID); callback(); }); @@ -1917,7 +2087,7 @@ class FileSystemInfo { if (expectedResult === false) return callback(err ? undefined : INVALID); if (err) return callback(err); - const resultPath = result.path; + const resultPath = /** @type {ResolveRequest} */ (result).path; if (resultPath !== expectedResult) return callback(INVALID); callback(); }); @@ -1927,7 +2097,7 @@ class FileSystemInfo { if (expectedResult === false) return callback(err ? undefined : INVALID); if (err) return callback(err); - const resultPath = result.path; + const resultPath = /** @type {ResolveRequest} */ (result).path; if (resultPath !== expectedResult) return callback(INVALID); callback(); }); @@ -1937,7 +2107,7 @@ class FileSystemInfo { if (expectedResult === false) return callback(err ? undefined : INVALID); if (err) return callback(err); - const resultPath = result.path; + const resultPath = /** @type {ResolveRequest} */ (result).path; if (resultPath !== expectedResult) return callback(INVALID); callback(); }); @@ -1969,33 +2139,33 @@ class FileSystemInfo { * @param {Iterable | null} directories all directories * @param {Iterable | null} missing all missing files or directories * @param {SnapshotOptions | null | undefined} options options object (for future extensions) - * @param {function((WebpackError | null)=, (Snapshot | null)=): void} callback callback function + * @param {function(WebpackError | null, Snapshot | null): void} callback callback function * @returns {void} */ createSnapshot(startTime, files, directories, missing, options, callback) { - /** @type {Map} */ + /** @type {FileTimestamps} */ const fileTimestamps = new Map(); - /** @type {Map} */ + /** @type {FileHashes} */ const fileHashes = new Map(); - /** @type {Map} */ + /** @type {FileTshs} */ const fileTshs = new Map(); - /** @type {Map} */ + /** @type {ContextTimestamps} */ const contextTimestamps = new Map(); - /** @type {Map} */ + /** @type {ContextHashes} */ const contextHashes = new Map(); - /** @type {Map} */ + /** @type {ContextTshs} */ const contextTshs = new Map(); - /** @type {Map} */ + /** @type {MissingExistence} */ const missingExistence = new Map(); - /** @type {Map} */ + /** @type {ManagedItemInfo} */ const managedItemInfo = new Map(); - /** @type {Set} */ + /** @type {ManagedFiles} */ const managedFiles = new Set(); - /** @type {Set} */ + /** @type {ManagedContexts} */ const managedContexts = new Set(); - /** @type {Set} */ + /** @type {ManagedMissing} */ const managedMissing = new Set(); - /** @type {Set} */ + /** @type {Children} */ const children = new Set(); const snapshot = new Snapshot(); @@ -2062,6 +2232,11 @@ class FileSystemInfo { callback(null, null); } }; + /** + * @param {string} path path + * @param {Set} managedSet managed set + * @returns {boolean} true when managed + */ const checkManaged = (path, managedSet) => { for (const unmanagedPath of this.unmanagedPathsRegExps) { if (unmanagedPath.test(path)) return false; @@ -2104,6 +2279,11 @@ class FileSystemInfo { } return false; }; + /** + * @param {Iterable} items items + * @param {Set} managedSet managed set + * @returns {Set} result + */ const captureNonManaged = (items, managedSet) => { const capturedItems = new Set(); for (const path of items) { @@ -2133,7 +2313,7 @@ class FileSystemInfo { } jobError(); } else { - fileTshs.set(path, entry); + fileTshs.set(path, /** @type {TimestampAndHash} */ (entry)); jobDone(); } }); @@ -2157,7 +2337,7 @@ class FileSystemInfo { } jobError(); } else { - fileHashes.set(path, entry); + fileHashes.set(path, /** @type {string} */ (entry)); jobDone(); } }); @@ -2183,7 +2363,11 @@ class FileSystemInfo { } jobError(); } else { - fileTimestamps.set(path, entry); + fileTimestamps.set( + path, + /** @type {FileSystemInfoEntry} */ + (entry) + ); jobDone(); } }); @@ -2195,13 +2379,16 @@ class FileSystemInfo { if (files) { processCapturedFiles(captureNonManaged(files, managedFiles)); } + /** + * @param {Set} capturedDirectories captured directories + */ const processCapturedDirectories = capturedDirectories => { switch (mode) { case 3: this._contextTshsOptimization.optimize(snapshot, capturedDirectories); for (const path of capturedDirectories) { const cache = this._contextTshs.get(path); - /** @type {ResolvedContextTimestampAndHash} */ + /** @type {ResolvedContextTimestampAndHash | null | undefined} */ let resolved; if ( cache !== undefined && @@ -2211,8 +2398,8 @@ class FileSystemInfo { } else { jobs++; /** - * @param {Error=} err error - * @param {ResolvedContextTimestampAndHash=} entry entry + * @param {(WebpackError | null)=} err error + * @param {(ResolvedContextTimestampAndHash | null)=} entry entry * @returns {void} */ const callback = (err, entry) => { @@ -2224,7 +2411,11 @@ class FileSystemInfo { } jobError(); } else { - contextTshs.set(path, entry); + contextTshs.set( + path, + /** @type {ResolvedContextTimestampAndHash | null} */ + (entry) + ); jobDone(); } }; @@ -2251,6 +2442,10 @@ class FileSystemInfo { contextHashes.set(path, resolved); } else { jobs++; + /** + * @param {(WebpackError | null)=} err err + * @param {string=} entry entry + */ const callback = (err, entry) => { if (err) { if (this.logger) { @@ -2260,7 +2455,7 @@ class FileSystemInfo { } jobError(); } else { - contextHashes.set(path, entry); + contextHashes.set(path, /** @type {string} */ (entry)); jobDone(); } }; @@ -2289,8 +2484,8 @@ class FileSystemInfo { } else { jobs++; /** - * @param {Error=} err error - * @param {ResolvedContextFileSystemInfoEntry=} entry entry + * @param {(Error | null)=} err error + * @param {(FileSystemInfoEntry | "ignore" | null)=} entry entry * @returns {void} */ const callback = (err, entry) => { @@ -2302,12 +2497,20 @@ class FileSystemInfo { } jobError(); } else { - contextTimestamps.set(path, entry); + contextTimestamps.set( + path, + /** @type {FileSystemInfoEntry | null} */ + (entry) + ); jobDone(); } }; if (cache !== undefined) { - this._resolveContextTimestamp(cache, callback); + this._resolveContextTimestamp( + /** @type {ContextFileSystemInfoEntry} */ + (cache), + callback + ); } else { this.getContextTimestamp(path, callback); } @@ -2321,6 +2524,9 @@ class FileSystemInfo { captureNonManaged(directories, managedContexts) ); } + /** + * @param {Set} capturedMissing captured missing + */ const processCapturedMissing = capturedMissing => { this._missingExistenceOptimization.optimize(snapshot, capturedMissing); for (const path of capturedMissing) { @@ -2380,6 +2586,10 @@ class FileSystemInfo { jobDone(); } else { // Fallback to normal snapshotting + /** + * @param {Set} set set + * @param {function(Set): void} fn fn + */ const process = (set, fn) => { if (set.size === 0) return; const captured = new Set(); @@ -2406,10 +2616,20 @@ class FileSystemInfo { */ mergeSnapshots(snapshot1, snapshot2) { const snapshot = new Snapshot(); - if (snapshot1.hasStartTime() && snapshot2.hasStartTime()) - snapshot.setStartTime(Math.min(snapshot1.startTime, snapshot2.startTime)); - else if (snapshot2.hasStartTime()) snapshot.startTime = snapshot2.startTime; - else if (snapshot1.hasStartTime()) snapshot.startTime = snapshot1.startTime; + if (snapshot1.hasStartTime() && snapshot2.hasStartTime()) { + snapshot.setStartTime( + Math.min( + /** @type {NonNullable} */ + (snapshot1.startTime), + /** @type {NonNullable} */ + (snapshot2.startTime) + ) + ); + } else if (snapshot2.hasStartTime()) { + snapshot.startTime = snapshot2.startTime; + } else if (snapshot1.hasStartTime()) { + snapshot.startTime = snapshot1.startTime; + } if (snapshot1.hasFileTimestamps() || snapshot2.hasFileTimestamps()) { snapshot.setFileTimestamps( mergeMaps(snapshot1.fileTimestamps, snapshot2.fileTimestamps) @@ -2521,6 +2741,10 @@ class FileSystemInfo { callback(null, false); } }; + /** + * @param {string} path path + * @param {WebpackError} err err + */ const invalidWithError = (path, err) => { if (this._remainingLogs > 0) { this._log(path, "error occurred: %s", err); @@ -2529,8 +2753,8 @@ class FileSystemInfo { }; /** * @param {string} path file path - * @param {string} current current hash - * @param {string} snap snapshot hash + * @param {string | null} current current hash + * @param {string | null} snap snapshot hash * @returns {boolean} true, if ok */ const checkHash = (path, current, snap) => { @@ -2565,40 +2789,38 @@ class FileSystemInfo { }; /** * @param {string} path file path - * @param {FileSystemInfoEntry} current current entry - * @param {FileSystemInfoEntry} snap entry from snapshot + * @param {FileSystemInfoEntry | null} c current entry + * @param {FileSystemInfoEntry | null} s entry from snapshot * @param {boolean} log log reason * @returns {boolean} true, if ok */ - const checkFile = (path, current, snap, log = true) => { - if (current === snap) return true; - if (!checkExistence(path, Boolean(current), Boolean(snap))) return false; - if (current) { + const checkFile = (path, c, s, log = true) => { + if (c === s) return true; + if (!checkExistence(path, Boolean(c), Boolean(s))) return false; + if (c) { // For existing items only - if (typeof startTime === "number" && current.safeTime > startTime) { + if (typeof startTime === "number" && c.safeTime > startTime) { // If a change happened after starting reading the item // this may no longer be valid if (log && this._remainingLogs > 0) { this._log( path, "it may have changed (%d) after the start time of the snapshot (%d)", - current.safeTime, + c.safeTime, startTime ); } return false; } - if ( - snap.timestamp !== undefined && - current.timestamp !== snap.timestamp - ) { + const snap = /** @type {FileSystemInfoEntry} */ (s); + if (snap.timestamp !== undefined && c.timestamp !== snap.timestamp) { // If we have a timestamp (it was a file or symlink) and it differs from current timestamp // it's invalid if (log && this._remainingLogs > 0) { this._log( path, "timestamps differ (%d != %d)", - current.timestamp, + c.timestamp, snap.timestamp ); } @@ -2609,32 +2831,33 @@ class FileSystemInfo { }; /** * @param {string} path file path - * @param {ResolvedContextFileSystemInfoEntry} current current entry - * @param {ResolvedContextFileSystemInfoEntry} snap entry from snapshot + * @param {ResolvedContextFileSystemInfoEntry | null} c current entry + * @param {ResolvedContextFileSystemInfoEntry | null} s entry from snapshot * @param {boolean} log log reason * @returns {boolean} true, if ok */ - const checkContext = (path, current, snap, log = true) => { - if (current === snap) return true; - if (!checkExistence(path, Boolean(current), Boolean(snap))) return false; - if (current) { + const checkContext = (path, c, s, log = true) => { + if (c === s) return true; + if (!checkExistence(path, Boolean(c), Boolean(s))) return false; + if (c) { // For existing items only - if (typeof startTime === "number" && current.safeTime > startTime) { + if (typeof startTime === "number" && c.safeTime > startTime) { // If a change happened after starting reading the item // this may no longer be valid if (log && this._remainingLogs > 0) { this._log( path, "it may have changed (%d) after the start time of the snapshot (%d)", - current.safeTime, + c.safeTime, startTime ); } return false; } + const snap = /** @type {ResolvedContextFileSystemInfoEntry} */ (s); if ( snap.timestampHash !== undefined && - current.timestampHash !== snap.timestampHash + c.timestampHash !== snap.timestampHash ) { // If we have a timestampHash (it was a directory) and it differs from current timestampHash // it's invalid @@ -2642,7 +2865,7 @@ class FileSystemInfo { this._log( path, "timestamps hashes differ (%s != %s)", - current.timestampHash, + c.timestampHash, snap.timestampHash ); } @@ -2652,11 +2875,16 @@ class FileSystemInfo { return true; }; if (snapshot.hasChildren()) { + /** + * @param {(WebpackError | null)=} err err + * @param {boolean=} result result + * @returns {void} + */ const childCallback = (err, result) => { if (err || !result) return invalid(); jobDone(); }; - for (const child of snapshot.children) { + for (const child of /** @type {Children} */ (snapshot.children)) { const cache = this._snapshotCache.get(child); if (cache !== undefined) { this._statTestedChildrenCached++; @@ -2678,7 +2906,9 @@ class FileSystemInfo { } } if (snapshot.hasFileTimestamps()) { - const { fileTimestamps } = snapshot; + const fileTimestamps = + /** @type {FileTimestamps} */ + (snapshot.fileTimestamps); this._statTestedEntries += fileTimestamps.size; for (const [path, ts] of fileTimestamps) { const cache = this._fileTimestamps.get(path); @@ -2691,7 +2921,13 @@ class FileSystemInfo { jobs++; this.fileTimestampQueue.add(path, (err, entry) => { if (err) return invalidWithError(path, err); - if (!checkFile(path, entry, ts)) { + if ( + !checkFile( + path, + /** @type {FileSystemInfoEntry | null} */ (entry), + ts + ) + ) { invalid(); } else { jobDone(); @@ -2702,7 +2938,7 @@ class FileSystemInfo { } /** * @param {string} path file path - * @param {string} hash hash + * @param {string | null} hash hash */ const processFileHashSnapshot = (path, hash) => { const cache = this._fileHashes.get(path); @@ -2714,7 +2950,7 @@ class FileSystemInfo { jobs++; this.fileHashQueue.add(path, (err, entry) => { if (err) return invalidWithError(path, err); - if (!checkHash(path, entry, hash)) { + if (!checkHash(path, /** @type {string} */ (entry), hash)) { invalid(); } else { jobDone(); @@ -2723,14 +2959,14 @@ class FileSystemInfo { } }; if (snapshot.hasFileHashes()) { - const { fileHashes } = snapshot; + const fileHashes = /** @type {FileHashes} */ (snapshot.fileHashes); this._statTestedEntries += fileHashes.size; for (const [path, hash] of fileHashes) { processFileHashSnapshot(path, hash); } } if (snapshot.hasFileTshs()) { - const { fileTshs } = snapshot; + const fileTshs = /** @type {FileTshs} */ (snapshot.fileTshs); this._statTestedEntries += fileTshs.size; for (const [path, tsh] of fileTshs) { if (typeof tsh === "string") { @@ -2745,7 +2981,15 @@ class FileSystemInfo { jobs++; this.fileTimestampQueue.add(path, (err, entry) => { if (err) return invalidWithError(path, err); - if (!checkFile(path, entry, tsh, false)) { + if ( + !checkFile( + path, + /** @type {FileSystemInfoEntry | null} */ + (entry), + tsh, + false + ) + ) { processFileHashSnapshot(path, tsh && tsh.hash); } jobDone(); @@ -2755,7 +2999,9 @@ class FileSystemInfo { } } if (snapshot.hasContextTimestamps()) { - const { contextTimestamps } = snapshot; + const contextTimestamps = + /** @type {ContextTimestamps} */ + (snapshot.contextTimestamps); this._statTestedEntries += contextTimestamps.size; for (const [path, ts] of contextTimestamps) { const cache = this._contextTimestamps.get(path); @@ -2772,26 +3018,41 @@ class FileSystemInfo { } else { jobs++; /** - * @param {Error=} err error - * @param {ResolvedContextFileSystemInfoEntry=} entry entry + * @param {(WebpackError | null)=} err error + * @param {(ResolvedContextFileSystemInfoEntry | "ignore" | null)=} entry entry * @returns {void} */ const callback = (err, entry) => { if (err) return invalidWithError(path, err); - if (!checkContext(path, entry, ts)) { + if ( + !checkContext( + path, + /** @type {ResolvedContextFileSystemInfoEntry | null} */ + (entry), + ts + ) + ) { invalid(); } else { jobDone(); } }; if (cache !== undefined) { - this._resolveContextTimestamp(cache, callback); + this._resolveContextTimestamp( + /** @type {ContextFileSystemInfoEntry} */ + (cache), + callback + ); } else { this.getContextTimestamp(path, callback); } } } } + /** + * @param {string} path path + * @param {string | null} hash hash + */ const processContextHashSnapshot = (path, hash) => { const cache = this._contextHashes.get(path); let resolved; @@ -2804,9 +3065,14 @@ class FileSystemInfo { } } else { jobs++; + /** + * @param {(WebpackError | null)=} err err + * @param {string=} entry entry + * @returns {void} + */ const callback = (err, entry) => { if (err) return invalidWithError(path, err); - if (!checkHash(path, entry, hash)) { + if (!checkHash(path, /** @type {string} */ (entry), hash)) { invalid(); } else { jobDone(); @@ -2820,14 +3086,16 @@ class FileSystemInfo { } }; if (snapshot.hasContextHashes()) { - const { contextHashes } = snapshot; + const contextHashes = + /** @type {ContextHashes} */ + (snapshot.contextHashes); this._statTestedEntries += contextHashes.size; for (const [path, hash] of contextHashes) { processContextHashSnapshot(path, hash); } } if (snapshot.hasContextTshs()) { - const { contextTshs } = snapshot; + const contextTshs = /** @type {ContextTshs} */ (snapshot.contextTshs); this._statTestedEntries += contextTshs.size; for (const [path, tsh] of contextTshs) { if (typeof tsh === "string") { @@ -2840,25 +3108,46 @@ class FileSystemInfo { cache !== undefined && (resolved = getResolvedTimestamp(cache)) !== undefined ) { - if (!checkContext(path, resolved, tsh, false)) { + if ( + !checkContext( + path, + /** @type {ResolvedContextFileSystemInfoEntry | null} */ + (resolved), + tsh, + false + ) + ) { processContextHashSnapshot(path, tsh && tsh.hash); } } else { jobs++; /** - * @param {Error=} err error - * @param {ResolvedContextFileSystemInfoEntry=} entry entry + * @param {(WebpackError | null)=} err error + * @param {(ResolvedContextFileSystemInfoEntry | "ignore" | null)=} entry entry * @returns {void} */ const callback = (err, entry) => { if (err) return invalidWithError(path, err); - if (!checkContext(path, entry, tsh, false)) { + if ( + !checkContext( + path, + // TODO: test with `"ignore"` + /** @type {ResolvedContextFileSystemInfoEntry | null} */ + (entry), + tsh, + false + ) + ) { processContextHashSnapshot(path, tsh && tsh.hash); } jobDone(); }; if (cache !== undefined) { - this._resolveContextTimestamp(cache, callback); + this._resolveContextTimestamp( + /** @type {ContextFileSystemInfoEntry} */ + (cache), + callback + ); } else { this.getContextTimestamp(path, callback); } @@ -2867,7 +3156,9 @@ class FileSystemInfo { } } if (snapshot.hasMissingExistence()) { - const { missingExistence } = snapshot; + const missingExistence = + /** @type {MissingExistence} */ + (snapshot.missingExistence); this._statTestedEntries += missingExistence.size; for (const [path, existence] of missingExistence) { const cache = this._fileTimestamps.get(path); @@ -2893,7 +3184,9 @@ class FileSystemInfo { } } if (snapshot.hasManagedItemInfo()) { - const { managedItemInfo } = snapshot; + const managedItemInfo = + /** @type {ManagedItemInfo} */ + (snapshot.managedItemInfo); this._statTestedEntries += managedItemInfo.size; for (const [path, info] of managedItemInfo) { const cache = this._managedItems.get(path); @@ -2906,7 +3199,7 @@ class FileSystemInfo { jobs++; this.managedItemQueue.add(path, (err, entry) => { if (err) return invalidWithError(path, err); - if (!checkHash(path, entry, info)) { + if (!checkHash(path, /** @type {string} */ (entry), info)) { invalid(); } else { jobDone(); @@ -2928,17 +3221,21 @@ class FileSystemInfo { } } + /** + * @type {Processor} + * @private + */ _readFileTimestamp(path, callback) { - this.fs.stat(path, (err, stat) => { + this.fs.stat(path, (err, _stat) => { if (err) { if (err.code === "ENOENT") { this._fileTimestamps.set(path, null); this._cachedDeprecatedFileTimestamps = undefined; return callback(null, null); } - return callback(err); + return callback(/** @type {WebpackError} */ (err)); } - + const stat = /** @type {IStats} */ (_stat); let ts; if (stat.isDirectory()) { ts = { @@ -2963,6 +3260,10 @@ class FileSystemInfo { }); } + /** + * @type {Processor} + * @private + */ _readFileHash(path, callback) { this.fs.readFile(path, (err, content) => { if (err) { @@ -2975,11 +3276,12 @@ class FileSystemInfo { return callback(null, null); } if (err.code === "ERR_FS_FILE_TOO_LARGE") { - this.logger.warn(`Ignoring ${path} for hashing as it's very large`); + /** @type {Logger} */ + (this.logger).warn(`Ignoring ${path} for hashing as it's very large`); this._fileHashes.set(path, "too large"); return callback(null, "too large"); } - return callback(err); + return callback(/** @type {WebpackError} */ (err)); } const hash = createHash(this._hashFunction); @@ -2994,27 +3296,38 @@ class FileSystemInfo { }); } + /** + * @param {string} path path + * @param {function(WebpackError | null, TimestampAndHash=) : void} callback callback + * @private + */ _getFileTimestampAndHash(path, callback) { + /** + * @param {string} hash hash + * @returns {void} + */ const continueWithHash = hash => { const cache = this._fileTimestamps.get(path); if (cache !== undefined) { if (cache !== "ignore") { + /** @type {TimestampAndHash} */ const result = { - ...cache, + .../** @type {FileSystemInfoEntry} */ (cache), hash }; this._fileTshs.set(path, result); return callback(null, result); } this._fileTshs.set(path, hash); - return callback(null, hash); + return callback(null, /** @type {TODO} */ (hash)); } this.fileTimestampQueue.add(path, (err, entry) => { if (err) { return callback(err); } + /** @type {TimestampAndHash} */ const result = { - ...entry, + .../** @type {FileSystemInfoEntry} */ (entry), hash }; this._fileTshs.set(path, result); @@ -3024,13 +3337,13 @@ class FileSystemInfo { const cache = this._fileHashes.get(path); if (cache !== undefined) { - continueWithHash(cache); + continueWithHash(/** @type {string} */ (cache)); } else { this.fileHashQueue.add(path, (err, entry) => { if (err) { return callback(err); } - continueWithHash(entry); + continueWithHash(/** @type {string} */ (entry)); }); } } @@ -3042,9 +3355,9 @@ class FileSystemInfo { * @param {string} options.path path * @param {function(string): ItemType} options.fromImmutablePath called when context item is an immutable path * @param {function(string): ItemType} options.fromManagedItem called when context item is a managed path - * @param {function(string, string, function(Error=, ItemType=): void): void} options.fromSymlink called when context item is a symlink - * @param {function(string, IStats, function(Error=, ItemType=): void): void} options.fromFile called when context item is a file - * @param {function(string, IStats, function(Error=, ItemType=): void): void} options.fromDirectory called when context item is a directory + * @param {function(string, string, function((WebpackError | null)=, ItemType=): void): void} options.fromSymlink called when context item is a symlink + * @param {function(string, IStats, function((WebpackError | null)=, (ItemType | null)=): void): void} options.fromFile called when context item is a file + * @param {function(string, IStats, function((WebpackError | null)=, ItemType=): void): void} options.fromDirectory called when context item is a directory * @param {function(string[], ItemType[]): T} options.reduce called from all context items * @param {function((Error | null)=, (T | null)=): void} callback callback */ @@ -3095,7 +3408,10 @@ class FileSystemInfo { // construct timestampHash from managed info return this.managedItemQueue.add(managedItem, (err, info) => { if (err) return callback(err); - return callback(null, fromManagedItem(info)); + return callback( + null, + fromManagedItem(/** @type {string} */ (info)) + ); }); } } @@ -3107,15 +3423,20 @@ class FileSystemInfo { // construct timestampHash from managed info return this.managedItemQueue.add(managedItem, (err, info) => { if (err) return callback(err); - return callback(null, fromManagedItem(info)); + return callback( + null, + fromManagedItem(/** @type {string} */ (info)) + ); }); } } } - lstatReadlinkAbsolute(this.fs, child, (err, stat) => { + lstatReadlinkAbsolute(this.fs, child, (err, _stat) => { if (err) return callback(err); + const stat = /** @type {IStats | string} */ (_stat); + if (typeof stat === "string") { return fromSymlink(child, stat, callback); } @@ -3131,27 +3452,37 @@ class FileSystemInfo { }, (err, results) => { if (err) return callback(err); - const result = reduce(files, results); + const result = reduce(files, /** @type {ItemType[]} */ (results)); callback(null, result); } ); }); } + /** + * @type {Processor} + * @private + */ _readContextTimestamp(path, callback) { this._readContext( { path, - fromImmutablePath: () => null, + fromImmutablePath: () => + /** @type {ContextFileSystemInfoEntry | FileSystemInfoEntry | "ignore" | null} */ + (null), fromManagedItem: info => ({ safeTime: 0, timestampHash: info }), fromSymlink: (file, target, callback) => { - callback(null, { - timestampHash: target, - symlinks: new Set([target]) - }); + callback( + null, + /** @type {ContextFileSystemInfoEntry} */ + ({ + timestampHash: target, + symlinks: new Set([target]) + }) + ); }, fromFile: (file, stat, callback) => { // Prefer the cached value over our new stat to report consistent results @@ -3163,6 +3494,7 @@ class FileSystemInfo { if (mtime) applyMtime(mtime); + /** @type {FileSystemInfoEntry} */ const ts = { safeTime: mtime ? mtime + FS_ACCURACY : Infinity, timestamp: mtime @@ -3186,21 +3518,36 @@ class FileSystemInfo { for (const file of files) hash.update(file); let safeTime = 0; - for (const entry of tsEntries) { - if (!entry) { + for (const _e of tsEntries) { + if (!_e) { hash.update("n"); continue; } - if (entry.timestamp) { + const entry = + /** @type {FileSystemInfoEntry | ContextFileSystemInfoEntry} */ + (_e); + if (/** @type {FileSystemInfoEntry} */ (entry).timestamp) { hash.update("f"); - hash.update(`${entry.timestamp}`); - } else if (entry.timestampHash) { + hash.update( + `${/** @type {FileSystemInfoEntry} */ (entry).timestamp}` + ); + } else if ( + /** @type {ContextFileSystemInfoEntry} */ (entry).timestampHash + ) { hash.update("d"); - hash.update(`${entry.timestampHash}`); + hash.update( + `${/** @type {ContextFileSystemInfoEntry} */ (entry).timestampHash}` + ); } - if (entry.symlinks !== undefined) { + if ( + /** @type {ContextFileSystemInfoEntry} */ + (entry).symlinks !== undefined + ) { if (symlinks === undefined) symlinks = new Set(); - addAll(entry.symlinks, symlinks); + addAll( + /** @type {ContextFileSystemInfoEntry} */ (entry).symlinks, + symlinks + ); } if (entry.safeTime) { safeTime = Math.max(safeTime, entry.safeTime); @@ -3208,7 +3555,7 @@ class FileSystemInfo { } const digest = /** @type {string} */ (hash.digest("hex")); - + /** @type {ContextFileSystemInfoEntry} */ const result = { safeTime, timestampHash: digest @@ -3218,7 +3565,7 @@ class FileSystemInfo { } }, (err, result) => { - if (err) return callback(err); + if (err) return callback(/** @type {WebpackError} */ (err)); this._contextTimestamps.set(path, result); this._cachedDeprecatedContextTimestamps = undefined; @@ -3229,7 +3576,7 @@ class FileSystemInfo { /** * @param {ContextFileSystemInfoEntry} entry entry - * @param {function((Error | null)=, ResolvedContextFileSystemInfoEntry=): void} callback callback + * @param {function((WebpackError | null)=, (ResolvedContextFileSystemInfoEntry | "ignore" | null)=): void} callback callback * @returns {void} */ _resolveContextTimestamp(entry, callback) { @@ -3237,13 +3584,13 @@ class FileSystemInfo { const hashes = []; let safeTime = 0; processAsyncTree( - entry.symlinks, + /** @type {NonNullable} */ (entry.symlinks), 10, (target, push, callback) => { this._getUnresolvedContextTimestamp(target, (err, entry) => { if (err) return callback(err); if (entry && entry !== "ignore") { - hashes.push(entry.timestampHash); + hashes.push(/** @type {string} */ (entry.timestampHash)); if (entry.safeTime) { safeTime = Math.max(safeTime, entry.safeTime); } @@ -3255,9 +3602,9 @@ class FileSystemInfo { }); }, err => { - if (err) return callback(err); + if (err) return callback(/** @type {WebpackError} */ (err)); const hash = createHash(this._hashFunction); - hash.update(entry.timestampHash); + hash.update(/** @type {string} */ (entry.timestampHash)); if (entry.safeTime) { safeTime = Math.max(safeTime, entry.safeTime); } @@ -3276,17 +3623,26 @@ class FileSystemInfo { ); } + /** + * @type {Processor} + * @private + */ _readContextHash(path, callback) { this._readContext( { path, - fromImmutablePath: () => "", + fromImmutablePath: () => + /** @type {ContextHash} */ (/** @type {unknown} */ ("")), fromManagedItem: info => info || "", fromSymlink: (file, target, callback) => { - callback(null, { - hash: target, - symlinks: new Set([target]) - }); + callback( + null, + /** @type {ContextHash} */ + ({ + hash: target, + symlinks: new Set([target]) + }) + ); }, fromFile: (file, stat, callback) => this.getFileHash(file, (err, hash) => { @@ -3321,6 +3677,7 @@ class FileSystemInfo { } } + /** @type {ContextHash} */ const result = { hash: /** @type {string} */ (hash.digest("hex")) }; @@ -3328,8 +3685,9 @@ class FileSystemInfo { return result; } }, - (err, result) => { - if (err) return callback(err); + (err, _result) => { + if (err) return callback(/** @type {WebpackError} */ (err)); + const result = /** @type {ContextHash} */ (_result); this._contextHashes.set(path, result); return callback(null, result); } @@ -3338,14 +3696,14 @@ class FileSystemInfo { /** * @param {ContextHash} entry context hash - * @param {function((WebpackError | null)=, string=): void} callback callback + * @param {function(WebpackError | null, string=): void} callback callback * @returns {void} */ _resolveContextHash(entry, callback) { /** @type {string[]} */ const hashes = []; processAsyncTree( - entry.symlinks, + /** @type {NonNullable} */ (entry.symlinks), 10, (target, push, callback) => { this._getUnresolvedContextHash(target, (err, hash) => { @@ -3375,15 +3733,19 @@ class FileSystemInfo { ); } + /** + * @type {Processor} + * @private + */ _readContextTimestampAndHash(path, callback) { + /** + * @param {ContextFileSystemInfoEntry | "ignore" | null} timestamp timestamp + * @param {ContextHash} hash hash + */ const finalize = (timestamp, hash) => { const result = - timestamp === "ignore" - ? hash - : { - ...timestamp, - ...hash - }; + /** @type {ContextTimestampAndHash} */ + (timestamp === "ignore" ? hash : { ...timestamp, ...hash }); this._contextTshs.set(path, result); callback(null, result); }; @@ -3395,13 +3757,17 @@ class FileSystemInfo { } else { this.contextTimestampQueue.add(path, (err, entry) => { if (err) return callback(err); - finalize(entry, cachedHash); + finalize( + /** @type {ContextFileSystemInfoEntry} */ + (entry), + cachedHash + ); }); } } else if (cachedTimestamp !== undefined) { this.contextHashQueue.add(path, (err, entry) => { if (err) return callback(err); - finalize(cachedTimestamp, entry); + finalize(cachedTimestamp, /** @type {ContextHash} */ (entry)); }); } else { this._readContext( @@ -3470,9 +3836,10 @@ class FileSystemInfo { if (entry.safeTime) { safeTime = Math.max(safeTime, entry.safeTime); } - hash.update(entry.hash); + hash.update(/** @type {string} */ (entry.hash)); } + /** @type {ContextTimestampAndHash} */ const result = { safeTime, timestampHash: /** @type {string} */ (tsHash.digest("hex")), @@ -3482,8 +3849,9 @@ class FileSystemInfo { return result; } }, - (err, result) => { - if (err) return callback(err); + (err, _result) => { + if (err) return callback(/** @type {WebpackError} */ (err)); + const result = /** @type {ContextTimestampAndHash} */ (_result); this._contextTshs.set(path, result); return callback(null, result); } @@ -3493,7 +3861,7 @@ class FileSystemInfo { /** * @param {ContextTimestampAndHash} entry entry - * @param {function((Error | null)=, ResolvedContextTimestampAndHash=): void} callback callback + * @param {ProcessorCallback} callback callback * @returns {void} */ _resolveContextTsh(entry, callback) { @@ -3503,7 +3871,7 @@ class FileSystemInfo { const tsHashes = []; let safeTime = 0; processAsyncTree( - entry.symlinks, + /** @type {NonNullable} */ (entry.symlinks), 10, (target, push, callback) => { this._getUnresolvedContextTsh(target, (err, entry) => { @@ -3522,7 +3890,7 @@ class FileSystemInfo { }); }, err => { - if (err) return callback(err); + if (err) return callback(/** @type {WebpackError} */ (err)); const hash = createHash(this._hashFunction); const tsHash = createHash(this._hashFunction); hash.update(entry.hash); @@ -3550,13 +3918,17 @@ class FileSystemInfo { ); } + /** + * @type {Processor>} + * @private + */ _getManagedItemDirectoryInfo(path, callback) { this.fs.readdir(path, (err, elements) => { if (err) { if (err.code === "ENOENT" || err.code === "ENOTDIR") { return callback(null, EMPTY_SET); } - return callback(err); + return callback(/** @type {WebpackError} */ (err)); } const set = new Set( /** @type {string[]} */ (elements).map(element => @@ -3567,13 +3939,17 @@ class FileSystemInfo { }); } + /** + * @type {Processor} + * @private + */ _getManagedItemInfo(path, callback) { const dir = dirname(this.fs, path); this.managedItemDirectoryQueue.add(dir, (err, elements) => { if (err) { return callback(err); } - if (!elements.has(path)) { + if (!(/** @type {Set} */ (elements).has(path))) { // file or directory doesn't exist this._managedItems.set(path, "*missing"); return callback(null, "*missing"); @@ -3598,28 +3974,29 @@ class FileSystemInfo { this.fs.readdir(path, (err, elements) => { if ( !err && - elements.length === 1 && - elements[0] === "node_modules" + /** @type {string[]} */ (elements).length === 1 && + /** @type {string[]} */ (elements)[0] === "node_modules" ) { // This is only a grouping folder e.g. used by yarn // we are only interested in existence of this special directory this._managedItems.set(path, "*nested"); return callback(null, "*nested"); } - this.logger.warn( + /** @type {Logger} */ + (this.logger).warn( `Managed item ${path} isn't a directory or doesn't contain a package.json (see snapshot.managedPaths option)` ); return callback(); }); return; } - return callback(err); + return callback(/** @type {WebpackError} */ (err)); } let data; try { data = JSON.parse(/** @type {Buffer} */ (content).toString("utf-8")); } catch (parseErr) { - return callback(parseErr); + return callback(/** @type {WebpackError} */ (parseErr)); } if (!data.name) { /** @type {Logger} */ diff --git a/lib/HotModuleReplacementPlugin.js b/lib/HotModuleReplacementPlugin.js index dc0b686b8..9b1810180 100644 --- a/lib/HotModuleReplacementPlugin.js +++ b/lib/HotModuleReplacementPlugin.js @@ -44,8 +44,10 @@ const { /** @typedef {import("estree").CallExpression} CallExpression */ /** @typedef {import("estree").Expression} Expression */ +/** @typedef {import("../declarations/WebpackOptions").OutputNormalized} OutputNormalized */ /** @typedef {import("./Chunk")} Chunk */ /** @typedef {import("./Chunk").ChunkId} ChunkId */ +/** @typedef {import("./ChunkGraph").ModuleId} ModuleId */ /** @typedef {import("./Compilation").AssetInfo} AssetInfo */ /** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */ @@ -62,7 +64,8 @@ const { * @property {SyncBailHook<[TODO, string[]], void>} hotAcceptWithoutCallback */ -/** @typedef {Map, removedChunkIds: Set, removedModules: Set, filename: string, assetInfo: AssetInfo }>} HotUpdateMainContentByRuntime */ +/** @typedef {{ updatedChunkIds: Set, removedChunkIds: Set, removedModules: Set, filename: string, assetInfo: AssetInfo }} HotUpdateMainContentByRuntimeItem */ +/** @typedef {Map} HotUpdateMainContentByRuntime */ /** @type {WeakMap} */ const parserHooksMap = new WeakMap(); @@ -512,7 +515,8 @@ class HotModuleReplacementPlugin { forEachRuntime(allOldRuntime, runtime => { const { path: filename, info: assetInfo } = compilation.getPathWithInfo( - compilation.outputOptions.hotUpdateMainFilename, + /** @type {NonNullable} */ + (compilation.outputOptions.hotUpdateMainFilename), { hash: records.hash, runtime @@ -535,7 +539,9 @@ class HotModuleReplacementPlugin { /** @type {Map} */ const allModules = new Map(); for (const module of compilation.modules) { - const id = chunkGraph.getModuleId(module); + const id = + /** @type {ModuleId} */ + (chunkGraph.getModuleId(module)); allModules.set(id, module); } @@ -606,9 +612,13 @@ class HotModuleReplacementPlugin { if (removedFromRuntime) { // chunk was removed from some runtimes forEachRuntime(removedFromRuntime, runtime => { - const item = hotUpdateMainContentByRuntime.get( - /** @type {string} */ (runtime) - ); + const item = + /** @type {HotUpdateMainContentByRuntimeItem} */ + ( + hotUpdateMainContentByRuntime.get( + /** @type {string} */ (runtime) + ) + ); item.removedChunkIds.add(/** @type {ChunkId} */ (chunkId)); }); // dispose modules from the chunk in these runtimes @@ -655,9 +665,12 @@ class HotModuleReplacementPlugin { ) return; } - const item = hotUpdateMainContentByRuntime.get( - /** @type {string} */ (runtime) - ); + const item = + /** @type {HotUpdateMainContentByRuntimeItem} */ ( + hotUpdateMainContentByRuntime.get( + /** @type {string} */ (runtime) + ) + ); item.removedModules.add(module); }); } @@ -732,9 +745,12 @@ class HotModuleReplacementPlugin { } } forEachRuntime(newRuntime, runtime => { - const item = hotUpdateMainContentByRuntime.get( - /** @type {string} */ (runtime) - ); + const item = + /** @type {HotUpdateMainContentByRuntimeItem} */ ( + hotUpdateMainContentByRuntime.get( + /** @type {string} */ (runtime) + ) + ); item.updatedChunkIds.add(/** @type {ChunkId} */ (chunkId)); }); } @@ -789,8 +805,10 @@ To fix this, make sure to include [runtime] in the output.hotUpdateMainFilename removedModules.size === 0 ? completelyRemovedModulesArray : completelyRemovedModulesArray.concat( - Array.from(removedModules, m => - chunkGraph.getModuleId(m) + Array.from( + removedModules, + m => + /** @type {ModuleId} */ (chunkGraph.getModuleId(m)) ) ) }; diff --git a/lib/LibManifestPlugin.js b/lib/LibManifestPlugin.js index d9061c8ec..ab9d2fc57 100644 --- a/lib/LibManifestPlugin.js +++ b/lib/LibManifestPlugin.js @@ -11,6 +11,7 @@ const { someInIterable } = require("./util/IterableHelpers"); const { compareModulesById } = require("./util/comparators"); const { dirname, mkdirp } = require("./util/fs"); +/** @typedef {import("./ChunkGraph").ModuleId} ModuleId */ /** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./Compiler").IntermediateFileSystem} IntermediateFileSystem */ /** @typedef {import("./Module").BuildMeta} BuildMeta */ @@ -102,7 +103,7 @@ class LibManifestPlugin { const providedExports = exportsInfo.getProvidedExports(); /** @type {ManifestModuleData} */ const data = { - id: chunkGraph.getModuleId(module), + id: /** @type {ModuleId} */ (chunkGraph.getModuleId(module)), buildMeta: /** @type {BuildMeta} */ (module.buildMeta), exports: Array.isArray(providedExports) ? providedExports diff --git a/lib/Module.js b/lib/Module.js index ec930d687..e4e973c97 100644 --- a/lib/Module.js +++ b/lib/Module.js @@ -81,7 +81,7 @@ const makeSerializable = require("./util/makeSerializable"); * @typedef {object} CodeGenerationResult * @property {Map} sources the resulting sources for all source types * @property {Map=} data the resulting data for all source types - * @property {ReadOnlyRuntimeRequirements} runtimeRequirements the runtime requirements + * @property {ReadOnlyRuntimeRequirements | null} runtimeRequirements the runtime requirements * @property {string=} hash a hash of the code generation result (will be automatically calculated from sources and runtimeRequirements if not provided) */ @@ -897,7 +897,11 @@ class Module extends DependenciesBlock { }; const sources = this.codeGeneration(codeGenContext).sources; - return type ? sources.get(type) : sources.get(first(this.getSourceTypes())); + return /** @type {Source} */ ( + type + ? sources.get(type) + : sources.get(/** @type {string} */ (first(this.getSourceTypes()))) + ); } /* istanbul ignore next */ @@ -1103,6 +1107,8 @@ class Module extends DependenciesBlock { makeSerializable(Module, "webpack/lib/Module"); // TODO remove in webpack 6 +// eslint-disable-next-line no-warning-comments +// @ts-ignore https://github.com/microsoft/TypeScript/issues/42919 Object.defineProperty(Module.prototype, "hasEqualsChunks", { get() { throw new Error( @@ -1112,6 +1118,8 @@ Object.defineProperty(Module.prototype, "hasEqualsChunks", { }); // TODO remove in webpack 6 +// eslint-disable-next-line no-warning-comments +// @ts-ignore https://github.com/microsoft/TypeScript/issues/42919 Object.defineProperty(Module.prototype, "isUsed", { get() { throw new Error( @@ -1157,6 +1165,8 @@ Object.defineProperty(Module.prototype, "warnings", { }); // TODO remove in webpack 6 +// eslint-disable-next-line no-warning-comments +// @ts-ignore https://github.com/microsoft/TypeScript/issues/42919 Object.defineProperty(Module.prototype, "used", { get() { throw new Error( diff --git a/lib/ModuleDependencyError.js b/lib/ModuleDependencyError.js index 1107274a0..bb7341db7 100644 --- a/lib/ModuleDependencyError.js +++ b/lib/ModuleDependencyError.js @@ -30,7 +30,7 @@ class ModuleDependencyError extends WebpackError { /** error is not (de)serialized, so it might be undefined after deserialization */ this.error = err; - if (err && /** @type {any} */ (err).hideStack) { + if (err && /** @type {any} */ (err).hideStack && err.stack) { this.stack = /** @type {string} */ `${err.stack .split("\n") .slice(1) diff --git a/lib/ModuleDependencyWarning.js b/lib/ModuleDependencyWarning.js index 9edc4a351..2fc403b9d 100644 --- a/lib/ModuleDependencyWarning.js +++ b/lib/ModuleDependencyWarning.js @@ -30,7 +30,7 @@ class ModuleDependencyWarning extends WebpackError { /** error is not (de)serialized, so it might be undefined after deserialization */ this.error = err; - if (err && /** @type {any} */ (err).hideStack) { + if (err && /** @type {any} */ (err).hideStack && err.stack) { this.stack = /** @type {string} */ `${err.stack .split("\n") .slice(1) diff --git a/lib/MultiStats.js b/lib/MultiStats.js index bfa14c746..bf4771a5f 100644 --- a/lib/MultiStats.js +++ b/lib/MultiStats.js @@ -8,15 +8,25 @@ const identifierUtils = require("./util/identifier"); /** @typedef {import("../declarations/WebpackOptions").StatsOptions} StatsOptions */ +/** @typedef {import("./Compilation").CreateStatsOptionsContext} CreateStatsOptionsContext */ +/** @typedef {import("./Compilation").NormalizedStatsOptions} NormalizedStatsOptions */ /** @typedef {import("./Stats")} Stats */ /** @typedef {import("./stats/DefaultStatsFactoryPlugin").KnownStatsCompilation} KnownStatsCompilation */ /** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */ +/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsError} StatsError */ +/** + * @param {string} str string + * @param {string} prefix pref + * @returns {string} indent + */ const indent = (str, prefix) => { const rem = str.replace(/\n([^\n])/g, `\n${prefix}$1`); return prefix + rem; }; +/** @typedef {{ version: boolean, hash: boolean, errorsCount: boolean, warningsCount: boolean, errors: boolean, warnings: boolean, children: NormalizedStatsOptions[] }} ChildOptions */ + class MultiStats { /** * @param {Stats[]} stats the child stats @@ -43,13 +53,30 @@ class MultiStats { return this.stats.some(stat => stat.hasWarnings()); } + /** + * @param {string | boolean | StatsOptions | undefined} options stats options + * @param {CreateStatsOptionsContext} context context + * @returns {ChildOptions} context context + */ _createChildOptions(options, context) { - if (!options) { - options = {}; - } - const { children: childrenOptions = undefined, ...baseOptions } = - typeof options === "string" ? { preset: options } : options; + const getCreateStatsOptions = () => { + if (!options) { + options = {}; + } + + const { children: childrenOptions = undefined, ...baseOptions } = + typeof options === "string" + ? { preset: options } + : /** @type {StatsOptions} */ (options); + + return { childrenOptions, baseOptions }; + }; + const children = this.stats.map((stat, idx) => { + if (typeof options === "boolean") { + return stat.compilation.createStatsOptions(options, context); + } + const { childrenOptions, baseOptions } = getCreateStatsOptions(); const childOptions = Array.isArray(childrenOptions) ? childrenOptions[idx] : childrenOptions; @@ -77,77 +104,96 @@ class MultiStats { } /** - * @param {any} options stats options + * @param {(string | boolean | StatsOptions)=} options stats options * @returns {StatsCompilation} json output */ toJson(options) { - options = this._createChildOptions(options, { forToString: false }); + const childOptions = this._createChildOptions(options, { + forToString: false + }); /** @type {KnownStatsCompilation} */ const obj = {}; obj.children = this.stats.map((stat, idx) => { - const obj = stat.toJson(options.children[idx]); + const obj = stat.toJson(childOptions.children[idx]); const compilationName = stat.compilation.name; const name = compilationName && identifierUtils.makePathsRelative( - options.context, + stat.compilation.compiler.context, compilationName, stat.compilation.compiler.root ); obj.name = name; return obj; }); - if (options.version) { + if (childOptions.version) { obj.version = obj.children[0].version; } - if (options.hash) { + if (childOptions.hash) { obj.hash = obj.children.map(j => j.hash).join(""); } + /** + * @param {StatsCompilation} j stats error + * @param {StatsError} obj Stats error + * @returns {TODO} result + */ const mapError = (j, obj) => ({ ...obj, compilerPath: obj.compilerPath ? `${j.name}.${obj.compilerPath}` : j.name }); - if (options.errors) { + if (childOptions.errors) { obj.errors = []; for (const j of obj.children) { - for (const i of j.errors) { + const errors = + /** @type {NonNullable} */ + (j.errors); + for (const i of errors) { obj.errors.push(mapError(j, i)); } } } - if (options.warnings) { + if (childOptions.warnings) { obj.warnings = []; for (const j of obj.children) { - for (const i of j.warnings) { + const warnings = + /** @type {NonNullable} */ + (j.warnings); + for (const i of warnings) { obj.warnings.push(mapError(j, i)); } } } - if (options.errorsCount) { + if (childOptions.errorsCount) { obj.errorsCount = 0; for (const j of obj.children) { - obj.errorsCount += j.errorsCount; + obj.errorsCount += /** @type {number} */ (j.errorsCount); } } - if (options.warningsCount) { + if (childOptions.warningsCount) { obj.warningsCount = 0; for (const j of obj.children) { - obj.warningsCount += j.warningsCount; + obj.warningsCount += /** @type {number} */ (j.warningsCount); } } return obj; } + /** + * @param {(string | boolean | StatsOptions)=} options stats options + * @returns {string} string output + */ toString(options) { - options = this._createChildOptions(options, { forToString: true }); + const childOptions = this._createChildOptions(options, { + forToString: true + }); const results = this.stats.map((stat, idx) => { - const str = stat.toString(options.children[idx]); + const str = stat.toString(childOptions.children[idx]); const compilationName = stat.compilation.name; const name = compilationName && identifierUtils .makePathsRelative( - options.context, + stat.compilation.compiler.context, compilationName, stat.compilation.compiler.root ) diff --git a/lib/NormalModule.js b/lib/NormalModule.js index f8e55798e..34e32a97c 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -88,6 +88,16 @@ const memoize = require("./util/memoize"); /** @typedef {UnsafeCacheData & { parser: undefined | Parser, parserOptions: undefined | ParserOptions, generator: undefined | Generator, generatorOptions: undefined | GeneratorOptions }} NormalModuleUnsafeCacheData */ +/** + * @template T + * @typedef {import("../declarations/LoaderContext").LoaderContext} LoaderContext + */ + +/** + * @template T + * @typedef {import("../declarations/LoaderContext").NormalModuleLoaderContext} NormalModuleLoaderContext + */ + /** * @typedef {object} SourceMap * @property {number} version @@ -184,6 +194,9 @@ const asBuffer = input => { }; class NonErrorEmittedError extends WebpackError { + /** + * @param {any} error value which is not an instance of Error + */ constructor(error) { super(); @@ -200,12 +213,12 @@ makeSerializable( /** * @typedef {object} NormalModuleCompilationHooks - * @property {SyncHook<[object, NormalModule]>} loader - * @property {SyncHook<[LoaderItem[], NormalModule, object]>} beforeLoaders + * @property {SyncHook<[LoaderContext, NormalModule]>} loader + * @property {SyncHook<[LoaderItem[], NormalModule, LoaderContext]>} beforeLoaders * @property {SyncHook<[NormalModule]>} beforeParse * @property {SyncHook<[NormalModule]>} beforeSnapshot - * @property {HookMap>} readResourceForScheme - * @property {HookMap>} readResource + * @property {HookMap>} readResourceForScheme + * @property {HookMap], string | Buffer>>} readResource * @property {AsyncSeriesBailHook<[NormalModule, NeedBuildContext], boolean>} needBuild */ @@ -251,20 +264,32 @@ class NormalModule extends Module { beforeSnapshot: new SyncHook(["module"]), // TODO webpack 6 deprecate readResourceForScheme: new HookMap(scheme => { - const hook = hooks.readResource.for(scheme); + const hook = + /** @type {NormalModuleCompilationHooks} */ + (hooks).readResource.for(scheme); return createFakeHook( /** @type {AsyncSeriesBailHook<[string, NormalModule], string | Buffer>} */ ({ tap: (options, fn) => hook.tap(options, loaderContext => - fn(loaderContext.resource, loaderContext._module) + fn( + loaderContext.resource, + /** @type {NormalModule} */ (loaderContext._module) + ) ), tapAsync: (options, fn) => hook.tapAsync(options, (loaderContext, callback) => - fn(loaderContext.resource, loaderContext._module, callback) + fn( + loaderContext.resource, + /** @type {NormalModule} */ (loaderContext._module), + callback + ) ), tapPromise: (options, fn) => hook.tapPromise(options, loaderContext => - fn(loaderContext.resource, loaderContext._module) + fn( + loaderContext.resource, + /** @type {NormalModule} */ (loaderContext._module) + ) ) }) ); @@ -741,7 +766,7 @@ class NormalModule extends Module { Object.assign(loaderContext, options.loader); - hooks.loader.call(loaderContext, this); + hooks.loader.call(/** @type {LoaderContext} */ (loaderContext), this); return loaderContext; } @@ -889,7 +914,11 @@ class NormalModule extends Module { buildInfo.cacheable = true; try { - hooks.beforeLoaders.call(this.loaders, this, loaderContext); + hooks.beforeLoaders.call( + this.loaders, + this, + /** @type {LoaderContext} */ (loaderContext) + ); } catch (err) { processResult(err); return; @@ -1509,7 +1538,8 @@ class NormalModule extends Module { */ updateHash(hash, context) { hash.update(/** @type {BuildInfo} */ (this.buildInfo).hash); - this.generator.updateHash(hash, { + /** @type {Generator} */ + (this.generator).updateHash(hash, { module: this, ...context }); diff --git a/lib/NormalModuleFactory.js b/lib/NormalModuleFactory.js index f312d5f05..a18df20d5 100644 --- a/lib/NormalModuleFactory.js +++ b/lib/NormalModuleFactory.js @@ -1165,7 +1165,9 @@ If changing the source code is not an option there is also a resolve options cal } if (err) return callback(err); - const parsedResult = this._parseResourceWithoutFragment(result); + const parsedResult = this._parseResourceWithoutFragment( + /** @type {string} */ (result) + ); const type = /\.mjs$/i.test(parsedResult.path) ? "module" diff --git a/lib/SourceMapDevToolPlugin.js b/lib/SourceMapDevToolPlugin.js index 853a34608..ed0030573 100644 --- a/lib/SourceMapDevToolPlugin.js +++ b/lib/SourceMapDevToolPlugin.js @@ -108,7 +108,7 @@ const getTaskForFile = ( source = asset.source(); } if (!sourceMap || typeof source !== "string") return; - const context = compilation.options.context; + const context = /** @type {string} */ (compilation.options.context); const root = compilation.compiler.root; const cachedAbsolutify = makePathsAbsolute.bindContextCache(context, root); const modules = sourceMap.sources.map(source => { @@ -137,8 +137,7 @@ class SourceMapDevToolPlugin { constructor(options = {}) { validate(options); - /** @type {string | false} */ - this.sourceMapFilename = options.filename; + this.sourceMapFilename = /** @type {string | false} */ (options.filename); /** @type {string | false | (function(PathData, AssetInfo=): string)}} */ this.sourceMappingURLComment = options.append === false diff --git a/lib/Stats.js b/lib/Stats.js index c6949117d..22a36632a 100644 --- a/lib/Stats.js +++ b/lib/Stats.js @@ -55,13 +55,11 @@ class Stats { * @returns {StatsCompilation} json output */ toJson(options) { - options = this.compilation.createStatsOptions(options, { + const normalizedOptions = this.compilation.createStatsOptions(options, { forToString: false }); - const statsFactory = this.compilation.createStatsFactory( - /** @type {NormalizedStatsOptions} */ (options) - ); + const statsFactory = this.compilation.createStatsFactory(normalizedOptions); return statsFactory.create("compilation", this.compilation, { compilation: this.compilation @@ -73,16 +71,12 @@ class Stats { * @returns {string} string output */ toString(options) { - options = this.compilation.createStatsOptions(options, { + const normalizedOptions = this.compilation.createStatsOptions(options, { forToString: true }); - const statsFactory = this.compilation.createStatsFactory( - /** @type {NormalizedStatsOptions} */ (options) - ); - const statsPrinter = this.compilation.createStatsPrinter( - /** @type {NormalizedStatsOptions} */ (options) - ); + const statsFactory = this.compilation.createStatsFactory(normalizedOptions); + const statsPrinter = this.compilation.createStatsPrinter(normalizedOptions); const data = statsFactory.create("compilation", this.compilation, { compilation: this.compilation diff --git a/lib/Template.js b/lib/Template.js index bcf3042c2..f80051f6f 100644 --- a/lib/Template.js +++ b/lib/Template.js @@ -13,6 +13,7 @@ const RuntimeGlobals = require("./RuntimeGlobals"); /** @typedef {import("../declarations/WebpackOptions").Output} OutputOptions */ /** @typedef {import("./Chunk")} Chunk */ /** @typedef {import("./ChunkGraph")} ChunkGraph */ +/** @typedef {import("./ChunkGraph").ModuleId} ModuleId */ /** @typedef {import("./CodeGenerationResults")} CodeGenerationResults */ /** @typedef {import("./Compilation").AssetInfo} AssetInfo */ /** @typedef {import("./Compilation").PathData} PathData */ @@ -294,7 +295,7 @@ class Template { } /** @type {{id: string|number, source: Source|string}[]} */ const allModules = modules.map(module => ({ - id: chunkGraph.getModuleId(module), + id: /** @type {ModuleId} */ (chunkGraph.getModuleId(module)), source: renderModule(module) || "false" })); const bounds = Template.getModulesArrayBounds(allModules); diff --git a/lib/cli.js b/lib/cli.js index 9ecf78dad..3f56d06a5 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -52,6 +52,8 @@ const webpackSchema = require("../schemas/WebpackOptions.json"); * @property {ArgumentConfig[]} configs */ +/** @typedef {string | number | boolean | RegExp | (string | number | boolean | RegExp)} Value */ + /** * @param {any=} schema a json schema to create arguments for (by default webpack schema is used) * @returns {Record} object of arguments @@ -60,6 +62,10 @@ const getArguments = (schema = webpackSchema) => { /** @type {Record} */ const flags = {}; + /** + * @param {string} input input + * @returns {string} result + */ const pathToArgumentName = input => input .replace(/\./g, "-") @@ -71,6 +77,10 @@ const getArguments = (schema = webpackSchema) => { .replace(/-?[^\p{Uppercase_Letter}\p{Lowercase_Letter}\d]+/gu, "-") .toLowerCase(); + /** + * @param {string} path path + * @returns {any} schema part + */ const getSchemaPart = path => { const newPath = path.split("/"); @@ -131,7 +141,7 @@ const getArguments = (schema = webpackSchema) => { /** * @param {any} schemaPart schema - * @returns {Pick} partial argument config + * @returns {Pick | undefined} partial argument config */ const schemaToArgumentConfig = schemaPart => { if (schemaPart.enum) { @@ -342,36 +352,49 @@ const getArguments = (schema = webpackSchema) => { traverse(schema); + /** @typedef {"string" | "number" | "boolean"} Type */ + // Summarize flags for (const name of Object.keys(flags)) { const argument = flags[name]; - argument.description = argument.configs.reduce((desc, { description }) => { - if (!desc) return description; - if (!description) return desc; - if (desc.includes(description)) return desc; - return `${desc} ${description}`; - }, /** @type {string | undefined} */ (undefined)); - argument.simpleType = argument.configs.reduce((t, argConfig) => { - /** @type {"string" | "number" | "boolean"} */ - let type = "string"; - switch (argConfig.type) { - case "number": - type = "number"; - break; - case "reset": - case "boolean": - type = "boolean"; - break; - case "enum": - if (argConfig.values.every(v => typeof v === "boolean")) - type = "boolean"; - if (argConfig.values.every(v => typeof v === "number")) - type = "number"; - break; - } - if (t === undefined) return type; - return t === type ? t : "string"; - }, /** @type {"string" | "number" | "boolean" | undefined} */ (undefined)); + argument.description = + /** @type {string} */ + ( + argument.configs.reduce((desc, { description }) => { + if (!desc) return description; + if (!description) return desc; + if (desc.includes(description)) return desc; + return `${desc} ${description}`; + }, /** @type {string | undefined} */ (undefined)) + ); + argument.simpleType = + /** @type {Type} */ + ( + argument.configs.reduce((t, argConfig) => { + /** @type {Type} */ + let type = "string"; + switch (argConfig.type) { + case "number": + type = "number"; + break; + case "reset": + case "boolean": + type = "boolean"; + break; + case "enum": { + const values = + /** @type {NonNullable} */ + (argConfig.values); + + if (values.every(v => typeof v === "boolean")) type = "boolean"; + if (values.every(v => typeof v === "number")) type = "number"; + break; + } + } + if (t === undefined) return type; + return t === type ? t : "string"; + }, /** @type {Type | undefined} */ (undefined)) + ); argument.multiple = argument.configs.some(c => c.multiple); } @@ -380,16 +403,18 @@ const getArguments = (schema = webpackSchema) => { const cliAddedItems = new WeakMap(); +/** @typedef {string | number} Property */ + /** * @param {any} config configuration * @param {string} schemaPath path in the config * @param {number | undefined} index index of value when multiple values are provided, otherwise undefined - * @returns {{ problem?: LocalProblem, object?: any, property?: string | number, value?: any }} problem or object with property and value + * @returns {{ problem?: LocalProblem, object?: any, property?: Property, value?: any }} problem or object with property and value */ const getObjectAndProperty = (config, schemaPath, index = 0) => { if (!schemaPath) return { value: config }; const parts = schemaPath.split("."); - const property = parts.pop(); + const property = /** @type {string} */ (parts.pop()); let current = config; let i = 0; for (const part of parts) { @@ -494,7 +519,7 @@ const setValue = (config, schemaPath, value, index) => { index ); if (problem) return problem; - object[property] = value; + object[/** @type {Property} */ (property)] = value; return null; }; @@ -536,7 +561,11 @@ const getExpectedValue = argConfig => { case "RegExp": return "regular expression (example: /ab?c*/)"; case "enum": - return argConfig.values.map(v => `${v}`).join(" | "); + return /** @type {NonNullable} */ ( + argConfig.values + ) + .map(v => `${v}`) + .join(" | "); case "reset": return "true (will reset the previous value to an empty array)"; default: @@ -582,12 +611,16 @@ const parseValueForArgumentConfig = (argConfig, value) => { return new RegExp(match[1], match[2]); } break; - case "enum": - if (argConfig.values.includes(value)) return value; - for (const item of argConfig.values) { + case "enum": { + const values = + /** @type {NonNullable} */ + (argConfig.values); + if (values.includes(value)) return value; + for (const item of values) { if (`${item}` === value) return item; } break; + } case "reset": if (value === true) return []; break; @@ -597,7 +630,7 @@ const parseValueForArgumentConfig = (argConfig, value) => { /** * @param {Record} args object of arguments * @param {any} config configuration - * @param {Record} values object with values + * @param {Record} values object with values * @returns {Problem[] | null} problems or null for success */ const processArguments = (args, config, values) => { @@ -613,6 +646,10 @@ const processArguments = (args, config, values) => { }); continue; } + /** + * @param {Value} value value + * @param {number} i index + */ const processValue = (value, i) => { const currentProblems = []; for (const argConfig of arg.configs) { diff --git a/lib/dependencies/JsonExportsDependency.js b/lib/dependencies/JsonExportsDependency.js index bb085e298..39ce927eb 100644 --- a/lib/dependencies/JsonExportsDependency.js +++ b/lib/dependencies/JsonExportsDependency.js @@ -14,10 +14,15 @@ const NullDependency = require("./NullDependency"); /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ /** @typedef {import("../json/JsonData")} JsonData */ +/** @typedef {import("../json/JsonData").RawJsonData} RawJsonData */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ /** @typedef {import("../util/Hash")} Hash */ +/** + * @param {RawJsonData} data data + * @returns {TODO} value + */ const getExportsFromData = data => { if (data && typeof data === "object") { if (Array.isArray(data)) { @@ -62,7 +67,9 @@ class JsonExportsDependency extends NullDependency { */ getExports(moduleGraph) { return { - exports: getExportsFromData(this.data && this.data.get()), + exports: getExportsFromData( + this.data && /** @type {RawJsonData} */ (this.data.get()) + ), dependencies: undefined }; } diff --git a/lib/dependencies/LoaderPlugin.js b/lib/dependencies/LoaderPlugin.js index b1f5e6dc7..1f42a4824 100644 --- a/lib/dependencies/LoaderPlugin.js +++ b/lib/dependencies/LoaderPlugin.js @@ -10,17 +10,12 @@ const LazySet = require("../util/LazySet"); const LoaderDependency = require("./LoaderDependency"); const LoaderImportDependency = require("./LoaderImportDependency"); +/** @typedef {import("../../declarations/LoaderContext").LoaderPluginLoaderContext} LoaderPluginLoaderContext */ /** @typedef {import("../Compilation").DepConstructor} DepConstructor */ +/** @typedef {import("../Compilation").ExecuteModuleResult} ExecuteModuleResult */ /** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Module")} Module */ - -/** - * @callback LoadModuleCallback - * @param {(Error | null)=} err error object - * @param {string | Buffer=} source source code - * @param {object=} map source map - * @param {Module=} module loaded module if successful - */ +/** @typedef {import("../Module").BuildInfo} BuildInfo */ /** * @callback ImportModuleCallback @@ -66,11 +61,6 @@ class LoaderPlugin { NormalModule.getCompilationHooks(compilation).loader.tap( "LoaderPlugin", loaderContext => { - /** - * @param {string} request the request string to load the module from - * @param {LoadModuleCallback} callback callback returning the loaded module or error - * @returns {void} - */ loaderContext.loadModule = (request, callback) => { const dep = new LoaderDependency(request); dep.loc = { @@ -91,7 +81,9 @@ class LoaderPlugin { { factory, dependencies: [dep], - originModule: loaderContext._module, + originModule: + /** @type {NormalModule} */ + (loaderContext._module), context: loaderContext.context, recursive: false }, @@ -150,15 +142,20 @@ class LoaderPlugin { for (const d of buildDependencies) { loaderContext.addBuildDependency(d); } - return callback(null, source, map, referencedModule); + return callback( + null, + source, + /** @type {object | null} */ (map), + referencedModule + ); } ); }; /** * @param {string} request the request string to load the module from - * @param {ImportModuleOptions=} options options - * @param {ImportModuleCallback=} callback callback returning the exports + * @param {ImportModuleOptions} options options + * @param {ImportModuleCallback} callback callback returning the exports * @returns {void} */ const importModule = (request, options, callback) => { @@ -181,7 +178,9 @@ class LoaderPlugin { { factory, dependencies: [dep], - originModule: loaderContext._module, + originModule: + /** @type {NormalModule} */ + (loaderContext._module), contextInfo: { issuerLayer: options.layer }, @@ -208,42 +207,53 @@ class LoaderPlugin { }, (err, result) => { if (err) return callback(err); - for (const d of result.fileDependencies) { + const { + fileDependencies, + contextDependencies, + missingDependencies, + buildDependencies, + cacheable, + assets, + exports + } = /** @type {ExecuteModuleResult} */ (result); + for (const d of fileDependencies) { loaderContext.addDependency(d); } - for (const d of result.contextDependencies) { + for (const d of contextDependencies) { loaderContext.addContextDependency(d); } - for (const d of result.missingDependencies) { + for (const d of missingDependencies) { loaderContext.addMissingDependency(d); } - for (const d of result.buildDependencies) { + for (const d of buildDependencies) { loaderContext.addBuildDependency(d); } - if (result.cacheable === false) - loaderContext.cacheable(false); - for (const [name, { source, info }] of result.assets) { - const { buildInfo } = loaderContext._module; + if (cacheable === false) loaderContext.cacheable(false); + for (const [name, { source, info }] of assets) { + const buildInfo = + /** @type {BuildInfo} */ + ( + /** @type {NormalModule} */ (loaderContext._module) + .buildInfo + ); if (!buildInfo.assets) { buildInfo.assets = Object.create(null); buildInfo.assetsInfo = new Map(); } - buildInfo.assets[name] = source; - buildInfo.assetsInfo.set(name, info); + /** @type {NonNullable} */ + (buildInfo.assets)[name] = source; + /** @type {NonNullable} */ + (buildInfo.assetsInfo).set(name, info); } - callback(null, result.exports); + callback(null, exports); } ); } ); }; - /** - * @param {string} request the request string to load the module from - * @param {ImportModuleOptions} options options - * @param {ImportModuleCallback=} callback callback returning the exports - * @returns {Promise | void} exports - */ + // eslint-disable-next-line no-warning-comments + // @ts-ignore Overloading doesn't work loaderContext.importModule = (request, options, callback) => { if (!callback) { return new Promise((resolve, reject) => { diff --git a/lib/esm/ModuleChunkLoadingRuntimeModule.js b/lib/esm/ModuleChunkLoadingRuntimeModule.js index 901c15cb3..829a35965 100644 --- a/lib/esm/ModuleChunkLoadingRuntimeModule.js +++ b/lib/esm/ModuleChunkLoadingRuntimeModule.js @@ -17,6 +17,7 @@ const { getInitialChunkIds } = require("../javascript/StartupHelpers"); const compileBooleanMatcher = require("../util/compileBooleanMatcher"); const { getUndoPath } = require("../util/identifier"); +/** @typedef {import("../../declarations/WebpackOptions").Environment} Environment */ /** @typedef {import("../Chunk")} Chunk */ /** @typedef {import("../ChunkGraph")} ChunkGraph */ /** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */ @@ -87,9 +88,12 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule { const compilation = /** @type {Compilation} */ (this.compilation); const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph); const chunk = /** @type {Chunk} */ (this.chunk); + const environment = + /** @type {Environment} */ + (compilation.outputOptions.environment); const { runtimeTemplate, - outputOptions: { environment, importFunctionName, crossOriginLoading } + outputOptions: { importFunctionName, crossOriginLoading } } = compilation; const fn = RuntimeGlobals.ensureChunkHandlers; const withBaseURI = this._runtimeRequirements.has(RuntimeGlobals.baseURI); diff --git a/lib/ids/HashedModuleIdsPlugin.js b/lib/ids/HashedModuleIdsPlugin.js index a1375a9ef..e3891a469 100644 --- a/lib/ids/HashedModuleIdsPlugin.js +++ b/lib/ids/HashedModuleIdsPlugin.js @@ -64,7 +64,11 @@ class HashedModuleIdsPlugin { ); for (const module of modulesInNaturalOrder) { const ident = getFullModuleName(module, context, compiler.root); - const hash = createHash(options.hashFunction); + const hash = createHash( + /** @type {NonNullable} */ ( + options.hashFunction + ) + ); hash.update(ident || ""); const hashId = /** @type {string} */ ( hash.digest(options.hashDigest) diff --git a/lib/ids/SyncModuleIdsPlugin.js b/lib/ids/SyncModuleIdsPlugin.js index 564ccc2f4..aa837624e 100644 --- a/lib/ids/SyncModuleIdsPlugin.js +++ b/lib/ids/SyncModuleIdsPlugin.js @@ -10,6 +10,7 @@ const { getUsedModuleIdsAndModules } = require("./IdHelpers"); /** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Module")} Module */ +/** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */ const plugin = "SyncModuleIdsPlugin"; @@ -42,7 +43,9 @@ class SyncModuleIdsPlugin { let dataChanged = false; if (this._read) { compiler.hooks.readRecords.tapAsync(plugin, callback => { - const fs = compiler.intermediateFileSystem; + const fs = + /** @type {IntermediateFileSystem} */ + (compiler.intermediateFileSystem); fs.readFile(this._path, (err, buffer) => { if (err) { if (err.code !== "ENOENT") { @@ -69,7 +72,9 @@ class SyncModuleIdsPlugin { for (const [key, value] of sorted) { json[key] = value; } - const fs = compiler.intermediateFileSystem; + const fs = + /** @type {IntermediateFileSystem} */ + (compiler.intermediateFileSystem); fs.writeFile(this._path, JSON.stringify(json), callback); }); } diff --git a/lib/library/ModernModuleLibraryPlugin.js b/lib/library/ModernModuleLibraryPlugin.js index 6b99532a5..23a9510c2 100644 --- a/lib/library/ModernModuleLibraryPlugin.js +++ b/lib/library/ModernModuleLibraryPlugin.js @@ -16,6 +16,7 @@ const AbstractLibraryPlugin = require("./AbstractLibraryPlugin"); /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */ /** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Module")} Module */ +/** @typedef {import("../Module").BuildMeta} BuildMeta */ /** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */ /** @typedef {import("../util/Hash")} Hash */ /** @template T @typedef {import("./AbstractLibraryPlugin").LibraryContext} LibraryContext */ @@ -92,7 +93,9 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin { ) { const result = new ConcatSource(source); const exportsInfo = moduleGraph.getExportsInfo(module); - const definitions = module.buildMeta.exportsFinalName; + const definitions = + /** @type {BuildMeta} */ + (module.buildMeta).exportsFinalName; const exports = []; for (const exportInfo of exportsInfo.orderedExports) { @@ -105,7 +108,7 @@ class ModernModuleLibraryPlugin extends AbstractLibraryPlugin { for (const reexportInfo of exp.orderedExports) { if ( !reexportInfo.provided && - reexportInfo.name === reexport.export[0] + reexportInfo.name === /** @type {string[]} */ (reexport.export)[0] ) { shouldContinue = true; } diff --git a/lib/optimize/ConcatenatedModule.js b/lib/optimize/ConcatenatedModule.js index a67bd544e..52a3388e5 100644 --- a/lib/optimize/ConcatenatedModule.js +++ b/lib/optimize/ConcatenatedModule.js @@ -155,11 +155,11 @@ if (!ReferencerClass.prototype.PropertyDefinition) { * @property {number} index * @property {string} name * @property {boolean} interopNamespaceObjectUsed - * @property {string} interopNamespaceObjectName + * @property {string | undefined} interopNamespaceObjectName * @property {boolean} interopNamespaceObject2Used - * @property {string} interopNamespaceObject2Name + * @property {string | undefined} interopNamespaceObject2Name * @property {boolean} interopDefaultAccessUsed - * @property {string} interopDefaultAccessName + * @property {string | undefined} interopDefaultAccessName */ /** @@ -631,7 +631,7 @@ const TYPES = new Set(["javascript"]); /** * @typedef {object} ConcatenateModuleHooks - * @property {SyncBailHook<[Record]>} exportsDefinitions + * @property {SyncBailHook<[Record], boolean>} exportsDefinitions */ /** @type {WeakMap} */ @@ -1511,7 +1511,7 @@ class ConcatenatedModule extends Module { // define exports if (exportsMap.size > 0) { const { exportsDefinitions } = ConcatenatedModule.getCompilationHooks( - this.compilation + /** @type {Compilation} */ (this.compilation) ); const definitions = []; @@ -1546,7 +1546,8 @@ class ConcatenatedModule extends Module { }, {${definitions.join(",")}\n});\n` ); } else { - this.buildMeta.exportsFinalName = exportsFinalName; + /** @type {BuildMeta} */ + (this.buildMeta).exportsFinalName = exportsFinalName; } } @@ -1867,7 +1868,7 @@ ${defineGetters}` /** @type {ModuleInfo} */ (item).module, /** @type {ModuleInfo} */ (item) ); - return item; + return /** @type {ModuleInfo} */ (item); } /** @type {ReferenceToModuleInfo} */ const ref = { diff --git a/lib/rules/BasicEffectRulePlugin.js b/lib/rules/BasicEffectRulePlugin.js index 7043f3b06..935716baa 100644 --- a/lib/rules/BasicEffectRulePlugin.js +++ b/lib/rules/BasicEffectRulePlugin.js @@ -5,6 +5,7 @@ "use strict"; +/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */ /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */ class BasicEffectRulePlugin { @@ -28,7 +29,8 @@ class BasicEffectRulePlugin { if (unhandledProperties.has(this.ruleProperty)) { unhandledProperties.delete(this.ruleProperty); - const value = rule[this.ruleProperty]; + const value = + rule[/** @type {keyof RuleSetRule} */ (this.ruleProperty)]; result.effects.push({ type: this.effectType, diff --git a/lib/rules/BasicMatcherRulePlugin.js b/lib/rules/BasicMatcherRulePlugin.js index 7bfd13dc4..47ac214f6 100644 --- a/lib/rules/BasicMatcherRulePlugin.js +++ b/lib/rules/BasicMatcherRulePlugin.js @@ -5,6 +5,7 @@ "use strict"; +/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */ /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */ /** @typedef {import("./RuleSetCompiler").RuleCondition} RuleCondition */ @@ -30,7 +31,8 @@ class BasicMatcherRulePlugin { (path, rule, unhandledProperties, result) => { if (unhandledProperties.has(this.ruleProperty)) { unhandledProperties.delete(this.ruleProperty); - const value = rule[this.ruleProperty]; + const value = + rule[/** @type {keyof RuleSetRule} */ (this.ruleProperty)]; const condition = ruleSetCompiler.compileCondition( `${path}.${this.ruleProperty}`, value diff --git a/lib/rules/ObjectMatcherRulePlugin.js b/lib/rules/ObjectMatcherRulePlugin.js index 11c34fbd0..984e86f83 100644 --- a/lib/rules/ObjectMatcherRulePlugin.js +++ b/lib/rules/ObjectMatcherRulePlugin.js @@ -5,6 +5,7 @@ "use strict"; +/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */ /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */ /** @typedef {import("./RuleSetCompiler").RuleCondition} RuleCondition */ /** @typedef {import("./RuleSetCompiler").RuleConditionFunction} RuleConditionFunction */ @@ -32,7 +33,9 @@ class ObjectMatcherRulePlugin { (path, rule, unhandledProperties, result) => { if (unhandledProperties.has(ruleProperty)) { unhandledProperties.delete(ruleProperty); - const value = rule[ruleProperty]; + const value = + /** @type {Record} */ + (rule[/** @type {keyof RuleSetRule} */ (ruleProperty)]); for (const property of Object.keys(value)) { const nestedDataProperties = property.split("."); const condition = ruleSetCompiler.compileCondition( diff --git a/lib/rules/RuleSetCompiler.js b/lib/rules/RuleSetCompiler.js index bc706b90b..7674dd727 100644 --- a/lib/rules/RuleSetCompiler.js +++ b/lib/rules/RuleSetCompiler.js @@ -7,6 +7,9 @@ const { SyncHook } = require("tapable"); +/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */ +/** @typedef {import("../../declarations/WebpackOptions").RuleSetRules} RuleSetRules */ + /** @typedef {function(string): boolean} RuleConditionFunction */ /** @@ -22,10 +25,14 @@ const { SyncHook } = require("tapable"); * @property {RuleConditionFunction} fn */ +/** + * @typedef {Record} EffectData + */ + /** * @typedef {object} CompiledRule * @property {RuleCondition[]} conditions - * @property {(Effect|function(object): Effect[])[]} effects + * @property {(Effect|function(EffectData): Effect[])[]} effects * @property {CompiledRule[]=} rules * @property {CompiledRule[]=} oneOf */ @@ -39,13 +46,18 @@ const { SyncHook } = require("tapable"); /** * @typedef {object} RuleSet * @property {Map} references map of references in the rule set (may grow over time) - * @property {function(object): Effect[]} exec execute the rule set + * @property {function(EffectData): Effect[]} exec execute the rule set */ +/** @typedef {{ apply: (function(RuleSetCompiler): void) }} RuleSetPlugin */ + class RuleSetCompiler { + /** + * @param {RuleSetPlugin[]} plugins plugins + */ constructor(plugins) { this.hooks = Object.freeze({ - /** @type {SyncHook<[string, object, Set, CompiledRule, Map]>} */ + /** @type {SyncHook<[string, RuleSetRule, Set, CompiledRule, Map]>} */ rule: new SyncHook([ "path", "rule", @@ -62,7 +74,7 @@ class RuleSetCompiler { } /** - * @param {object[]} ruleSet raw user provided rules + * @param {TODO[]} ruleSet raw user provided rules * @returns {RuleSet} compiled RuleSet */ compile(ruleSet) { @@ -70,7 +82,7 @@ class RuleSetCompiler { const rules = this.compileRules("ruleSet", ruleSet, refs); /** - * @param {object} data data passed in + * @param {EffectData} data data passed in * @param {CompiledRule} rule the compiled rule * @param {Effect[]} effects an array where effects are pushed to * @returns {boolean} true, if the rule has matched @@ -79,6 +91,7 @@ class RuleSetCompiler { for (const condition of rule.conditions) { const p = condition.property; if (Array.isArray(p)) { + /** @type {EffectData | string | undefined} */ let current = data; for (const subProperty of p) { if ( @@ -93,7 +106,7 @@ class RuleSetCompiler { } } if (current !== undefined) { - if (!condition.fn(current)) return false; + if (!condition.fn(/** @type {string} */ (current))) return false; continue; } } else if (p in data) { @@ -147,25 +160,33 @@ class RuleSetCompiler { /** * @param {string} path current path - * @param {object[]} rules the raw rules provided by user + * @param {RuleSetRules} rules the raw rules provided by user * @param {Map} refs references * @returns {CompiledRule[]} rules */ compileRules(path, rules, refs) { return rules .filter(Boolean) - .map((rule, i) => this.compileRule(`${path}[${i}]`, rule, refs)); + .map((rule, i) => + this.compileRule( + `${path}[${i}]`, + /** @type {RuleSetRule} */ (rule), + refs + ) + ); } /** * @param {string} path current path - * @param {object} rule the raw rule provided by user + * @param {RuleSetRule} rule the raw rule provided by user * @param {Map} refs references * @returns {CompiledRule} normalized and compiled rule for processing */ compileRule(path, rule, refs) { const unhandledProperties = new Set( - Object.keys(rule).filter(key => rule[key] !== undefined) + Object.keys(rule).filter( + key => rule[/** @type {keyof RuleSetRule} */ (key)] !== undefined + ) ); /** @type {CompiledRule} */ @@ -303,7 +324,7 @@ class RuleSetCompiler { const fn = matcher.fn; conditions.push({ matchWhenEmpty: !matcher.matchWhenEmpty, - fn: v => !fn(v) + fn: /** @type {RuleConditionFunction} */ (v => !fn(v)) }); } break; diff --git a/lib/rules/UseEffectRulePlugin.js b/lib/rules/UseEffectRulePlugin.js index bf336e9ff..56f2423de 100644 --- a/lib/rules/UseEffectRulePlugin.js +++ b/lib/rules/UseEffectRulePlugin.js @@ -7,6 +7,9 @@ const util = require("util"); +/** @typedef {import("../../declarations/WebpackOptions").RuleSetLoader} RuleSetLoader */ +/** @typedef {import("../../declarations/WebpackOptions").RuleSetLoaderOptions} RuleSetLoaderOptions */ +/** @typedef {import("../../declarations/WebpackOptions").RuleSetRule} RuleSetRule */ /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */ /** @typedef {import("./RuleSetCompiler").Effect} Effect */ @@ -19,6 +22,10 @@ class UseEffectRulePlugin { ruleSetCompiler.hooks.rule.tap( "UseEffectRulePlugin", (path, rule, unhandledProperties, result, references) => { + /** + * @param {keyof RuleSetRule} property property + * @param {string} correctProperty correct property + */ const conflictWith = (property, correctProperty) => { if (unhandledProperties.has(property)) { throw ruleSetCompiler.error( @@ -57,7 +64,7 @@ class UseEffectRulePlugin { /** * @param {string} path options path * @param {string} defaultIdent default ident when none is provided - * @param {object} item user provided use value + * @param {{ ident?: string, loader?: RuleSetLoader, options?: RuleSetLoaderOptions }} item user provided use value * @returns {Effect} effect */ const useToEffectRaw = (path, defaultIdent, item) => { @@ -128,7 +135,10 @@ class UseEffectRulePlugin { if (typeof use === "function") { result.effects.push(data => - useToEffectsWithoutIdent(`${path}.use`, use(data)) + useToEffectsWithoutIdent( + `${path}.use`, + use(/** @type {TODO} */ (data)) + ) ); } else { for (const effect of useToEffects(`${path}.use`, use)) { @@ -142,7 +152,7 @@ class UseEffectRulePlugin { unhandledProperties.delete("options"); unhandledProperties.delete("enforce"); - const loader = rule.loader; + const loader = /** @type {RuleSetLoader} */ (rule.loader); const options = rule.options; const enforce = rule.enforce; @@ -185,8 +195,6 @@ class UseEffectRulePlugin { } ); } - - useItemToEffects(path, item) {} } module.exports = UseEffectRulePlugin; diff --git a/lib/schemes/HttpUriPlugin.js b/lib/schemes/HttpUriPlugin.js index 801b6876b..ca772de26 100644 --- a/lib/schemes/HttpUriPlugin.js +++ b/lib/schemes/HttpUriPlugin.js @@ -15,17 +15,37 @@ const createHash = require("../util/createHash"); const { mkdirp, dirname, join } = require("../util/fs"); const memoize = require("../util/memoize"); +/** @typedef {import("http").IncomingMessage} IncomingMessage */ +/** @typedef {import("http").RequestOptions} RequestOptions */ +/** @typedef {import("net").Socket} Socket */ +/** @typedef {import("stream").Readable} Readable */ /** @typedef {import("../../declarations/plugins/schemes/HttpUriPlugin").HttpUriPluginOptions} HttpUriPluginOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../FileSystemInfo").Snapshot} Snapshot */ +/** @typedef {import("../Module").BuildInfo} BuildInfo */ +/** @typedef {import("../NormalModuleFactory").ResourceDataWithData} ResourceDataWithData */ +/** @typedef {import("../util/fs").IntermediateFileSystem} IntermediateFileSystem */ const getHttp = memoize(() => require("http")); const getHttps = memoize(() => require("https")); + +/** + * @param {typeof import("http") | typeof import("https")} request request + * @param {string | { toString: () => string } | undefined} proxy proxy + * @returns {function(URL, RequestOptions, function(IncomingMessage): void): EventEmitter} fn + */ const proxyFetch = (request, proxy) => (url, options, callback) => { const eventEmitter = new EventEmitter(); - const doRequest = socket => + + /** + * @param {Socket=} socket socket + * @returns {void} + */ + const doRequest = socket => { request .get(url, { ...options, ...(socket && { socket }) }, callback) .on("error", eventEmitter.emit.bind(eventEmitter, "error")); + }; if (proxy) { const { hostname: host, port } = new URL(proxy); @@ -59,7 +79,8 @@ const proxyFetch = (request, proxy) => (url, options, callback) => { return eventEmitter; }; -/** @type {(() => void)[] | undefined} */ +/** @typedef {() => void} InProgressWriteItem */ +/** @type {InProgressWriteItem[] | undefined} */ let inProgressWrite; const validate = createSchemaValidation( @@ -157,6 +178,11 @@ const parseCacheControl = (cacheControl, requestTime) => { * @property {string} contentType */ +/** + * @param {LockfileEntry} a first lockfile entry + * @param {LockfileEntry} b second lockfile entry + * @returns {boolean} true when equal, otherwise false + */ const areLockfileEntriesEqual = (a, b) => a.resolved === b.resolved && a.integrity === b.integrity && @@ -229,8 +255,8 @@ class Lockfile { /** * @template R - * @param {function(function(Error=, R=): void): void} fn function - * @returns {function(function((Error | null)=, R=): void): void} cached function + * @param {function(function(Error | null, R=): void): void} fn function + * @returns {function(function(Error | null, R=): void): void} cached function */ const cachedWithoutKey = fn => { let inFlight = false; @@ -238,7 +264,7 @@ const cachedWithoutKey = fn => { let cachedError; /** @type {R | undefined} */ let cachedResult; - /** @type {(function(Error=, R=): void)[] | undefined} */ + /** @type {(function(Error| null, R=): void)[] | undefined} */ let cachedCallbacks; return callback => { if (inFlight) { @@ -263,17 +289,22 @@ const cachedWithoutKey = fn => { /** * @template T * @template R - * @param {function(T, function(Error=, R=): void): void} fn function - * @param {function(T, function(Error=, R=): void): void=} forceFn function for the second try - * @returns {(function(T, function((Error | null)=, R=): void): void) & { force: function(T, function((Error | null)=, R=): void): void }} cached function + * @param {function(T, function(Error | null, R=): void): void} fn function + * @param {function(T, function(Error | null, R=): void): void=} forceFn function for the second try + * @returns {(function(T, function(Error | null, R=): void): void) & { force: function(T, function(Error | null, R=): void): void }} cached function */ const cachedWithKey = (fn, forceFn = fn) => { /** * @template R - * @typedef {{ result?: R, error?: Error, callbacks?: (function((Error | null)=, R=): void)[], force?: true }} CacheEntry + * @typedef {{ result?: R, error?: Error, callbacks?: (function(Error | null, R=): void)[], force?: true }} CacheEntry */ /** @type {Map>} */ const cache = new Map(); + /** + * @param {T} arg arg + * @param {function(Error | null, R=): void} callback callback + * @returns {void} + */ const resultFn = (arg, callback) => { const cacheEntry = cache.get(arg); if (cacheEntry !== undefined) { @@ -300,6 +331,11 @@ const cachedWithKey = (fn, forceFn = fn) => { if (callbacks !== undefined) for (const cb of callbacks) cb(err, result); }); }; + /** + * @param {T} arg arg + * @param {function(Error | null, R=): void} callback callback + * @returns {void} + */ resultFn.force = (arg, callback) => { const cacheEntry = cache.get(arg); if (cacheEntry !== undefined && cacheEntry.force) { @@ -330,6 +366,24 @@ const cachedWithKey = (fn, forceFn = fn) => { return resultFn; }; +/** + * @typedef {object} LockfileCache + * @property {Lockfile} lockfile lockfile + * @property {Snapshot} snapshot snapshot + */ + +/** + * @typedef {object} ResolveContentResult + * @property {LockfileEntry} entry lockfile entry + * @property {Buffer} content content + * @property {boolean} storeLock need store lockfile + */ + +/** @typedef {{ storeCache: boolean, storeLock: boolean, validUntil: number, etag: string | undefined, fresh: boolean }} FetchResultMeta */ +/** @typedef {FetchResultMeta & { location: string }} RedirectFetchResult */ +/** @typedef {FetchResultMeta & { entry: LockfileEntry, content: Buffer }} ContentFetchResult */ +/** @typedef {RedirectFetchResult | ContentFetchResult} FetchResult */ + class HttpUriPlugin { /** * @param {HttpUriPluginOptions} options options @@ -362,11 +416,14 @@ class HttpUriPlugin { fetch: proxyFetch(getHttps(), proxy) } ]; + /** @type {LockfileCache} */ let lockfileCache; compiler.hooks.compilation.tap( "HttpUriPlugin", (compilation, { normalModuleFactory }) => { - const intermediateFs = compiler.intermediateFileSystem; + const intermediateFs = + /** @type {IntermediateFileSystem} */ + (compiler.intermediateFileSystem); const fs = compilation.inputFileSystem; const cache = compilation.getCache("webpack.HttpUriPlugin"); const logger = compilation.getLogger("webpack.HttpUriPlugin"); @@ -430,7 +487,7 @@ class HttpUriPlugin { const getLockfile = cachedWithoutKey( /** - * @param {function((Error | null)=, Lockfile=): void} callback callback + * @param {function(Error | null, Lockfile=): void} callback callback * @returns {void} */ callback => { @@ -447,14 +504,14 @@ class HttpUriPlugin { [], buffer ? [] : [lockfileLocation], { timestamp: true }, - (err, snapshot) => { + (err, s) => { if (err) return callback(err); const lockfile = buffer ? Lockfile.parse(buffer.toString("utf-8")) : new Lockfile(); lockfileCache = { lockfile, - snapshot + snapshot: /** @type {Snapshot} */ (s) }; callback(null, lockfile); } @@ -476,7 +533,9 @@ class HttpUriPlugin { } ); - /** @type {Map | undefined} */ + /** @typedef {Map} LockfileUpdates */ + + /** @type {LockfileUpdates | undefined} */ let lockfileUpdates; /** @@ -518,6 +577,13 @@ class HttpUriPlugin { } }; + /** + * @param {Lockfile} lockfile lockfile + * @param {string} url url + * @param {ResolveContentResult} result result + * @param {function(Error | null, ResolveContentResult=): void} callback callback + * @returns {void} + */ const storeResult = (lockfile, url, result, callback) => { if (result.storeLock) { storeLockEntry(lockfile, url, result.entry); @@ -541,10 +607,15 @@ class HttpUriPlugin { for (const { scheme, fetch } of schemes) { /** * @param {string} url URL - * @param {string} integrity integrity - * @param {function((Error | null)=, { entry: LockfileEntry, content: Buffer, storeLock: boolean }=): void} callback callback + * @param {string | null} integrity integrity + * @param {function(Error | null, ResolveContentResult=): void} callback callback */ const resolveContent = (url, integrity, callback) => { + /** + * @param {Error | null} err error + * @param {TODO} result result result + * @returns {void} + */ const handleResult = (err, result) => { if (err) return callback(err); if ("location" in result) { @@ -553,10 +624,12 @@ class HttpUriPlugin { integrity, (err, innerResult) => { if (err) return callback(err); + const { entry, content, storeLock } = + /** @type {ResolveContentResult} */ (innerResult); callback(null, { - entry: innerResult.entry, - content: innerResult.content, - storeLock: innerResult.storeLock && result.storeLock + entry, + content, + storeLock: storeLock && result.storeLock }); } ); @@ -578,15 +651,10 @@ class HttpUriPlugin { fetchContent(url, handleResult); }; - /** @typedef {{ storeCache: boolean, storeLock: boolean, validUntil: number, etag: string | undefined, fresh: boolean }} FetchResultMeta */ - /** @typedef {FetchResultMeta & { location: string }} RedirectFetchResult */ - /** @typedef {FetchResultMeta & { entry: LockfileEntry, content: Buffer }} ContentFetchResult */ - /** @typedef {RedirectFetchResult | ContentFetchResult} FetchResult */ - /** * @param {string} url URL - * @param {FetchResult | RedirectFetchResult} cachedResult result from cache - * @param {function((Error | null)=, FetchResult=): void} callback callback + * @param {FetchResult | RedirectFetchResult | undefined} cachedResult result from cache + * @param {function(Error | null, FetchResult=): void} callback callback * @returns {void} */ const fetchContentRaw = (url, cachedResult, callback) => { @@ -659,22 +727,21 @@ class HttpUriPlugin { ); }; if (res.statusCode === 304) { + const result = /** @type {FetchResult} */ (cachedResult); if ( - cachedResult.validUntil < validUntil || - cachedResult.storeLock !== storeLock || - cachedResult.storeCache !== storeCache || - cachedResult.etag !== etag + result.validUntil < validUntil || + result.storeLock !== storeLock || + result.storeCache !== storeCache || + result.etag !== etag ) { - return finishWith(cachedResult); + return finishWith(result); } logger.debug(`GET ${url} [${res.statusCode}] (unchanged)`); - return callback(null, { - ...cachedResult, - fresh: true - }); + return callback(null, { ...result, fresh: true }); } if ( location && + res.statusCode && res.statusCode >= 301 && res.statusCode <= 308 ) { @@ -703,9 +770,11 @@ class HttpUriPlugin { }); } const contentType = res.headers["content-type"] || ""; + /** @type {Buffer[]} */ const bufferArr = []; const contentEncoding = res.headers["content-encoding"]; + /** @type {Readable} */ let stream = res; if (contentEncoding === "gzip") { stream = stream.pipe(createGunzip()); @@ -757,9 +826,10 @@ class HttpUriPlugin { const fetchContent = cachedWithKey( /** * @param {string} url URL - * @param {function((Error | null)=, { validUntil: number, etag?: string, entry: LockfileEntry, content: Buffer, fresh: boolean } | { validUntil: number, etag?: string, location: string, fresh: boolean }=): void} callback callback + * @param {function(Error | null, { validUntil: number, etag?: string, entry: LockfileEntry, content: Buffer, fresh: boolean } | { validUntil: number, etag?: string, location: string, fresh: boolean }=): void} callback callback * @returns {void} - */ (url, callback) => { + */ + (url, callback) => { cache.get(url, null, (err, cachedResult) => { if (err) return callback(err); if (cachedResult) { @@ -772,6 +842,10 @@ class HttpUriPlugin { (url, callback) => fetchContentRaw(url, undefined, callback) ); + /** + * @param {string} uri uri + * @returns {boolean} true when allowed, otherwise false + */ const isAllowed = uri => { for (const allowed of allowedUris) { if (typeof allowed === "string") { @@ -785,10 +859,12 @@ class HttpUriPlugin { return false; }; + /** @typedef {{ entry: LockfileEntry, content: Buffer }} Info */ + const getInfo = cachedWithKey( /** * @param {string} url the url - * @param {function((Error | null)=, { entry: LockfileEntry, content: Buffer }=): void} callback callback + * @param {function(Error | null, Info=): void} callback callback * @returns {void} */ // eslint-disable-next-line no-loop-func @@ -802,8 +878,9 @@ class HttpUriPlugin { ) ); } - getLockfile((err, lockfile) => { + getLockfile((err, _lockfile) => { if (err) return callback(err); + const lockfile = /** @type {Lockfile} */ (_lockfile); const entryOrString = lockfile.entries.get(url); if (!entryOrString) { if (frozen) { @@ -815,14 +892,24 @@ class HttpUriPlugin { } resolveContent(url, null, (err, result) => { if (err) return callback(err); - storeResult(lockfile, url, result, callback); + storeResult( + /** @type {Lockfile} */ + (lockfile), + url, + /** @type {ResolveContentResult} */ + (result), + callback + ); }); return; } if (typeof entryOrString === "string") { const entryTag = entryOrString; - resolveContent(url, null, (err, result) => { + resolveContent(url, null, (err, _result) => { if (err) return callback(err); + const result = + /** @type {ResolveContentResult} */ + (_result); if (!result.storeLock || entryTag === "ignore") return callback(null, result); if (frozen) { @@ -846,8 +933,11 @@ Remove this line from the lockfile to force upgrading.` return; } let entry = entryOrString; + /** + * @param {Buffer=} lockedContent locked content + */ const doFetch = lockedContent => { - resolveContent(url, entry.integrity, (err, result) => { + resolveContent(url, entry.integrity, (err, _result) => { if (err) { if (lockedContent) { logger.warn( @@ -861,6 +951,9 @@ Remove this line from the lockfile to force upgrading.` } return callback(err); } + const result = + /** @type {ResolveContentResult} */ + (_result); if (!result.storeLock) { // When the lockfile entry should be no-cache // we need to update the lockfile @@ -915,12 +1008,16 @@ Remove this line from the lockfile to force upgrading.` const key = getCacheKey(entry.resolved); const filePath = join(intermediateFs, cacheLocation, key); fs.readFile(filePath, (err, result) => { - const content = /** @type {Buffer} */ (result); if (err) { if (err.code === "ENOENT") return doFetch(); return callback(err); } - const continueWithCachedContent = result => { + const content = /** @type {Buffer} */ (result); + /** + * @param {Buffer | undefined} _result result + * @returns {void} + */ + const continueWithCachedContent = _result => { if (!upgrade) { // When not in upgrade mode, we accept the result from the lockfile cache return callback(null, { entry, content }); @@ -928,6 +1025,7 @@ Remove this line from the lockfile to force upgrading.` return doFetch(content); }; if (!verifyIntegrity(content, entry.integrity)) { + /** @type {Buffer | undefined} */ let contentWithChangedEol; let isEolChanged = false; try { @@ -965,10 +1063,14 @@ This will avoid that the end of line sequence is changed by git on Windows.`; ); intermediateFs.writeFile( filePath, - contentWithChangedEol, + /** @type {Buffer} */ + (contentWithChangedEol), err => { if (err) return callback(err); - continueWithCachedContent(contentWithChangedEol); + continueWithCachedContent( + /** @type {Buffer} */ + (contentWithChangedEol) + ); } ); return; @@ -1008,9 +1110,15 @@ Run build with un-frozen lockfile to automatically fix lockfile.` } ); + /** + * @param {URL} url url + * @param {ResourceDataWithData} resourceData resource data + * @param {function(Error | null, true | void): void} callback callback + */ const respondWithUrlModule = (url, resourceData, callback) => { - getInfo(url.href, (err, result) => { + getInfo(url.href, (err, _result) => { if (err) return callback(err); + const result = /** @type {Info} */ (_result); resourceData.resource = url.href; resourceData.path = url.origin + url.pathname; resourceData.query = url.search; @@ -1055,9 +1163,11 @@ Run build with un-frozen lockfile to automatically fix lockfile.` hooks.readResourceForScheme .for(scheme) .tapAsync("HttpUriPlugin", (resource, module, callback) => - getInfo(resource, (err, result) => { + getInfo(resource, (err, _result) => { if (err) return callback(err); - module.buildInfo.resourceIntegrity = result.entry.integrity; + const result = /** @type {Info} */ (_result); + /** @type {BuildInfo} */ + (module.buildInfo).resourceIntegrity = result.entry.integrity; callback(null, result.content); }) ); @@ -1068,11 +1178,13 @@ Run build with un-frozen lockfile to automatically fix lockfile.` module.resource && module.resource.startsWith(`${scheme}://`) ) { - getInfo(module.resource, (err, result) => { + getInfo(module.resource, (err, _result) => { if (err) return callback(err); + const result = /** @type {Info} */ (_result); if ( result.entry.integrity !== - module.buildInfo.resourceIntegrity + /** @type {BuildInfo} */ + (module.buildInfo).resourceIntegrity ) { return callback(null, true); } @@ -1098,7 +1210,9 @@ Run build with un-frozen lockfile to automatically fix lockfile.` ); const writeDone = () => { - const nextOperation = inProgressWrite.shift(); + const nextOperation = + /** @type {InProgressWriteItem[]} */ + (inProgressWrite).shift(); if (nextOperation) { nextOperation(); } else { @@ -1114,19 +1228,25 @@ Run build with un-frozen lockfile to automatically fix lockfile.` const lockfile = buffer ? Lockfile.parse(buffer.toString("utf-8")) : new Lockfile(); - for (const [key, value] of lockfileUpdates) { + for (const [key, value] of /** @type {LockfileUpdates} */ ( + lockfileUpdates + )) { lockfile.entries.set(key, value); } intermediateFs.writeFile(tempFile, lockfile.toString(), err => { if (err) { writeDone(); - return intermediateFs.unlink(tempFile, () => callback(err)); + return ( + /** @type {NonNullable} */ + (intermediateFs.unlink)(tempFile, () => callback(err)) + ); } intermediateFs.rename(tempFile, lockfileLocation, err => { if (err) { writeDone(); - return intermediateFs.unlink(tempFile, () => - callback(err) + return ( + /** @type {NonNullable} */ + (intermediateFs.unlink)(tempFile, () => callback(err)) ); } writeDone(); diff --git a/lib/stats/DefaultStatsFactoryPlugin.js b/lib/stats/DefaultStatsFactoryPlugin.js index 4e24b961e..cb0373a24 100644 --- a/lib/stats/DefaultStatsFactoryPlugin.js +++ b/lib/stats/DefaultStatsFactoryPlugin.js @@ -26,27 +26,36 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); /** @typedef {import("webpack-sources").Source} Source */ /** @typedef {import("../Chunk")} Chunk */ +/** @typedef {import("../Chunk").ChunkId} ChunkId */ /** @typedef {import("../ChunkGroup")} ChunkGroup */ /** @typedef {import("../ChunkGroup").OriginRecord} OriginRecord */ /** @typedef {import("../Compilation")} Compilation */ /** @typedef {import("../Compilation").Asset} Asset */ /** @typedef {import("../Compilation").AssetInfo} AssetInfo */ +/** @typedef {import("../Compilation").PathData} PathData */ /** @typedef {import("../Compilation").NormalizedStatsOptions} NormalizedStatsOptions */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../ChunkGraph").ModuleId} ModuleId */ /** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */ /** @typedef {import("../Module")} Module */ +/** @typedef {import("../Module").BuildInfo} BuildInfo */ /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */ /** @typedef {import("../ModuleProfile")} ModuleProfile */ /** @typedef {import("../RequestShortener")} RequestShortener */ /** @typedef {import("../WebpackError")} WebpackError */ -/** @template T @typedef {import("../util/comparators").Comparator} Comparator */ +/** + * @template T + * @typedef {import("../util/comparators").Comparator} Comparator + */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ -/** @typedef {import("../util/smartGrouping").GroupConfig} GroupConfig */ +/** + * @template T, R + * @typedef {import("../util/smartGrouping").GroupConfig} GroupConfig + */ /** @typedef {import("./StatsFactory")} StatsFactory */ /** @typedef {import("./StatsFactory").StatsFactoryContext} StatsFactoryContext */ - -/** @typedef {KnownStatsCompilation & Record} StatsCompilation */ +/** @typedef {Record & KnownStatsCompilation} StatsCompilation */ /** * @typedef {object} KnownStatsCompilation * @property {any=} env @@ -74,7 +83,7 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); * @property {Record=} logging */ -/** @typedef {KnownStatsLogging & Record} StatsLogging */ +/** @typedef {Record & KnownStatsLogging} StatsLogging */ /** * @typedef {object} KnownStatsLogging * @property {StatsLoggingEntry[]} entries @@ -82,18 +91,18 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); * @property {boolean} debug */ -/** @typedef {KnownStatsLoggingEntry & Record} StatsLoggingEntry */ +/** @typedef {Record & KnownStatsLoggingEntry} StatsLoggingEntry */ /** * @typedef {object} KnownStatsLoggingEntry * @property {string} type - * @property {string} message + * @property {string=} message * @property {string[]=} trace * @property {StatsLoggingEntry[]=} children * @property {any[]=} args * @property {number=} time */ -/** @typedef {KnownStatsAsset & Record} StatsAsset */ +/** @typedef {Record & KnownStatsAsset} StatsAsset */ /** * @typedef {object} KnownStatsAsset * @property {string} type @@ -114,7 +123,7 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); * @property {boolean=} isOverSizeLimit */ -/** @typedef {KnownStatsChunkGroup & Record} StatsChunkGroup */ +/** @typedef {Record & KnownStatsChunkGroup} StatsChunkGroup */ /** * @typedef {object} KnownStatsChunkGroup * @property {string=} name @@ -130,21 +139,21 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); * @property {boolean=} isOverSizeLimit */ -/** @typedef {KnownStatsModule & Record} StatsModule */ +/** @typedef {Record & KnownStatsModule} StatsModule */ /** * @typedef {object} KnownStatsModule * @property {string=} type * @property {string=} moduleType - * @property {string=} layer + * @property {(string | null)=} layer * @property {string=} identifier * @property {string=} name - * @property {string=} nameForCondition + * @property {(string | null)=} nameForCondition * @property {number=} index * @property {number=} preOrderIndex * @property {number=} index2 * @property {number=} postOrderIndex * @property {number=} size - * @property {{[x: string]: number}=} sizes + * @property {{ [x: string]: number }=} sizes * @property {boolean=} cacheable * @property {boolean=} built * @property {boolean=} codeGenerated @@ -152,29 +161,29 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); * @property {boolean=} cached * @property {boolean=} optional * @property {boolean=} orphan - * @property {string|number=} id - * @property {string|number=} issuerId - * @property {(string|number)[]=} chunks - * @property {(string|number)[]=} assets + * @property {string | number=} id + * @property {string | number | null=} issuerId + * @property {(string | number)[]=} chunks + * @property {(string | number)[]=} assets * @property {boolean=} dependent - * @property {string=} issuer - * @property {string=} issuerName + * @property {(string | null)=} issuer + * @property {(string | null)=} issuerName * @property {StatsModuleIssuer[]=} issuerPath * @property {boolean=} failed * @property {number=} errors * @property {number=} warnings * @property {StatsProfile=} profile * @property {StatsModuleReason[]=} reasons - * @property {(boolean | string[])=} usedExports - * @property {string[]=} providedExports + * @property {(boolean | null | string[])=} usedExports + * @property {(string[] | null)=} providedExports * @property {string[]=} optimizationBailout - * @property {number=} depth + * @property {(number | null)=} depth * @property {StatsModule[]=} modules * @property {number=} filteredModules * @property {ReturnType=} source */ -/** @typedef {KnownStatsProfile & Record} StatsProfile */ +/** @typedef {Record & KnownStatsProfile} StatsProfile */ /** * @typedef {object} KnownStatsProfile * @property {number} total @@ -189,33 +198,33 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); * @property {number} dependencies */ -/** @typedef {KnownStatsModuleIssuer & Record} StatsModuleIssuer */ +/** @typedef {Record & KnownStatsModuleIssuer} StatsModuleIssuer */ /** * @typedef {object} KnownStatsModuleIssuer - * @property {string=} identifier - * @property {string=} name + * @property {string} identifier + * @property {string} name * @property {(string|number)=} id - * @property {StatsProfile=} profile + * @property {StatsProfile} profile */ -/** @typedef {KnownStatsModuleReason & Record} StatsModuleReason */ +/** @typedef {Record & KnownStatsModuleReason} StatsModuleReason */ /** * @typedef {object} KnownStatsModuleReason - * @property {string=} moduleIdentifier - * @property {string=} module - * @property {string=} moduleName - * @property {string=} resolvedModuleIdentifier - * @property {string=} resolvedModule - * @property {string=} type + * @property {string | null} moduleIdentifier + * @property {string | null} module + * @property {string | null} moduleName + * @property {string | null} resolvedModuleIdentifier + * @property {string | null} resolvedModule + * @property {string | null} type * @property {boolean} active - * @property {string=} explanation - * @property {string=} userRequest - * @property {string=} loc - * @property {(string|number)=} moduleId - * @property {(string|number)=} resolvedModuleId + * @property {string | null} explanation + * @property {string | null} userRequest + * @property {(string | null)=} loc + * @property {(string | number | null)=} moduleId + * @property {(string | number | null)=} resolvedModuleId */ -/** @typedef {KnownStatsChunk & Record} StatsChunk */ +/** @typedef {Record & KnownStatsChunk} StatsChunk */ /** * @typedef {object} KnownStatsChunk * @property {boolean} rendered @@ -224,14 +233,14 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); * @property {boolean} recorded * @property {string=} reason * @property {number} size - * @property {Record=} sizes - * @property {string[]=} names - * @property {string[]=} idHints + * @property {Record} sizes + * @property {string[]} names + * @property {string[]} idHints * @property {string[]=} runtime - * @property {string[]=} files - * @property {string[]=} auxiliaryFiles + * @property {string[]} files + * @property {string[]} auxiliaryFiles * @property {string} hash - * @property {Record=} childrenByOrder + * @property {Record} childrenByOrder * @property {(string|number)=} id * @property {(string|number)[]=} siblings * @property {(string|number)[]=} parents @@ -241,18 +250,18 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); * @property {StatsChunkOrigin[]=} origins */ -/** @typedef {KnownStatsChunkOrigin & Record} StatsChunkOrigin */ +/** @typedef {Record & KnownStatsChunkOrigin} StatsChunkOrigin */ /** * @typedef {object} KnownStatsChunkOrigin - * @property {string=} module - * @property {string=} moduleIdentifier - * @property {string=} moduleName - * @property {string=} loc - * @property {string=} request - * @property {(string|number)=} moduleId + * @property {string} module + * @property {string} moduleIdentifier + * @property {string} moduleName + * @property {string} loc + * @property {string} request + * @property {(string | number)=} moduleId */ -/** @typedef {KnownStatsModuleTraceItem & Record} StatsModuleTraceItem */ +/** @typedef { Record & KnownStatsModuleTraceItem} StatsModuleTraceItem */ /** * @typedef {object} KnownStatsModuleTraceItem * @property {string=} originIdentifier @@ -264,13 +273,13 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); * @property {(string|number)=} moduleId */ -/** @typedef {KnownStatsModuleTraceDependency & Record} StatsModuleTraceDependency */ +/** @typedef {Record & KnownStatsModuleTraceDependency} StatsModuleTraceDependency */ /** * @typedef {object} KnownStatsModuleTraceDependency * @property {string=} loc */ -/** @typedef {KnownStatsError & Record} StatsError */ +/** @typedef {Record & KnownStatsError} StatsError */ /** * @typedef {object} KnownStatsError * @property {string} message @@ -281,14 +290,14 @@ const { makePathsRelative, parseResource } = require("../util/identifier"); * @property {string=} moduleIdentifier * @property {string=} moduleName * @property {string=} loc - * @property {string|number=} chunkId + * @property {ChunkId=} chunkId * @property {string|number=} moduleId * @property {StatsModuleTraceItem[]=} moduleTrace * @property {any=} details * @property {string=} stack */ -/** @typedef {Asset & { type: string, related: PreprocessedAsset[] }} PreprocessedAsset */ +/** @typedef {Asset & { type: string, related: PreprocessedAsset[] | undefined }} PreprocessedAsset */ /** * @template T @@ -347,8 +356,8 @@ const uniqueOrderedArray = (items, selector, comparator) => /** @template T @template R @typedef {{ [P in keyof T]: R }} MappedValues */ /** - * @template T - * @template R + * @template {object} T + * @template {object} R * @param {T} obj object to be mapped * @param {function(T[keyof T], keyof T): R} fn mapping function * @returns {MappedValues} mapped object @@ -356,7 +365,10 @@ const uniqueOrderedArray = (items, selector, comparator) => const mapObject = (obj, fn) => { const newObj = Object.create(null); for (const key of Object.keys(obj)) { - newObj[key] = fn(obj[key], /** @type {keyof T} */ (key)); + newObj[key] = fn( + obj[/** @type {keyof T} */ (key)], + /** @type {keyof T} */ (key) + ); } return newObj; }; @@ -404,10 +416,12 @@ const EXTRACT_ERROR = { ids: (object, error, { compilation: { chunkGraph } }) => { if (typeof error !== "string") { if (error.chunk) { - object.chunkId = error.chunk.id; + object.chunkId = /** @type {ChunkId} */ (error.chunk.id); } if (error.module) { - object.moduleId = chunkGraph.getModuleId(error.module); + object.moduleId = + /** @type {ModuleId} */ + (chunkGraph.getModuleId(error.module)); } } }, @@ -578,7 +592,9 @@ const SIMPLE_EXTRACTORS = { groupStack.pop(); currentList = groupStack.length > 0 - ? groupStack[groupStack.length - 1].children + ? /** @type {KnownStatsLoggingEntry[]} */ ( + groupStack[groupStack.length - 1].children + ) : rootList; if (depthInCollapsedGroup > 0) depthInCollapsedGroup--; continue; @@ -630,7 +646,7 @@ const SIMPLE_EXTRACTORS = { } }, hash: (object, compilation) => { - object.hash = compilation.hash; + object.hash = /** @type {string} */ (compilation.hash); }, version: object => { object.version = require("../../package.json").version; @@ -639,18 +655,23 @@ const SIMPLE_EXTRACTORS = { object.env = _env; }, timings: (object, compilation) => { - object.time = compilation.endTime - compilation.startTime; + object.time = + /** @type {number} */ (compilation.endTime) - + /** @type {number} */ (compilation.startTime); }, builtAt: (object, compilation) => { - object.builtAt = compilation.endTime; + object.builtAt = /** @type {number} */ (compilation.endTime); }, publicPath: (object, compilation) => { object.publicPath = compilation.getPath( - compilation.outputOptions.publicPath + /** @type {string | function(PathData, AssetInfo=): string} */ + (compilation.outputOptions.publicPath) ); }, outputPath: (object, compilation) => { - object.outputPath = compilation.outputOptions.path; + object.outputPath = /** @type {string} */ ( + compilation.outputOptions.path + ); }, assets: (object, compilation, context, options, factory) => { const { type } = context; @@ -735,7 +756,10 @@ const SIMPLE_EXTRACTORS = { compilationAuxiliaryFileToChunks } ); - const limited = spaceLimited(groupedAssets, options.assetsSpace); + const limited = spaceLimited( + groupedAssets, + /** @type {number} */ (options.assetsSpace) + ); object.assets = limited.children; object.filteredAssets = limited.filteredChildren; }, @@ -828,7 +852,8 @@ const SIMPLE_EXTRACTORS = { } const [errors, filteredBySpace] = errorsSpaceLimit( factorizedErrors, - options.errorsSpace + /** @type {number} */ + (options.errorsSpace) ); object.filteredErrorDetailsCount = filtered + filteredBySpace; object.errors = errors; @@ -861,7 +886,8 @@ const SIMPLE_EXTRACTORS = { } const [warnings, filteredBySpace] = errorsSpaceLimit( rawWarnings, - options.warningsSpace + /** @type {number} */ + (options.warningsSpace) ); object.filteredWarningDetailsCount = filtered + filteredBySpace; object.warnings = warnings; @@ -875,18 +901,31 @@ const SIMPLE_EXTRACTORS = { ) => { const { type, cachedGetWarnings } = context; object.warningsCount = countWithChildren(compilation, (c, childType) => { - if (!warningsFilter && warningsFilter.length === 0) + if ( + !warningsFilter && + /** @type {((warning: StatsError, textValue: string) => boolean)[]} */ + (warningsFilter).length === 0 + ) return cachedGetWarnings(c); return factory .create(`${type}${childType}.warnings`, cachedGetWarnings(c), context) - .filter(warning => { - const warningString = Object.keys(warning) - .map(key => `${warning[key]}`) - .join("\n"); - return !warningsFilter.some(filter => - filter(warning, warningString) - ); - }); + .filter( + /** + * @param {TODO} warning warning + * @returns {boolean} result + */ + warning => { + const warningString = Object.keys(warning) + .map( + key => + `${warning[/** @type {keyof KnownStatsError} */ (key)]}` + ) + .join("\n"); + return !warningsFilter.some(filter => + filter(warning, warningString) + ); + } + ); }); }, children: (object, compilation, context, options, factory) => { @@ -958,7 +997,8 @@ const SIMPLE_EXTRACTORS = { context ); object.filteredRelated = asset.related - ? asset.related.length - object.related.length + ? asset.related.length - + /** @type {StatsAsset[]} */ (object.related).length : undefined; }, ids: ( @@ -969,10 +1009,14 @@ const SIMPLE_EXTRACTORS = { const chunks = compilationFileToChunks.get(asset.name) || []; const auxiliaryChunks = compilationAuxiliaryFileToChunks.get(asset.name) || []; - object.chunks = uniqueOrderedArray(chunks, c => c.ids, compareIds); + object.chunks = uniqueOrderedArray( + chunks, + c => /** @type {ChunkId[]} */ (c.ids), + compareIds + ); object.auxiliaryChunks = uniqueOrderedArray( auxiliaryChunks, - c => c.ids, + c => /** @type {ChunkId[]} */ (c.ids), compareIds ); }, @@ -998,7 +1042,7 @@ const SIMPLE_EXTRACTORS = { const asset = compilation.getAsset(name); return { name, - size: asset ? asset.info.size : -1 + size: /** @type {number} */ (asset ? asset.info.size : -1) }; }; /** @type {(total: number, asset: { size: number }) => number} */ @@ -1014,7 +1058,9 @@ const SIMPLE_EXTRACTORS = { /** @type {KnownStatsChunkGroup} */ const statsChunkGroup = { name, - chunks: ids ? chunkGroup.chunks.map(c => c.id) : undefined, + chunks: ids + ? /** @type {ChunkId[]} */ (chunkGroup.chunks.map(c => c.id)) + : undefined, assets: assets.length <= chunkGroupMaxAssets ? assets : undefined, filteredAssets: assets.length <= chunkGroupMaxAssets ? 0 : assets.length, @@ -1043,7 +1089,10 @@ const SIMPLE_EXTRACTORS = { /** @type {KnownStatsChunkGroup} */ const childStatsChunkGroup = { name: group.name, - chunks: ids ? group.chunks.map(c => c.id) : undefined, + chunks: ids + ? /** @type {ChunkId[]} */ + (group.chunks.map(c => c.id)) + : undefined, assets: assets.length <= chunkGroupMaxAssets ? assets : undefined, filteredAssets: @@ -1087,7 +1136,8 @@ const SIMPLE_EXTRACTORS = { }, module: { _: (object, module, context, options, factory) => { - const { compilation, type } = context; + const { type } = context; + const compilation = /** @type {Compilation} */ (context.compilation); const built = compilation.builtModules.has(module); const codeGenerated = compilation.codeGeneratedModules.has(module); const buildTimeExecuted = @@ -1121,7 +1171,8 @@ const SIMPLE_EXTRACTORS = { }, module$visible: { _: (object, module, context, { requestShortener }, factory) => { - const { compilation, type, rootModules } = context; + const { type, rootModules } = context; + const compilation = /** @type {Compilation} */ (context.compilation); const { moduleGraph } = compilation; /** @type {Module[]} */ const path = []; @@ -1148,11 +1199,15 @@ const SIMPLE_EXTRACTORS = { identifier: module.identifier(), name: module.readableIdentifier(requestShortener), nameForCondition: module.nameForCondition(), - index: moduleGraph.getPreOrderIndex(module), - preOrderIndex: moduleGraph.getPreOrderIndex(module), - index2: moduleGraph.getPostOrderIndex(module), - postOrderIndex: moduleGraph.getPostOrderIndex(module), - cacheable: module.buildInfo.cacheable, + index: /** @type {number} */ (moduleGraph.getPreOrderIndex(module)), + preOrderIndex: /** @type {number} */ ( + moduleGraph.getPreOrderIndex(module) + ), + index2: /** @type {number} */ (moduleGraph.getPostOrderIndex(module)), + postOrderIndex: /** @type {number} */ ( + moduleGraph.getPostOrderIndex(module) + ), + cacheable: /** @type {BuildInfo} */ (module.buildInfo).cacheable, optional: module.isOptional(moduleGraph), orphan: !type.endsWith("module.modules[].module$visible") && @@ -1177,17 +1232,24 @@ const SIMPLE_EXTRACTORS = { } }, ids: (object, module, { compilation: { chunkGraph, moduleGraph } }) => { - object.id = chunkGraph.getModuleId(module); + object.id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module)); const issuer = moduleGraph.getIssuer(module); object.issuerId = issuer && chunkGraph.getModuleId(issuer); - object.chunks = Array.from( - chunkGraph.getOrderedModuleChunksIterable(module, compareChunksById), - chunk => chunk.id - ); + object.chunks = + /** @type {ChunkId[]} */ + ( + Array.from( + chunkGraph.getOrderedModuleChunksIterable( + module, + compareChunksById + ), + chunk => chunk.id + ) + ); }, moduleAssets: (object, module) => { - object.assets = module.buildInfo.assets - ? Object.keys(module.buildInfo.assets) + object.assets = /** @type {BuildInfo} */ (module.buildInfo).assets + ? Object.keys(/** @type {BuildInfo} */ (module.buildInfo).assets) : []; }, reasons: (object, module, context, options, factory) => { @@ -1200,7 +1262,11 @@ const SIMPLE_EXTRACTORS = { Array.from(moduleGraph.getIncomingConnections(module)), context ); - const limited = spaceLimited(groupsReasons, options.reasonsSpace); + const limited = spaceLimited( + groupsReasons, + /** @type {number} */ + (options.reasonsSpace) + ); object.reasons = limited.children; object.filteredReasons = limited.filteredChildren; }, @@ -1293,10 +1359,11 @@ const SIMPLE_EXTRACTORS = { }, moduleIssuer: { _: (object, module, context, { requestShortener }, factory) => { - const { compilation, type } = context; + const { type } = context; + const compilation = /** @type {Compilation} */ (context.compilation); const { moduleGraph } = compilation; const profile = moduleGraph.getProfile(module); - /** @type {KnownStatsModuleIssuer} */ + /** @type {Partial} */ const statsModuleIssuer = { identifier: module.identifier(), name: module.readableIdentifier(requestShortener) @@ -1307,7 +1374,7 @@ const SIMPLE_EXTRACTORS = { } }, ids: (object, module, { compilation: { chunkGraph } }) => { - object.id = chunkGraph.getModuleId(module); + object.id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module)); } }, moduleReason: { @@ -1377,13 +1444,13 @@ const SIMPLE_EXTRACTORS = { : Array.from(chunk.runtime.sort(), makePathsRelative), files: Array.from(chunk.files), auxiliaryFiles: Array.from(chunk.auxiliaryFiles).sort(compareIds), - hash: chunk.renderedHash, + hash: /** @type {string} */ (chunk.renderedHash), childrenByOrder: childIdByOrder }; Object.assign(object, statsChunk); }, ids: (object, chunk) => { - object.id = chunk.id; + object.id = /** @type {ChunkId} */ (chunk.id); }, chunkRelations: (object, chunk, { compilation: { chunkGraph } }) => { /** @type {Set} */ @@ -1396,16 +1463,17 @@ const SIMPLE_EXTRACTORS = { for (const chunkGroup of chunk.groupsIterable) { for (const parentGroup of chunkGroup.parentsIterable) { for (const chunk of parentGroup.chunks) { - parents.add(chunk.id); + parents.add(/** @type {ChunkId} */ (chunk.id)); } } for (const childGroup of chunkGroup.childrenIterable) { for (const chunk of childGroup.chunks) { - children.add(chunk.id); + children.add(/** @type {ChunkId} */ (chunk.id)); } } for (const sibling of chunkGroup.chunks) { - if (sibling !== chunk) siblings.add(sibling.id); + if (sibling !== chunk) + siblings.add(/** @type {ChunkId} */ (sibling.id)); } } object.siblings = Array.from(siblings).sort(compareIds); @@ -1467,7 +1535,7 @@ const SIMPLE_EXTRACTORS = { }, ids: (object, origin, { compilation: { chunkGraph } }) => { object.moduleId = origin.module - ? chunkGraph.getModuleId(origin.module) + ? /** @type {ModuleId} */ (chunkGraph.getModuleId(origin.module)) : undefined; } }, @@ -1495,8 +1563,12 @@ const SIMPLE_EXTRACTORS = { ); }, ids: (object, { origin, module }, { compilation: { chunkGraph } }) => { - object.originId = chunkGraph.getModuleId(origin); - object.moduleId = chunkGraph.getModuleId(module); + object.originId = + /** @type {ModuleId} */ + (chunkGraph.getModuleId(origin)); + object.moduleId = + /** @type {ModuleId} */ + (chunkGraph.getModuleId(module)); } }, moduleTraceDependency: { @@ -1520,13 +1592,13 @@ const FILTER = { } }; -/** @type {Record boolean | undefined>>} */ +/** @type {Record boolean | undefined>>} */ const FILTER_RESULTS = { "compilation.warnings": { warningsFilter: util.deprecate( (warning, context, { warningsFilter }) => { const warningString = Object.keys(warning) - .map(key => `${warning[key]}`) + .map(key => `${warning[/** @type {keyof KnownStatsError} */ (key)]}`) .join("\n"); return !warningsFilter.some(filter => filter(warning, warningString)); }, @@ -1543,7 +1615,7 @@ const MODULES_SORTER = { compareSelect( /** * @param {Module} m module - * @returns {number} depth + * @returns {number | null} depth */ m => moduleGraph.getDepth(m), compareNumbers @@ -1551,7 +1623,7 @@ const MODULES_SORTER = { compareSelect( /** * @param {Module} m module - * @returns {number} index + * @returns {number | null} index */ m => moduleGraph.getPreOrderIndex(m), compareNumbers @@ -1620,6 +1692,16 @@ const SORTERS = { } }; +/** + * @template T + * @typedef {T & { children: Children[] | undefined, filteredChildren?: number }} Children + */ + +/** + * @template T + * @param {Children} item item + * @returns {number} item size + */ const getItemSize = item => // Each item takes 1 line // + the size of the children @@ -1629,6 +1711,12 @@ const getItemSize = item => : item.filteredChildren ? 2 + getTotalSize(item.children) : 1 + getTotalSize(item.children); + +/** + * @template T + * @param {Children[]} children children + * @returns {number} total size + */ const getTotalSize = children => { let size = 0; for (const child of children) { @@ -1637,6 +1725,11 @@ const getTotalSize = children => { return size; }; +/** + * @template T + * @param {Children[]} children children + * @returns {number} total items + */ const getTotalItems = children => { let count = 0; for (const child of children) { @@ -1650,6 +1743,11 @@ const getTotalItems = children => { return count; }; +/** + * @template T + * @param {Children[]} children children + * @returns {Children[]} collapsed children + */ const collapse = children => { // After collapse each child must take exactly one line const newChildren = []; @@ -1669,24 +1767,33 @@ const collapse = children => { return newChildren; }; +/** + * @template T + * @param {Children[]} itemsAndGroups item and groups + * @param {number} max max + * @param {boolean=} filteredChildrenLineReserved filtered children line reserved + * @returns {Children} result + */ const spaceLimited = ( itemsAndGroups, max, filteredChildrenLineReserved = false ) => { if (max < 1) { - return { + return /** @type {Children} */ ({ children: undefined, filteredChildren: getTotalItems(itemsAndGroups) - }; + }); } - /** @type {any[] | undefined} */ + /** @type {Children[] | undefined} */ let children; /** @type {number | undefined} */ let filteredChildren; // This are the groups, which take 1+ lines each + /** @type {Children[] | undefined} */ const groups = []; // The sizes of the groups are stored in groupSizes + /** @type {number[]} */ const groupSizes = []; // This are the items, which take 1 line each const items = []; @@ -1749,7 +1856,7 @@ const spaceLimited = ( // So it should always end up being smaller const headerSize = group.filteredChildren ? 2 : 1; const limited = spaceLimited( - group.children, + /** @type {Children} */ (group.children), maxGroupSize - // we should use ceil to always feet in max Math.ceil(oversize / groups.length) - @@ -1784,12 +1891,14 @@ const spaceLimited = ( } } - return { - children, - filteredChildren - }; + return /** @type {Children} */ ({ children, filteredChildren }); }; +/** + * @param {StatsError[]} errors errors + * @param {number} max max + * @returns {[StatsError[], number]} error space limit + */ const errorsSpaceLimit = (errors, max) => { let filtered = 0; // Can not fit into limit @@ -1845,18 +1954,30 @@ const errorsSpaceLimit = (errors, max) => { return [result, filtered]; }; +/** + * @template {{ size: number }} T + * @template {{ size: number }} R + * @param {(R | T)[]} children children + * @param {T[]} assets assets + * @returns {{ size: number }} asset size + */ const assetGroup = (children, assets) => { let size = 0; for (const asset of children) { size += asset.size; } - return { - size - }; + return { size }; }; +/** + * @template {{ size: number, sizes: Record }} T + * @param {Children[]} children children + * @param {KnownStatsModule[]} modules modules + * @returns {{ size: number, sizes: Record}} size and sizes + */ const moduleGroup = (children, modules) => { let size = 0; + /** @type {Record} */ const sizes = {}; for (const module of children) { size += module.size; @@ -1870,6 +1991,12 @@ const moduleGroup = (children, modules) => { }; }; +/** + * @template {{ active: boolean }} T + * @param {Children[]} children children + * @param {KnownStatsModuleReason[]} reasons reasons + * @returns {{ active: boolean }} reason group + */ const reasonGroup = (children, reasons) => { let active = false; for (const reason of children) { @@ -1883,9 +2010,15 @@ const reasonGroup = (children, reasons) => { const GROUP_EXTENSION_REGEXP = /(\.[^.]+?)(?:\?|(?: \+ \d+ modules?)?$)/; const GROUP_PATH_REGEXP = /(.+)[/\\][^/\\]+?(?:\?|(?: \+ \d+ modules?)?$)/; -/** @type {Record void>} */ +/** @typedef {Record[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} AssetsGroupers */ + +/** @type {AssetsGroupers} */ const ASSETS_GROUPERS = { _: (groupConfigs, context, options) => { + /** + * @param {keyof KnownStatsAsset} name name + * @param {boolean=} exclude need exclude? + */ const groupByFlag = (name, exclude) => { groupConfigs.push({ getKeys: asset => (asset[name] ? ["1"] : undefined), @@ -1959,6 +2092,9 @@ const ASSETS_GROUPERS = { } }, groupAssetsByInfo: (groupConfigs, context, options) => { + /** + * @param {string} name name + */ const groupByAssetInfoFlag = name => { groupConfigs.push({ getKeys: asset => (asset.info && asset.info[name] ? ["1"] : undefined), @@ -1977,9 +2113,12 @@ const ASSETS_GROUPERS = { groupByAssetInfoFlag("hotModuleReplacement"); }, groupAssetsByChunk: (groupConfigs, context, options) => { + /** + * @param {keyof KnownStatsAsset} name name + */ const groupByNames = name => { groupConfigs.push({ - getKeys: asset => asset[name], + getKeys: asset => /** @type {string[]} */ (asset[name]), createGroup: (key, children, assets) => ({ type: "assets by chunk", [name]: [key], @@ -2013,9 +2152,16 @@ const ASSETS_GROUPERS = { } }; -/** @type {function("module" | "chunk" | "root-of-chunk" | "nested"): Record void>} */ +/** @typedef {Record[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} ModulesGroupers */ + +/** @type {function("module" | "chunk" | "root-of-chunk" | "nested"): ModulesGroupers} */ const MODULES_GROUPERS = type => ({ _: (groupConfigs, context, options) => { + /** + * @param {keyof KnownStatsModule} name name + * @param {string} type type + * @param {boolean=} exclude need exclude? + */ const groupByFlag = (name, type, exclude) => { groupConfigs.push({ getKeys: module => (module[name] ? ["1"] : undefined), @@ -2091,7 +2237,7 @@ const MODULES_GROUPERS = type => ({ } if (groupModulesByLayer) { groupConfigs.push({ - getKeys: module => [module.layer], + getKeys: module => /** @type {string[]} */ ([module.layer]), createGroup: (key, children, modules) => ({ type: "modules by layer", layer: key, @@ -2104,7 +2250,9 @@ const MODULES_GROUPERS = type => ({ groupConfigs.push({ getKeys: module => { if (!module.name) return; - const resource = parseResource(module.name.split("!").pop()).path; + const resource = parseResource( + /** @type {string} */ (module.name.split("!").pop()) + ).path; const dataUrl = /^data:[^,;]+/.exec(resource); if (dataUrl) return [dataUrl[0]]; const extensionMatch = @@ -2168,7 +2316,24 @@ const MODULES_GROUPERS = type => ({ } }); -/** @type {Record void>>} */ +/** @typedef {Record[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} ModuleReasonsGroupers */ + +/** @type {ModuleReasonsGroupers} */ +const MODULE_REASONS_GROUPERS = { + groupReasonsByOrigin: groupConfigs => { + groupConfigs.push({ + getKeys: reason => /** @type {string[]} */ ([reason.module]), + createGroup: (key, children, reasons) => ({ + type: "from origin", + module: key, + children, + ...reasonGroup(children, reasons) + }) + }); + } +}; + +/** @type {Record} */ const RESULT_GROUPERS = { "compilation.assets": ASSETS_GROUPERS, "asset.related": ASSETS_GROUPERS, @@ -2176,22 +2341,14 @@ const RESULT_GROUPERS = { "chunk.modules": MODULES_GROUPERS("chunk"), "chunk.rootModules": MODULES_GROUPERS("root-of-chunk"), "module.modules": MODULES_GROUPERS("nested"), - "module.reasons": { - groupReasonsByOrigin: groupConfigs => { - groupConfigs.push({ - getKeys: reason => [reason.module], - createGroup: (key, children, reasons) => ({ - type: "from origin", - module: key, - children, - ...reasonGroup(children, reasons) - }) - }); - } - } + "module.reasons": MODULE_REASONS_GROUPERS }; // remove a prefixed "!" that can be specified to reverse sort order +/** + * @param {string} field a field name + * @returns {field} normalized field + */ const normalizeFieldKey = field => { if (field[0] === "!") { return field.slice(1); @@ -2200,6 +2357,10 @@ const normalizeFieldKey = field => { }; // if a field is prefixed by a "!" reverse sort order +/** + * @param {string} field a field name + * @returns {boolean} result + */ const sortOrderRegular = field => { if (field[0] === "!") { return false; @@ -2209,7 +2370,7 @@ const sortOrderRegular = field => { /** * @param {string} field field name - * @returns {function(object, object): number} comparators + * @returns {function(object, object): 0 | 1 | -1} comparators */ const sortByField = field => { if (!field) { @@ -2237,8 +2398,8 @@ const sortByField = field => { return sortFn; }; +/** @type {Record[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} */ const ASSET_SORTERS = { - /** @type {(comparators: Function[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void} */ assetsSort: (comparators, context, { assetsSort }) => { comparators.push(sortByField(assetsSort)); }, @@ -2247,7 +2408,7 @@ const ASSET_SORTERS = { } }; -/** @type {Record void>>} */ +/** @type {Record[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>>} */ const RESULT_SORTERS = { "compilation.chunks": { chunksSort: (comparators, context, { chunksSort }) => { @@ -2324,8 +2485,14 @@ const ITEM_NAMES = { }; /** - * @param {object[]} items items to be merged - * @returns {object} an object + * @template T + * @typedef {{ name: T }} NamedObject + */ + +/** + * @template {{ name: string }} T + * @param {T[]} items items to be merged + * @returns {NamedObject} an object */ const mergeToObject = items => { const obj = Object.create(null); @@ -2335,7 +2502,10 @@ const mergeToObject = items => { return obj; }; -/** @type {Record any>} */ +/** + * @template {{ name: string }} T + * @type {Record NamedObject>} + */ const MERGER = { "compilation.entrypoints": mergeToObject, "compilation.namedChunkGroups": mergeToObject @@ -2351,7 +2521,11 @@ class DefaultStatsFactoryPlugin { compiler.hooks.compilation.tap("DefaultStatsFactoryPlugin", compilation => { compilation.hooks.statsFactory.tap( "DefaultStatsFactoryPlugin", - (stats, options, context) => { + /** + * @param {StatsFactory} stats stats factory + * @param {NormalizedStatsOptions} options stats options + */ + (stats, options) => { iterateConfig(SIMPLE_EXTRACTORS, options, (hookFor, fn) => { stats.hooks.extract .for(hookFor) @@ -2408,19 +2582,27 @@ class DefaultStatsFactoryPlugin { if (Array.isArray(options.children)) { stats.hooks.getItemFactory .for("compilation.children[].compilation") - .tap("DefaultStatsFactoryPlugin", (comp, { _index: idx }) => { - if (idx < options.children.length) { - return compilation.createStatsFactory( - compilation.createStatsOptions( - options.children[idx], - context - ) - ); + .tap( + "DefaultStatsFactoryPlugin", + /** + * @param {Compilation} comp compilation + * @param {StatsFactoryContext} options options + * @returns {StatsFactory | undefined} stats factory + */ + (comp, { _index: idx }) => { + const children = + /** @type {TODO} */ + (options.children); + if (idx < children.length) { + return compilation.createStatsFactory( + compilation.createStatsOptions(children[idx]) + ); + } } - }); + ); } else if (options.children !== true) { const childFactory = compilation.createStatsFactory( - compilation.createStatsOptions(options.children, context) + compilation.createStatsOptions(options.children) ); stats.hooks.getItemFactory .for("compilation.children[].compilation") diff --git a/lib/stats/DefaultStatsPresetPlugin.js b/lib/stats/DefaultStatsPresetPlugin.js index 983755904..70e56b8cb 100644 --- a/lib/stats/DefaultStatsPresetPlugin.js +++ b/lib/stats/DefaultStatsPresetPlugin.js @@ -11,15 +11,24 @@ const RequestShortener = require("../RequestShortener"); /** @typedef {import("../Compilation")} Compilation */ /** @typedef {import("../Compilation").CreateStatsOptionsContext} CreateStatsOptionsContext */ /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("./DefaultStatsFactoryPlugin").StatsError} StatsError */ +/** + * @param {StatsOptions} options options + * @param {StatsOptions} defaults default options + */ const applyDefaults = (options, defaults) => { - for (const key of Object.keys(defaults)) { + for (const _k of Object.keys(defaults)) { + const key = /** @type {keyof StatsOptions} */ (_k); if (typeof options[key] === "undefined") { - options[key] = defaults[key]; + /** @type {TODO} */ + (options)[key] = defaults[key]; } } }; +/** @typedef {Record} NamedPresets */ +/** @type {NamedPresets} */ const NAMED_PRESETS = { verbose: { hash: true, @@ -126,12 +135,35 @@ const NAMED_PRESETS = { } }; +/** + * @param {StatsOptions} all stats option + * @returns {boolean} true when enabled, otherwise false + */ const NORMAL_ON = ({ all }) => all !== false; +/** + * @param {StatsOptions} all stats option + * @returns {boolean} true when enabled, otherwise false + */ const NORMAL_OFF = ({ all }) => all === true; +/** + * @param {StatsOptions} all stats option + * @param {CreateStatsOptionsContext} forToString stats options context + * @returns {boolean} true when enabled, otherwise false + */ const ON_FOR_TO_STRING = ({ all }, { forToString }) => forToString ? all !== false : all === true; +/** + * @param {StatsOptions} all stats option + * @param {CreateStatsOptionsContext} forToString stats options context + * @returns {boolean} true when enabled, otherwise false + */ const OFF_FOR_TO_STRING = ({ all }, { forToString }) => forToString ? all === true : all !== false; +/** + * @param {StatsOptions} all stats option + * @param {CreateStatsOptionsContext} forToString stats options context + * @returns {boolean | "auto"} true when enabled, otherwise false + */ const AUTO_FOR_TO_STRING = ({ all }, { forToString }) => { if (all === false) return false; if (all === true) return true; @@ -139,13 +171,19 @@ const AUTO_FOR_TO_STRING = ({ all }, { forToString }) => { return true; }; -/** @type {Record any>} */ +/** @typedef {Record StatsOptions[keyof StatsOptions] | RequestShortener>} Defaults */ + +/** @type {Defaults} */ const DEFAULTS = { context: (options, context, compilation) => compilation.compiler.context, requestShortener: (options, context, compilation) => compilation.compiler.context === options.context ? compilation.requestShortener - : new RequestShortener(options.context, compilation.compiler.root), + : new RequestShortener( + /** @type {string} */ + (options.context), + compilation.compiler.root + ), performance: NORMAL_ON, hash: OFF_FOR_TO_STRING, env: NORMAL_OFF, @@ -235,6 +273,10 @@ const DEFAULTS = { colors: () => false }; +/** + * @param {string | ({ test: function(string): boolean }) | (function(string): boolean) | boolean} item item to normalize + * @returns {(function(string): boolean) | undefined} normalize fn + */ const normalizeFilter = item => { if (typeof item === "string") { const regExp = new RegExp( @@ -253,6 +295,7 @@ const normalizeFilter = item => { } }; +/** @type {Record} */ const NORMALIZER = { excludeModules: value => { if (!Array.isArray(value)) { @@ -270,20 +313,32 @@ const NORMALIZER = { if (!Array.isArray(value)) { value = value ? [value] : []; } - return value.map(filter => { - if (typeof filter === "string") { - return (warning, warningString) => warningString.includes(filter); + /** + * @callback WarningFilterFn + * @param {StatsError} warning warning + * @param {string} warningString warning string + * @returns {boolean} result + */ + return value.map( + /** + * @param {StatsOptions["warningsFilter"]} filter a warning filter + * @returns {WarningFilterFn} result + */ + filter => { + if (typeof filter === "string") { + return (warning, warningString) => warningString.includes(filter); + } + if (filter instanceof RegExp) { + return (warning, warningString) => filter.test(warningString); + } + if (typeof filter === "function") { + return filter; + } + throw new Error( + `Can only filter warnings with Strings or RegExps. (Given: ${filter})` + ); } - if (filter instanceof RegExp) { - return (warning, warningString) => filter.test(warningString); - } - if (typeof filter === "function") { - return filter; - } - throw new Error( - `Can only filter warnings with Strings or RegExps. (Given: ${filter})` - ); - }); + ); }, logging: value => { if (value === true) value = "log"; @@ -306,7 +361,7 @@ class DefaultStatsPresetPlugin { apply(compiler) { compiler.hooks.compilation.tap("DefaultStatsPresetPlugin", compilation => { for (const key of Object.keys(NAMED_PRESETS)) { - const defaults = NAMED_PRESETS[key]; + const defaults = NAMED_PRESETS[/** @type {keyof NamedPresets} */ (key)]; compilation.hooks.statsPreset .for(key) .tap("DefaultStatsPresetPlugin", (options, context) => { diff --git a/lib/stats/DefaultStatsPrinterPlugin.js b/lib/stats/DefaultStatsPrinterPlugin.js index a3c755ffe..419311a72 100644 --- a/lib/stats/DefaultStatsPrinterPlugin.js +++ b/lib/stats/DefaultStatsPrinterPlugin.js @@ -6,7 +6,10 @@ "use strict"; /** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("./DefaultStatsFactoryPlugin").KnownStatsChunkGroup} KnownStatsChunkGroup */ /** @typedef {import("./StatsPrinter")} StatsPrinter */ +/** @typedef {import("./StatsPrinter").KnownStatsPrinterColorFn} KnownStatsPrinterColorFn */ +/** @typedef {import("./StatsPrinter").KnownStatsPrinterFormaters} KnownStatsPrinterFormaters */ /** @typedef {import("./StatsPrinter").StatsPrinterContext} StatsPrinterContext */ const DATA_URI_CONTENT_LENGTH = 16; @@ -22,9 +25,8 @@ const plural = (n, singular, plural) => (n === 1 ? singular : plural); /** * @param {Record} sizes sizes by source type - * @param {object} options options - * @param {(number) => string=} options.formatSize size formatter - * @returns {string} text + * @param {StatsPrinterContext} options options + * @returns {string | undefined} text */ const printSizes = (sizes, { formatSize = n => `${n}` }) => { const keys = Object.keys(sizes); @@ -56,7 +58,9 @@ const getResourceName = resource => { * @returns {[string,string]} prefix and module name */ const getModuleName = name => { - const [, prefix, resource] = /^(.*!)?([^!]*)$/.exec(name); + const [, prefix, resource] = + /** @type {[any, string, string]} */ + (/** @type {unknown} */ (/^(.*!)?([^!]*)$/.exec(name))); if (resource.length > MAX_MODULE_IDENTIFIER_LENGTH) { const truncatedResource = `${resource.slice( @@ -86,19 +90,29 @@ const mapLines = (str, fn) => str.split("\n").map(fn).join("\n"); */ const twoDigit = n => (n >= 10 ? `${n}` : `0${n}`); +/** + * @param {string | number} id an id + * @returns {boolean | string} is i + */ const isValidId = id => typeof id === "number" || id; /** * @template T - * @param {Array} list of items + * @param {Array | undefined} list of items * @param {number} count number of items to show * @returns {string} string representation of list */ const moreCount = (list, count) => list && list.length > 0 ? `+ ${count}` : `${count}`; -/** @type {Record string | void>} */ -const SIMPLE_PRINTERS = { +/** + * @template T + * @template {keyof T} K + * @typedef {{ [P in K]-?: T[P] }} WithRequired + */ + +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const COMPILATION_SIMPLE_PRINTERS = { "compilation.summary!": ( _, { @@ -122,14 +136,16 @@ const SIMPLE_PRINTERS = { ) => { const root = type === "compilation.summary!"; const warningsMessage = - warningsCount > 0 + /** @type {number} */ (warningsCount) > 0 ? yellow( - `${warningsCount} ${plural(warningsCount, "warning", "warnings")}` + `${warningsCount} ${plural(/** @type {number} */ (warningsCount), "warning", "warnings")}` ) : ""; const errorsMessage = - errorsCount > 0 - ? red(`${errorsCount} ${plural(errorsCount, "error", "errors")}`) + /** @type {number} */ (errorsCount) > 0 + ? red( + `${errorsCount} ${plural(/** @type {number} */ (errorsCount), "error", "errors")}` + ) : ""; const timeMessage = root && time ? ` in ${formatTime(time)}` : ""; const hashMessage = hash ? ` (${hash})` : ""; @@ -255,11 +271,12 @@ const SIMPLE_PRINTERS = { "compilation.warningsInChildren!": (_, { yellow, compilation }) => { if ( !compilation.children && - compilation.warningsCount > 0 && + /** @type {number} */ (compilation.warningsCount) > 0 && compilation.warnings ) { const childWarnings = - compilation.warningsCount - compilation.warnings.length; + /** @type {number} */ (compilation.warningsCount) - + compilation.warnings.length; if (childWarnings > 0) { return yellow( `${childWarnings} ${plural( @@ -278,10 +295,12 @@ const SIMPLE_PRINTERS = { "compilation.errorsInChildren!": (_, { red, compilation }) => { if ( !compilation.children && - compilation.errorsCount > 0 && + /** @type {number} */ (compilation.errorsCount) > 0 && compilation.errors ) { - const childErrors = compilation.errorsCount - compilation.errors.length; + const childErrors = + /** @type {number} */ (compilation.errorsCount) - + compilation.errors.length; if (childErrors > 0) { return red( `${childErrors} ${plural( @@ -296,15 +315,16 @@ const SIMPLE_PRINTERS = { ); } } - }, + } +}; +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const ASSET_SIMPLE_PRINTERS = { "asset.type": type => type, "asset.name": (name, { formatFilename, asset: { isOverSizeLimit } }) => formatFilename(name, isOverSizeLimit), - "asset.size": ( - size, - { asset: { isOverSizeLimit }, yellow, green, formatSize } - ) => (isOverSizeLimit ? yellow(formatSize(size)) : formatSize(size)), + "asset.size": (size, { asset: { isOverSizeLimit }, yellow, formatSize }) => + isOverSizeLimit ? yellow(formatSize(size)) : formatSize(size), "asset.emitted": (emitted, { green, formatFlag }) => emitted ? green(formatFlag("emitted")) : undefined, "asset.comparedForEmit": (comparedForEmit, { yellow, formatFlag }) => @@ -353,8 +373,11 @@ const SIMPLE_PRINTERS = { assetChunk: (id, { formatChunkId }) => formatChunkId(id), assetChunkName: name => name, - assetChunkIdHint: name => name, + assetChunkIdHint: name => name +}; +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const MODULE_SIMPLE_PRINTERS = { "module.type": type => (type !== "module" ? type : undefined), "module.id": (id, { formatModuleId }) => isValidId(id) ? formatModuleId(id) : undefined, @@ -467,11 +490,17 @@ const SIMPLE_PRINTERS = { "modules" )}` : undefined, - "module.separator!": () => "\n", + "module.separator!": () => "\n" +}; +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const MODULE_ISSUER_PRINTERS = { "moduleIssuer.id": (id, { formatModuleId }) => formatModuleId(id), - "moduleIssuer.profile.total": (value, { formatTime }) => formatTime(value), + "moduleIssuer.profile.total": (value, { formatTime }) => formatTime(value) +}; +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const MODULE_REASON_PRINTERS = { "moduleReason.type": type => type, "moduleReason.userRequest": (userRequest, { cyan }) => cyan(getResourceName(userRequest)), @@ -493,8 +522,11 @@ const SIMPLE_PRINTERS = { "reason", "reasons" )}` - : undefined, + : undefined +}; +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const MODULE_PROFILE_PRINTERS = { "module.profile.total": (value, { formatTime }) => formatTime(value), "module.profile.resolving": (value, { formatTime }) => `resolving: ${formatTime(value)}`, @@ -509,8 +541,11 @@ const SIMPLE_PRINTERS = { "module.profile.additionalResolving": (value, { formatTime }) => value ? `additional resolving: ${formatTime(value)}` : undefined, "module.profile.additionalIntegration": (value, { formatTime }) => - value ? `additional integration: ${formatTime(value)}` : undefined, + value ? `additional integration: ${formatTime(value)}` : undefined +}; +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const CHUNK_GROUP_PRINTERS = { "chunkGroup.kind!": (_, { chunkGroupKind }) => chunkGroupKind, "chunkGroup.separator!": () => "\n", "chunkGroup.name": (name, { bold }) => bold(name), @@ -538,10 +573,11 @@ const SIMPLE_PRINTERS = { "chunkGroup.is!": () => "=", "chunkGroupAsset.name": (asset, { green }) => green(asset), "chunkGroupAsset.size": (size, { formatSize, chunkGroup }) => - chunkGroup.assets.length > 1 || + chunkGroup.assets && + (chunkGroup.assets.length > 1 || (chunkGroup.auxiliaryAssets && chunkGroup.auxiliaryAssets.length > 0) ? formatSize(size) - : undefined, + : undefined), "chunkGroup.children": (children, context, printer) => Array.isArray(children) ? undefined @@ -557,8 +593,11 @@ const SIMPLE_PRINTERS = { "chunkGroupChild.assets[]": (file, { formatFilename }) => formatFilename(file), "chunkGroupChild.chunks[]": (id, { formatChunkId }) => formatChunkId(id), - "chunkGroupChild.name": name => (name ? `(name: ${name})` : undefined), + "chunkGroupChild.name": name => (name ? `(name: ${name})` : undefined) +}; +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const CHUNK_PRINTERS = { "chunk.id": (id, { formatChunkId }) => formatChunkId(id), "chunk.files[]": (file, { formatFilename }) => formatFilename(file), "chunk.names[]": name => name, @@ -608,8 +647,11 @@ const SIMPLE_PRINTERS = { "chunkOrigin.moduleId": (moduleId, { formatModuleId }) => isValidId(moduleId) ? formatModuleId(moduleId) : undefined, "chunkOrigin.moduleName": (moduleName, { bold }) => bold(moduleName), - "chunkOrigin.loc": loc => loc, + "chunkOrigin.loc": loc => loc +}; +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const ERROR_PRINTERS = { "error.compilerPath": (compilerPath, { bold }) => compilerPath ? bold(`(${compilerPath})`) : undefined, "error.chunkId": (chunkId, { formatChunkId }) => @@ -631,8 +673,11 @@ const SIMPLE_PRINTERS = { filteredDetails ? `+ ${filteredDetails} hidden lines` : undefined, "error.stack": stack => stack, "error.moduleTrace": moduleTrace => undefined, - "error.separator!": () => "\n", + "error.separator!": () => "\n" +}; +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const LOG_ENTRY_PRINTERS = { "loggingEntry(error).loggingEntry.message": (message, { red }) => mapLines(message, x => ` ${red(x)}`), "loggingEntry(warn).loggingEntry.message": (message, { yellow }) => @@ -662,20 +707,26 @@ const SIMPLE_PRINTERS = { "loggingEntry.trace[]": trace => trace ? mapLines(trace, x => `| ${x}`) : undefined, - "moduleTraceItem.originName": originName => originName, - loggingGroup: loggingGroup => loggingGroup.entries.length === 0 ? "" : undefined, "loggingGroup.debug": (flag, { red }) => (flag ? red("DEBUG") : undefined), "loggingGroup.name": (name, { bold }) => bold(`LOG from ${name}`), "loggingGroup.separator!": () => "\n", "loggingGroup.filteredEntries": filteredEntries => - filteredEntries > 0 ? `+ ${filteredEntries} hidden lines` : undefined, + filteredEntries > 0 ? `+ ${filteredEntries} hidden lines` : undefined +}; +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const MODULE_TRACE_ITEM_PRINTERS = { + "moduleTraceItem.originName": originName => originName +}; + +/** @type {Record & Required & WithRequired, printer: StatsPrinter) => string | undefined>} */ +const MODULE_TRACE_DEPENDENCY_PRINTERS = { "moduleTraceDependency.loc": loc => loc }; -/** @type {Record} */ +/** @type {Record} */ const ITEM_NAMES = { "compilation.assets[]": "asset", "compilation.modules[]": "module", @@ -904,19 +955,27 @@ const PREFERRED_ORDERS = { loggingEntry: ["message", "trace", "children"] }; +/** @typedef {(items: string[]) => string | undefined} SimpleItemsJoiner */ + +/** @type {SimpleItemsJoiner} */ const itemsJoinOneLine = items => items.filter(Boolean).join(" "); +/** @type {SimpleItemsJoiner} */ const itemsJoinOneLineBrackets = items => items.length > 0 ? `(${items.filter(Boolean).join(" ")})` : undefined; +/** @type {SimpleItemsJoiner} */ const itemsJoinMoreSpacing = items => items.filter(Boolean).join("\n\n"); +/** @type {SimpleItemsJoiner} */ const itemsJoinComma = items => items.filter(Boolean).join(", "); +/** @type {SimpleItemsJoiner} */ const itemsJoinCommaBrackets = items => items.length > 0 ? `(${items.filter(Boolean).join(", ")})` : undefined; +/** @type {function(string): SimpleItemsJoiner} */ const itemsJoinCommaBracketsWithName = name => items => items.length > 0 ? `(${name}: ${items.filter(Boolean).join(", ")})` : undefined; -/** @type {Record string>} */ +/** @type {Record} */ const SIMPLE_ITEMS_JOINER = { "chunk.parents": itemsJoinOneLine, "chunk.siblings": itemsJoinOneLine, @@ -948,18 +1007,27 @@ const SIMPLE_ITEMS_JOINER = { "compilation.errors": itemsJoinMoreSpacing, "compilation.warnings": itemsJoinMoreSpacing, "compilation.logging": itemsJoinMoreSpacing, - "compilation.children": items => indent(itemsJoinMoreSpacing(items), " "), + "compilation.children": items => + indent(/** @type {string} */ (itemsJoinMoreSpacing(items)), " "), "moduleTraceItem.dependencies": itemsJoinOneLine, "loggingEntry.children": items => indent(items.filter(Boolean).join("\n"), " ", false) }; +/** + * @param {Item[]} items items + * @returns {string} result + */ const joinOneLine = items => items .map(item => item.content) .filter(Boolean) .join(" "); +/** + * @param {Item[]} items items + * @returns {string} result + */ const joinInBrackets = items => { const res = []; let mode = 0; @@ -1002,6 +1070,12 @@ const joinInBrackets = items => { return res.join(""); }; +/** + * @param {string} str a string + * @param {string} prefix prefix + * @param {boolean=} noPrefixInFirstLine need prefix in the first line? + * @returns {string} result + */ const indent = (str, prefix, noPrefixInFirstLine) => { const rem = str.replace(/\n([^\n])/g, `\n${prefix}$1`); if (noPrefixInFirstLine) return rem; @@ -1009,6 +1083,11 @@ const indent = (str, prefix, noPrefixInFirstLine) => { return ind + rem; }; +/** + * @param {(false | Item)[]} items items + * @param {string} indenter indenter + * @returns {string} result + */ const joinExplicitNewLine = (items, indenter) => { let firstInLine = true; let first = true; @@ -1030,15 +1109,27 @@ const joinExplicitNewLine = (items, indenter) => { .trim(); }; +/** + * @param {boolean} error is an error + * @returns {SimpleElementJoiner} joiner + */ const joinError = error => + /** + * @param {Item[]} items items + * @param {Required} ctx context + * @returns {string} result + */ (items, { red, yellow }) => `${error ? red("ERROR") : yellow("WARNING")} in ${joinExplicitNewLine( items, "" )}`; -/** @type {Record string>} */ +/** @typedef {{ element: string, content: string }} Item */ +/** @typedef {(items: Item[], context: Required) => string} SimpleElementJoiner */ + +/** @type {Record} */ const SIMPLE_ELEMENT_JOINERS = { compilation: items => { const result = []; @@ -1197,6 +1288,9 @@ const SIMPLE_ELEMENT_JOINERS = { moduleTraceDependency: joinOneLine }; +/** @typedef {"bold" | "yellow" | "red" | "green" | "cyan" | "magenta"} ColorNames */ + +/** @type {Record} */ const AVAILABLE_COLORS = { bold: "\u001B[1m", yellow: "\u001B[1m\u001B[33m", @@ -1206,6 +1300,7 @@ const AVAILABLE_COLORS = { magenta: "\u001B[1m\u001B[35m" }; +/** @type {Record & StatsPrinterContext, ...any): string>} */ const AVAILABLE_FORMATS = { formatChunkId: (id, { yellow }, direction) => { switch (direction) { @@ -1282,21 +1377,36 @@ const AVAILABLE_FORMATS = { } ]; for (const { regExp, format } of highlights) { - message = message.replace(regExp, (match, content) => - match.replace(content, format(content)) + message = message.replace( + regExp, + /** + * @param {string} match match + * @param {string} content content + * @returns {string} result + */ + (match, content) => match.replace(content, format(content)) ); } return message; } }; +/** @typedef {function(string): string} ResultModifierFn */ +/** @type {Record} */ const RESULT_MODIFIER = { "module.modules": result => indent(result, "| ") }; +/** + * @param {string[]} array array + * @param {string[]} preferredOrder preferred order + * @returns {string[]} result + */ const createOrder = (array, preferredOrder) => { const originalArray = array.slice(); + /** @type {Set} */ const set = new Set(array); + /** @type {Set} */ const usedSet = new Set(); array.length = 0; for (const element of preferredOrder) { @@ -1323,24 +1433,30 @@ class DefaultStatsPrinterPlugin { compiler.hooks.compilation.tap("DefaultStatsPrinterPlugin", compilation => { compilation.hooks.statsPrinter.tap( "DefaultStatsPrinterPlugin", - (stats, options, context) => { + (stats, options) => { // Put colors into context stats.hooks.print .for("compilation") .tap("DefaultStatsPrinterPlugin", (compilation, context) => { for (const color of Object.keys(AVAILABLE_COLORS)) { + const name = /** @type {ColorNames} */ (color); + /** @type {string | undefined} */ let start; if (options.colors) { if ( typeof options.colors === "object" && - typeof options.colors[color] === "string" + typeof options.colors[name] === "string" ) { - start = options.colors[color]; + start = options.colors[name]; } else { - start = AVAILABLE_COLORS[color]; + start = AVAILABLE_COLORS[name]; } } if (start) { + /** + * @param {string} str string + * @returns {string} string with color + */ context[color] = str => `${start}${ typeof str === "string" @@ -1351,21 +1467,184 @@ class DefaultStatsPrinterPlugin { : str }\u001B[39m\u001B[22m`; } else { + /** + * @param {string} str string + * @returns {string} str string + */ context[color] = str => str; } } for (const format of Object.keys(AVAILABLE_FORMATS)) { - context[format] = (content, ...args) => - AVAILABLE_FORMATS[format](content, context, ...args); + context[format] = + /** + * @param {string | number} content content + * @param {...TODO} args args + * @returns {string} result + */ + (content, ...args) => + AVAILABLE_FORMATS[format]( + content, + /** @type {Required & StatsPrinterContext} */ + (context), + ...args + ); } context.timeReference = compilation.time; }); - for (const key of Object.keys(SIMPLE_PRINTERS)) { + for (const key of Object.keys(COMPILATION_SIMPLE_PRINTERS)) { stats.hooks.print .for(key) .tap("DefaultStatsPrinterPlugin", (obj, ctx) => - SIMPLE_PRINTERS[key](obj, ctx, stats) + COMPILATION_SIMPLE_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(ASSET_SIMPLE_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + ASSET_SIMPLE_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(MODULE_SIMPLE_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + MODULE_SIMPLE_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(MODULE_ISSUER_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + MODULE_ISSUER_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(MODULE_REASON_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + MODULE_REASON_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(MODULE_PROFILE_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + MODULE_PROFILE_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(CHUNK_GROUP_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + CHUNK_GROUP_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(CHUNK_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + CHUNK_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(ERROR_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + ERROR_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(LOG_ENTRY_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + LOG_ENTRY_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(MODULE_TRACE_DEPENDENCY_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + MODULE_TRACE_DEPENDENCY_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) + ); + } + + for (const key of Object.keys(MODULE_TRACE_ITEM_PRINTERS)) { + stats.hooks.print + .for(key) + .tap("DefaultStatsPrinterPlugin", (obj, ctx) => + MODULE_TRACE_ITEM_PRINTERS[key]( + obj, + /** @type {Required & Required & WithRequired} */ + (ctx), + stats + ) ); } @@ -1399,7 +1678,7 @@ class DefaultStatsPrinterPlugin { const joiner = SIMPLE_ELEMENT_JOINERS[key]; stats.hooks.printElements .for(key) - .tap("DefaultStatsPrinterPlugin", joiner); + .tap("DefaultStatsPrinterPlugin", /** @type {TODO} */ (joiner)); } for (const key of Object.keys(RESULT_MODIFIER)) { diff --git a/lib/stats/StatsFactory.js b/lib/stats/StatsFactory.js index 9d7d9676e..18f21fb9d 100644 --- a/lib/stats/StatsFactory.js +++ b/lib/stats/StatsFactory.js @@ -11,83 +11,108 @@ const smartGrouping = require("../util/smartGrouping"); /** @typedef {import("../Chunk")} Chunk */ /** @typedef {import("../Compilation")} Compilation */ +/** @typedef {import("../Compilation").NormalizedStatsOptions} NormalizedStatsOptions */ /** @typedef {import("../Module")} Module */ /** @typedef {import("../WebpackError")} WebpackError */ +/** @typedef {import("../util/comparators").Comparator} Comparator */ /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ - /** @typedef {import("../util/smartGrouping").GroupConfig} GroupConfig */ /** * @typedef {object} KnownStatsFactoryContext * @property {string} type - * @property {function(string): string=} makePathsRelative - * @property {Compilation=} compilation - * @property {Set=} rootModules - * @property {Map=} compilationFileToChunks - * @property {Map=} compilationAuxiliaryFileToChunks - * @property {RuntimeSpec=} runtime - * @property {function(Compilation): WebpackError[]=} cachedGetErrors - * @property {function(Compilation): WebpackError[]=} cachedGetWarnings + * @property {function(string): string} makePathsRelative + * @property {Compilation} compilation + * @property {Set} rootModules + * @property {Map} compilationFileToChunks + * @property {Map} compilationAuxiliaryFileToChunks + * @property {RuntimeSpec} runtime + * @property {function(Compilation): WebpackError[]} cachedGetErrors + * @property {function(Compilation): WebpackError[]} cachedGetWarnings */ -/** @typedef {KnownStatsFactoryContext & Record} StatsFactoryContext */ +/** @typedef {Record & KnownStatsFactoryContext} StatsFactoryContext */ + +/** @typedef {any} CreatedObject */ +/** @typedef {any} FactoryData */ +/** @typedef {any} FactoryDataItem */ +/** @typedef {any} Result */ +/** @typedef {Record} ObjectForExtract */ + +/** + * @typedef {object} StatsFactoryHooks + * @property {HookMap>} extract + * @property {HookMap>} filter + * @property {HookMap>} sort + * @property {HookMap>} filterSorted + * @property {HookMap>} groupResults + * @property {HookMap>} sortResults + * @property {HookMap>} filterResults + * @property {HookMap>} merge + * @property {HookMap>} result + * @property {HookMap>} getItemName + * @property {HookMap>} getItemFactory + */ + +/** + * @template T + * @typedef {Map} Caches + */ class StatsFactory { constructor() { + /** @type {StatsFactoryHooks} */ this.hooks = Object.freeze({ - /** @type {HookMap>} */ extract: new HookMap( () => new SyncBailHook(["object", "data", "context"]) ), - /** @type {HookMap>} */ filter: new HookMap( () => new SyncBailHook(["item", "context", "index", "unfilteredIndex"]) ), - /** @type {HookMap>} */ sort: new HookMap(() => new SyncBailHook(["comparators", "context"])), - /** @type {HookMap>} */ filterSorted: new HookMap( () => new SyncBailHook(["item", "context", "index", "unfilteredIndex"]) ), - /** @type {HookMap>} */ groupResults: new HookMap( () => new SyncBailHook(["groupConfigs", "context"]) ), - /** @type {HookMap>} */ sortResults: new HookMap( () => new SyncBailHook(["comparators", "context"]) ), - /** @type {HookMap>} */ filterResults: new HookMap( () => new SyncBailHook(["item", "context", "index", "unfilteredIndex"]) ), - /** @type {HookMap>} */ merge: new HookMap(() => new SyncBailHook(["items", "context"])), - /** @type {HookMap>} */ result: new HookMap(() => new SyncWaterfallHook(["result", "context"])), - /** @type {HookMap>} */ getItemName: new HookMap(() => new SyncBailHook(["item", "context"])), - /** @type {HookMap>} */ getItemFactory: new HookMap(() => new SyncBailHook(["item", "context"])) }); const hooks = this.hooks; - this._caches = - /** @type {Record[]>>} */ ({}); + this._caches = /** @type {TODO} */ ({}); for (const key of Object.keys(hooks)) { - this._caches[key] = new Map(); + this._caches[/** @type {keyof StatsFactoryHooks} */ (key)] = new Map(); } this._inCreate = false; } + /** + * @template {StatsFactoryHooks[keyof StatsFactoryHooks]} HM + * @template {HM extends HookMap ? H : never} H + * @param {HM} hookMap hook map + * @param {Caches} cache cache + * @param {string} type type + * @returns {H[]} hooks + * @private + */ _getAllLevelHooks(hookMap, cache, type) { const cacheEntry = cache.get(type); if (cacheEntry !== undefined) { return cacheEntry; } - const hooks = []; + const hooks = /** @type {H[]} */ ([]); const typeParts = type.split("."); for (let i = 0; i < typeParts.length; i++) { - const hook = hookMap.get(typeParts.slice(i).join(".")); + const hook = /** @type {H} */ (hookMap.get(typeParts.slice(i).join("."))); if (hook) { hooks.push(hook); } @@ -96,27 +121,62 @@ class StatsFactory { return hooks; } + /** + * @template {StatsFactoryHooks[keyof StatsFactoryHooks]} HM + * @template {HM extends HookMap ? H : never} H + * @template {H extends import("tapable").Hook ? R : never} R + * @param {HM} hookMap hook map + * @param {Caches} cache cache + * @param {string} type type + * @param {function(H): R | undefined} fn fn + * @returns {R | undefined} hook + * @private + */ _forEachLevel(hookMap, cache, type, fn) { for (const hook of this._getAllLevelHooks(hookMap, cache, type)) { - const result = fn(hook); + const result = fn(/** @type {H} */ (hook)); if (result !== undefined) return result; } } + /** + * @template {StatsFactoryHooks[keyof StatsFactoryHooks]} HM + * @template {HM extends HookMap ? H : never} H + * @param {HM} hookMap hook map + * @param {Caches} cache cache + * @param {string} type type + * @param {FactoryData} data data + * @param {function(H, FactoryData): FactoryData} fn fn + * @returns {FactoryData} data + * @private + */ _forEachLevelWaterfall(hookMap, cache, type, data, fn) { for (const hook of this._getAllLevelHooks(hookMap, cache, type)) { - data = fn(hook, data); + data = fn(/** @type {H} */ (hook), data); } return data; } + /** + * @template {StatsFactoryHooks[keyof StatsFactoryHooks]} T + * @template {T extends HookMap ? H : never} H + * @template {H extends import("tapable").Hook ? R : never} R + * @param {T} hookMap hook map + * @param {Caches} cache cache + * @param {string} type type + * @param {Array} items items + * @param {function(H, R, number, number): R | undefined} fn fn + * @param {boolean} forceClone force clone + * @returns {R[]} result for each level + * @private + */ _forEachLevelFilter(hookMap, cache, type, items, fn, forceClone) { const hooks = this._getAllLevelHooks(hookMap, cache, type); if (hooks.length === 0) return forceClone ? items.slice() : items; let i = 0; return items.filter((item, idx) => { for (const hook of hooks) { - const r = fn(hook, item, idx, i); + const r = fn(/** @type {H} */ (hook), item, idx, i); if (r !== undefined) { if (r) i++; return r; @@ -129,9 +189,9 @@ class StatsFactory { /** * @param {string} type type - * @param {any} data factory data + * @param {FactoryData} data factory data * @param {Omit} baseContext context used as base - * @returns {any} created object + * @returns {CreatedObject} created object */ create(type, data, baseContext) { if (this._inCreate) { @@ -141,17 +201,25 @@ class StatsFactory { this._inCreate = true; return this._create(type, data, baseContext); } finally { - for (const key of Object.keys(this._caches)) this._caches[key].clear(); + for (const key of Object.keys(this._caches)) + this._caches[/** @type {keyof StatsFactoryHooks} */ (key)].clear(); this._inCreate = false; } } + /** + * @param {string} type type + * @param {FactoryData} data factory data + * @param {Omit} baseContext context used as base + * @returns {CreatedObject} created object + * @private + */ _create(type, data, baseContext) { - const context = { + const context = /** @type {StatsFactoryContext} */ ({ ...baseContext, type, [type]: data - }; + }); if (Array.isArray(data)) { // run filter on unsorted items const items = this._forEachLevelFilter( @@ -164,6 +232,7 @@ class StatsFactory { ); // sort items + /** @type {Comparator[]} */ const comparators = []; this._forEachLevel(this.hooks.sort, this._caches.sort, type, h => h.call(comparators, context) @@ -187,6 +256,7 @@ class StatsFactory { // for each item let resultItems = items2.map((item, i) => { + /** @type {StatsFactoryContext} */ const itemContext = { ...context, _index: i @@ -216,6 +286,7 @@ class StatsFactory { }); // sort result items + /** @type {Comparator[]} */ const comparators2 = []; this._forEachLevel( this.hooks.sortResults, @@ -231,6 +302,7 @@ class StatsFactory { } // group result items + /** @type {GroupConfig[]} */ const groupConfigs = []; this._forEachLevel( this.hooks.groupResults, @@ -270,6 +342,7 @@ class StatsFactory { (h, r) => h.call(r, context) ); } + /** @type {ObjectForExtract} */ const object = {}; // run extract on value diff --git a/lib/stats/StatsPrinter.js b/lib/stats/StatsPrinter.js index dfd0aa9e9..f1e736de1 100644 --- a/lib/stats/StatsPrinter.js +++ b/lib/stats/StatsPrinter.js @@ -7,14 +7,18 @@ const { HookMap, SyncWaterfallHook, SyncBailHook } = require("tapable"); -/** @template T @typedef {import("tapable").AsArray} AsArray */ -/** @typedef {import("tapable").Hook} Hook */ /** @typedef {import("./DefaultStatsFactoryPlugin").StatsAsset} StatsAsset */ /** @typedef {import("./DefaultStatsFactoryPlugin").StatsChunk} StatsChunk */ /** @typedef {import("./DefaultStatsFactoryPlugin").StatsChunkGroup} StatsChunkGroup */ /** @typedef {import("./DefaultStatsFactoryPlugin").StatsCompilation} StatsCompilation */ +/** @typedef {import("./DefaultStatsFactoryPlugin").StatsError} StatsError */ +/** @typedef {import("./DefaultStatsFactoryPlugin").StatsLogging} StatsLogging */ /** @typedef {import("./DefaultStatsFactoryPlugin").StatsModule} StatsModule */ +/** @typedef {import("./DefaultStatsFactoryPlugin").StatsModuleIssuer} StatsModuleIssuer */ /** @typedef {import("./DefaultStatsFactoryPlugin").StatsModuleReason} StatsModuleReason */ +/** @typedef {import("./DefaultStatsFactoryPlugin").StatsModuleTraceDependency} StatsModuleTraceDependency */ +/** @typedef {import("./DefaultStatsFactoryPlugin").StatsModuleTraceItem} StatsModuleTraceItem */ +/** @typedef {import("./DefaultStatsFactoryPlugin").StatsProfile} StatsProfile */ /** * @typedef {object} PrintedElement @@ -27,53 +31,78 @@ const { HookMap, SyncWaterfallHook, SyncBailHook } = require("tapable"); * @property {string=} type * @property {StatsCompilation=} compilation * @property {StatsChunkGroup=} chunkGroup + * @property {string=} chunkGroupKind * @property {StatsAsset=} asset * @property {StatsModule=} module * @property {StatsChunk=} chunk * @property {StatsModuleReason=} moduleReason + * @property {StatsModuleIssuer=} moduleIssuer + * @property {StatsError=} error + * @property {StatsProfile=} profile + * @property {StatsLogging=} logging + * @property {StatsModuleTraceItem=} moduleTraceItem + * @property {StatsModuleTraceDependency=} moduleTraceDependency + */ + +/** + * @typedef {object} KnownStatsPrinterColorFn * @property {(str: string) => string=} bold * @property {(str: string) => string=} yellow * @property {(str: string) => string=} red * @property {(str: string) => string=} green * @property {(str: string) => string=} magenta * @property {(str: string) => string=} cyan + */ + +/** + * @typedef {object} KnownStatsPrinterFormaters * @property {(file: string, oversize?: boolean) => string=} formatFilename * @property {(id: string) => string=} formatModuleId * @property {(id: string, direction?: "parent"|"child"|"sibling") => string=} formatChunkId * @property {(size: number) => string=} formatSize + * @property {(size: string) => string=} formatLayer * @property {(dateTime: number) => string=} formatDateTime * @property {(flag: string) => string=} formatFlag * @property {(time: number, boldQuantity?: boolean) => string=} formatTime - * @property {string=} chunkGroupKind + * @property {(message: string) => string=} formatError */ -/** @typedef {KnownStatsPrinterContext & Record} StatsPrinterContext */ +/** @typedef {Record & KnownStatsPrinterColorFn & KnownStatsPrinterFormaters & KnownStatsPrinterContext} StatsPrinterContext */ +/** @typedef {any} PrintObject */ + +/** + * @typedef {object} StatsPrintHooks + * @property {HookMap>} sortElements + * @property {HookMap>} printElements + * @property {HookMap>} sortItems + * @property {HookMap>} getItemName + * @property {HookMap>} printItems + * @property {HookMap>} print + * @property {HookMap>} result + */ class StatsPrinter { constructor() { + /** @type {StatsPrintHooks} */ this.hooks = Object.freeze({ - /** @type {HookMap>} */ sortElements: new HookMap( () => new SyncBailHook(["elements", "context"]) ), - /** @type {HookMap>} */ printElements: new HookMap( () => new SyncBailHook(["printedElements", "context"]) ), - /** @type {HookMap>} */ sortItems: new HookMap(() => new SyncBailHook(["items", "context"])), - /** @type {HookMap>} */ getItemName: new HookMap(() => new SyncBailHook(["item", "context"])), - /** @type {HookMap>} */ printItems: new HookMap( () => new SyncBailHook(["printedItems", "context"]) ), - /** @type {HookMap>} */ print: new HookMap(() => new SyncBailHook(["object", "context"])), /** @type {HookMap>} */ result: new HookMap(() => new SyncWaterfallHook(["result", "context"])) }); - /** @type {Map, Map>} */ + /** + * @type {TODO} + */ this._levelHookCache = new Map(); this._inPrint = false; } @@ -81,15 +110,14 @@ class StatsPrinter { /** * get all level hooks * @private - * @template {Hook} T - * @param {HookMap} hookMap HookMap + * @template {StatsPrintHooks[keyof StatsPrintHooks]} HM + * @template {HM extends HookMap ? H : never} H + * @param {HM} hookMap hook map * @param {string} type type - * @returns {T[]} hooks + * @returns {H[]} hooks */ _getAllLevelHooks(hookMap, type) { - let cache = /** @type {Map} */ ( - this._levelHookCache.get(hookMap) - ); + let cache = this._levelHookCache.get(hookMap); if (cache === undefined) { cache = new Map(); this._levelHookCache.set(hookMap, cache); @@ -98,11 +126,11 @@ class StatsPrinter { if (cacheEntry !== undefined) { return cacheEntry; } - /** @type {T[]} */ + /** @type {H[]} */ const hooks = []; const typeParts = type.split("."); for (let i = 0; i < typeParts.length; i++) { - const hook = hookMap.get(typeParts.slice(i).join(".")); + const hook = /** @type {H} */ (hookMap.get(typeParts.slice(i).join("."))); if (hook) { hooks.push(hook); } @@ -114,16 +142,17 @@ class StatsPrinter { /** * Run `fn` for each level * @private - * @template T - * @template R - * @param {HookMap>} hookMap HookMap + * @template {StatsPrintHooks[keyof StatsPrintHooks]} HM + * @template {HM extends HookMap ? H : never} H + * @template {H extends import("tapable").Hook ? R : never} R + * @param {HM} hookMap hook map * @param {string} type type - * @param {(hook: SyncBailHook) => R} fn function - * @returns {R} result of `fn` + * @param {function(H): R | undefined} fn fn + * @returns {R | undefined} hook */ _forEachLevel(hookMap, type, fn) { for (const hook of this._getAllLevelHooks(hookMap, type)) { - const result = fn(hook); + const result = fn(/** @type {H} */ (hook)); if (result !== undefined) return result; } } @@ -131,24 +160,25 @@ class StatsPrinter { /** * Run `fn` for each level * @private - * @template T - * @param {HookMap>} hookMap HookMap + * @template {StatsPrintHooks[keyof StatsPrintHooks]} HM + * @template {HM extends HookMap ? H : never} H + * @param {HM} hookMap hook map * @param {string} type type - * @param {AsArray[0]} data data - * @param {(hook: SyncWaterfallHook, data: AsArray[0]) => AsArray[0]} fn function - * @returns {AsArray[0]} result of `fn` + * @param {string} data data + * @param {function(H, string): string} fn fn + * @returns {string} result of `fn` */ _forEachLevelWaterfall(hookMap, type, data, fn) { for (const hook of this._getAllLevelHooks(hookMap, type)) { - data = fn(hook, data); + data = fn(/** @type {H} */ (hook), data); } return data; } /** * @param {string} type The type - * @param {object} object Object to print - * @param {object=} baseContext The base context + * @param {PrintObject} object Object to print + * @param {StatsPrinterContext=} baseContext The base context * @returns {string} printed result */ print(type, object, baseContext) { @@ -167,11 +197,12 @@ class StatsPrinter { /** * @private * @param {string} type type - * @param {object} object object - * @param {object=} baseContext context + * @param {PrintObject} object object + * @param {StatsPrinterContext=} baseContext context * @returns {string} printed result */ _print(type, object, baseContext) { + /** @type {StatsPrinterContext} */ const context = { ...baseContext, type, @@ -188,6 +219,7 @@ class StatsPrinter { h.call(sortedItems, context) ); const printedItems = sortedItems.map((item, i) => { + /** @type {StatsPrinterContext} */ const itemContext = { ...context, _index: i @@ -240,7 +272,7 @@ class StatsPrinter { return this._forEachLevelWaterfall( this.hooks.result, type, - printResult, + /** @type {string} */ (printResult), (h, r) => h.call(r, context) ); } diff --git a/lib/util/AsyncQueue.js b/lib/util/AsyncQueue.js index f7834d0a9..9a5a260c2 100644 --- a/lib/util/AsyncQueue.js +++ b/lib/util/AsyncQueue.js @@ -20,7 +20,7 @@ let inHandleResult = 0; * @template T * @callback Callback * @param {(WebpackError | null)=} err - * @param {T=} result + * @param {(T | null)=} result */ /** @@ -37,15 +37,27 @@ class AsyncQueueEntry { this.item = item; /** @type {typeof QUEUED_STATE | typeof PROCESSING_STATE | typeof DONE_STATE} */ this.state = QUEUED_STATE; + /** @type {Callback | undefined} */ this.callback = callback; /** @type {Callback[] | undefined} */ this.callbacks = undefined; + /** @type {R | null | undefined} */ this.result = undefined; - /** @type {WebpackError | undefined} */ + /** @type {WebpackError | null | undefined} */ this.error = undefined; } } +/** + * @template T, K + * @typedef {function(T): K} getKey + */ + +/** + * @template T, R + * @typedef {function(T, Callback): void} Processor + */ + /** * @template T * @template K @@ -57,15 +69,16 @@ class AsyncQueue { * @param {string=} options.name name of the queue * @param {number=} options.parallelism how many items should be processed at once * @param {AsyncQueue=} options.parent parent queue, which will have priority over this queue and with shared parallelism - * @param {function(T): K=} options.getKey extract key from item - * @param {function(T, Callback): void} options.processor async function to process items + * @param {getKey=} options.getKey extract key from item + * @param {Processor} options.processor async function to process items */ constructor({ name, parallelism, parent, processor, getKey }) { this._name = name; this._parallelism = parallelism || 1; this._processor = processor; this._getKey = - getKey || /** @type {(T) => K} */ (item => /** @type {any} */ (item)); + getKey || + /** @type {getKey} */ (item => /** @type {T & K} */ (item)); /** @type {Map>} */ this._entries = new Map(); /** @type {ArrayQueue>} */ @@ -76,6 +89,7 @@ class AsyncQueue { this._willEnsureProcessing = false; this._needProcessing = false; this._stopped = false; + /** @type {AsyncQueue} */ this._root = parent ? parent._root : this; if (parent) { if (this._root._children === undefined) { @@ -94,7 +108,7 @@ class AsyncQueue { beforeStart: new AsyncSeriesHook(["item"]), /** @type {SyncHook<[T]>} */ started: new SyncHook(["item"]), - /** @type {SyncHook<[T, Error, R]>} */ + /** @type {SyncHook<[T, WebpackError | null | undefined, R | null | undefined]>} */ result: new SyncHook(["item", "error", "result"]) }; @@ -202,9 +216,14 @@ class AsyncQueue { this._queued = new ArrayQueue(); const root = this._root; for (const entry of queue) { - this._entries.delete(this._getKey(entry.item)); + this._entries.delete( + this._getKey(/** @type {AsyncQueueEntry} */ (entry).item) + ); root._activeTasks++; - this._handleResult(entry, new WebpackError("Queue was stopped")); + this._handleResult( + /** @type {AsyncQueueEntry} */ (entry), + new WebpackError("Queue was stopped") + ); } } @@ -308,7 +327,7 @@ class AsyncQueue { }); } catch (err) { if (inCallback) throw err; - this._handleResult(entry, err, null); + this._handleResult(entry, /** @type {WebpackError} */ (err), null); } this.hooks.started.call(entry.item); }); @@ -316,8 +335,8 @@ class AsyncQueue { /** * @param {AsyncQueueEntry} entry the entry - * @param {WebpackError=} err error, if any - * @param {R=} result result, if any + * @param {(WebpackError | null)=} err error, if any + * @param {(R | null)=} result result, if any * @returns {void} */ _handleResult(entry, err, result) { @@ -326,7 +345,7 @@ class AsyncQueue { ? makeWebpackError(hookError, `AsyncQueue(${this._name}).hooks.result`) : err; - const callback = entry.callback; + const callback = /** @type {Callback} */ (entry.callback); const callbacks = entry.callbacks; entry.state = DONE_STATE; entry.callback = undefined; diff --git a/lib/util/SortableSet.js b/lib/util/SortableSet.js index 3a9fb7a2e..9260c163a 100644 --- a/lib/util/SortableSet.js +++ b/lib/util/SortableSet.js @@ -24,7 +24,7 @@ class SortableSet extends Set { super(initialIterable); /** * @private - * @type {undefined | function(T, T): number}} + * @type {undefined | SortFunction} */ this._sortFn = defaultSort; /** @@ -77,7 +77,7 @@ class SortableSet extends Set { /** * Sort with a comparer function - * @param {SortFunction} sortFn Sorting comparer function + * @param {SortFunction | undefined} sortFn Sorting comparer function * @returns {void} */ sortWith(sortFn) { diff --git a/lib/util/StackedCacheMap.js b/lib/util/StackedCacheMap.js index c8283c7bd..820f0d1b3 100644 --- a/lib/util/StackedCacheMap.js +++ b/lib/util/StackedCacheMap.js @@ -92,7 +92,7 @@ class StackedCacheMap { /** * @param {K} item the key of the element to return - * @returns {V} the value of the element + * @returns {V | undefined} the value of the element */ get(item) { for (const map of this.stack) { @@ -128,7 +128,7 @@ class StackedCacheMap { next() { let result = current.next(); while (result.done && iterators.length > 0) { - current = iterators.pop(); + current = /** @type {IterableIterator<[K, V]>} */ (iterators.pop()); result = current.next(); } return result; diff --git a/lib/util/comparators.js b/lib/util/comparators.js index adb2f7f22..8d228026e 100644 --- a/lib/util/comparators.js +++ b/lib/util/comparators.js @@ -10,14 +10,26 @@ const { compareRuntime } = require("./runtime"); /** @typedef {import("../Chunk")} Chunk */ /** @typedef {import("../Chunk").ChunkId} ChunkId */ /** @typedef {import("../ChunkGraph")} ChunkGraph */ +/** @typedef {import("../ChunkGraph").ModuleId} ModuleId */ /** @typedef {import("../ChunkGroup")} ChunkGroup */ /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */ /** @typedef {import("../Module")} Module */ /** @typedef {import("../ModuleGraph")} ModuleGraph */ -/** @template T @typedef {function(T, T): -1|0|1} Comparator */ -/** @template TArg @template T @typedef {function(TArg, T, T): -1|0|1} RawParameterizedComparator */ -/** @template TArg @template T @typedef {function(TArg): Comparator} ParameterizedComparator */ +/** + * @template T + * @typedef {function(T, T): -1|0|1} Comparator + */ +/** + * @template TArg + * @template T + * @typedef {function(TArg, T, T): -1|0|1} RawParameterizedComparator + */ +/** + * @template TArg + * @template T + * @typedef {function(TArg): Comparator} ParameterizedComparator + */ /** * @template T @@ -64,7 +76,10 @@ module.exports.compareModulesByIdentifier = (a, b) => * @returns {-1|0|1} compare result */ const compareModulesById = (chunkGraph, a, b) => - compareIds(chunkGraph.getModuleId(a), chunkGraph.getModuleId(b)); + compareIds( + /** @type {ModuleId} */ (chunkGraph.getModuleId(a)), + /** @type {ModuleId} */ (chunkGraph.getModuleId(b)) + ); /** @type {ParameterizedComparator} */ module.exports.compareModulesById = createCachedParameterizedComparator(compareModulesById); @@ -203,7 +218,10 @@ module.exports.compareModulesByPreOrderIndexOrIdentifier = * @returns {-1|0|1} compare result */ const compareModulesByIdOrIdentifier = (chunkGraph, a, b) => { - const cmp = compareIds(chunkGraph.getModuleId(a), chunkGraph.getModuleId(b)); + const cmp = compareIds( + /** @type {ModuleId} */ (chunkGraph.getModuleId(a)), + /** @type {ModuleId} */ (chunkGraph.getModuleId(b)) + ); if (cmp !== 0) return cmp; return compareIds(a.identifier(), b.identifier()); }; diff --git a/lib/util/deterministicGrouping.js b/lib/util/deterministicGrouping.js index 0bef8567b..b69be0288 100644 --- a/lib/util/deterministicGrouping.js +++ b/lib/util/deterministicGrouping.js @@ -229,7 +229,7 @@ class Group { newSimilarities.push( lastNode === this.nodes[i - 1] ? /** @type {number[]} */ (this.similarities)[i - 1] - : similarity(lastNode.key, node.key) + : similarity(/** @type {Node} */ (lastNode).key, node.key) ); } newNodes.push(node); diff --git a/lib/util/identifier.js b/lib/util/identifier.js index f10019e3d..e94a63b50 100644 --- a/lib/util/identifier.js +++ b/lib/util/identifier.js @@ -85,24 +85,48 @@ const requestToAbsolute = (context, relativePath) => { return relativePath; }; +/** + * @template T + * @typedef {function(string, object=): T} MakeCacheableResult + */ + +/** + * @template T + * @typedef {function(string): T} BindCacheResultFn + */ + +/** + * @template T + * @typedef {function(object): BindCacheResultFn} BindCache + */ + +/** + * @template T + * @param {(function(string): T)} realFn real function + * @returns {MakeCacheableResult & { bindCache: BindCache }} cacheable function + */ const makeCacheable = realFn => { - /** @type {WeakMap>} */ + /** + * @template T + * @typedef {Map} CacheItem + */ + /** @type {WeakMap>} */ const cache = new WeakMap(); + /** + * @param {object} associatedObjectForCache an object to which the cache will be attached + * @returns {CacheItem} cache item + */ const getCache = associatedObjectForCache => { const entry = cache.get(associatedObjectForCache); if (entry !== undefined) return entry; - /** @type {Map} */ + /** @type {Map} */ const map = new Map(); cache.set(associatedObjectForCache, map); return map; }; - /** - * @param {string} str the path with query and fragment - * @param {object=} associatedObjectForCache an object to which the cache will be attached - * @returns {ParsedResource} parsed parts - */ + /** @type {MakeCacheableResult & { bindCache: BindCache }} */ const fn = (str, associatedObjectForCache) => { if (!associatedObjectForCache) return realFn(str); const cache = getCache(associatedObjectForCache); @@ -113,8 +137,13 @@ const makeCacheable = realFn => { return result; }; + /** @type {BindCache} */ fn.bindCache = associatedObjectForCache => { const cache = getCache(associatedObjectForCache); + /** + * @param {string} str string + * @returns {T} value + */ return str => { const entry = cache.get(str); if (entry !== undefined) return entry; @@ -127,16 +156,21 @@ const makeCacheable = realFn => { return fn; }; +/** @typedef {function(string, string, object=): string} MakeCacheableWithContextResult */ +/** @typedef {function(string, string): string} BindCacheForContextResultFn */ +/** @typedef {function(string): string} BindContextCacheForContextResultFn */ +/** @typedef {function(object=): BindCacheForContextResultFn} BindCacheForContext */ +/** @typedef {function(string, object=): BindContextCacheForContextResultFn} BindContextCacheForContext */ + +/** + * @param {function(string, string): string} fn function + * @returns {MakeCacheableWithContextResult & { bindCache: BindCacheForContext, bindContextCache: BindContextCacheForContext }} cacheable function with context + */ const makeCacheableWithContext = fn => { /** @type {WeakMap>>} */ const cache = new WeakMap(); - /** - * @param {string} context context used to create relative path - * @param {string} identifier identifier used to create relative path - * @param {object=} associatedObjectForCache an object to which the cache will be attached - * @returns {string} the returned relative path - */ + /** @type {MakeCacheableWithContextResult & { bindCache: BindCacheForContext, bindContextCache: BindContextCacheForContext }} */ const cachedFn = (context, identifier, associatedObjectForCache) => { if (!associatedObjectForCache) return fn(context, identifier); @@ -162,10 +196,7 @@ const makeCacheableWithContext = fn => { return result; }; - /** - * @param {object=} associatedObjectForCache an object to which the cache will be attached - * @returns {function(string, string): string} cached function - */ + /** @type {BindCacheForContext} */ cachedFn.bindCache = associatedObjectForCache => { let innerCache; if (associatedObjectForCache) { @@ -203,11 +234,7 @@ const makeCacheableWithContext = fn => { return boundFn; }; - /** - * @param {string} context context used to create relative path - * @param {object=} associatedObjectForCache an object to which the cache will be attached - * @returns {function(string): string} cached function - */ + /** @type {BindContextCacheForContext} */ cachedFn.bindContextCache = (context, associatedObjectForCache) => { let innerSubCache; if (associatedObjectForCache) { @@ -311,7 +338,9 @@ const PATH_QUERY_REGEXP = /^((?:\0.|[^?\0])*)(\?.*)?$/; * @returns {ParsedResource} parsed parts */ const _parseResource = str => { - const match = PATH_QUERY_FRAGMENT_REGEXP.exec(str); + const match = + /** @type {[string, string, string | undefined, string | undefined]} */ + (/** @type {unknown} */ (PATH_QUERY_FRAGMENT_REGEXP.exec(str))); return { resource: str, path: match[1].replace(/\0(.)/g, "$1"), @@ -327,7 +356,9 @@ module.exports.parseResource = makeCacheable(_parseResource); * @returns {ParsedResourceWithoutFragment} parsed parts */ const _parseResourceWithoutFragment = str => { - const match = PATH_QUERY_REGEXP.exec(str); + const match = + /** @type {[string, string, string | undefined]} */ + (/** @type {unknown} */ (PATH_QUERY_REGEXP.exec(str))); return { resource: str, path: match[1].replace(/\0(.)/g, "$1"), diff --git a/lib/util/registerExternalSerializer.js b/lib/util/registerExternalSerializer.js index 21cf714a1..711bcfa21 100644 --- a/lib/util/registerExternalSerializer.js +++ b/lib/util/registerExternalSerializer.js @@ -9,7 +9,7 @@ const { register } = require("./serialization"); const Position = /** @type {TODO} */ (require("acorn")).Position; const SourceLocation = require("acorn").SourceLocation; -const ValidationError = require("schema-utils/dist/ValidationError").default; +const ValidationError = require("schema-utils").ValidationError; const { CachedSource, ConcatSource, diff --git a/lib/util/smartGrouping.js b/lib/util/smartGrouping.js index 15c3dad44..f75648c45 100644 --- a/lib/util/smartGrouping.js +++ b/lib/util/smartGrouping.js @@ -16,7 +16,7 @@ * @template T * @template R * @typedef {object} GroupConfig - * @property {function(T): string[]} getKeys + * @property {function(T): string[] | undefined} getKeys * @property {function(string, (R | T)[], T[]): R} createGroup * @property {function(string, T[]): GroupOptions=} getOptions */ diff --git a/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js b/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js index a354295a4..654a6204f 100644 --- a/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js +++ b/lib/wasm-sync/WasmChunkLoadingRuntimeModule.js @@ -13,6 +13,7 @@ const WebAssemblyUtils = require("./WebAssemblyUtils"); /** @typedef {import("@webassemblyjs/ast").Signature} Signature */ /** @typedef {import("../Chunk")} Chunk */ /** @typedef {import("../ChunkGraph")} ChunkGraph */ +/** @typedef {import("../ChunkGraph").ModuleId} ModuleId */ /** @typedef {import("../Compilation")} Compilation */ /** @typedef {import("../Module")} Module */ /** @typedef {import("../Module").ReadOnlyRuntimeRequirements} ReadOnlyRuntimeRequirements */ @@ -89,7 +90,8 @@ const generateImportObject = ( const instanceVar = `m${waitForInstances.size}`; waitForInstances.set( instanceVar, - chunkGraph.getModuleId(/** @type {Module} */ (importedModule)) + /** @type {ModuleId} */ + (chunkGraph.getModuleId(/** @type {Module} */ (importedModule))) ); properties.push({ module, diff --git a/types.d.ts b/types.d.ts index 7d3c3a36d..074e691fe 100644 --- a/types.d.ts +++ b/types.d.ts @@ -400,7 +400,9 @@ declare abstract class AsyncQueue { added: SyncHook<[T]>; beforeStart: AsyncSeriesHook<[T]>; started: SyncHook<[T]>; - result: SyncHook<[T, Error, R]>; + result: SyncHook< + [T, undefined | null | WebpackError, undefined | null | R] + >; }; add(item: T, callback: CallbackAsyncQueue): void; invalidate(item: T): void; @@ -996,7 +998,7 @@ declare interface CallExpressionInfo { getMemberRanges: () => [number, number][]; } declare interface CallbackAsyncQueue { - (err?: null | WebpackError, result?: T): any; + (err?: null | WebpackError, result?: null | T): any; } declare interface CallbackCacheCache { (err: null | WebpackError, result?: T): void; @@ -1217,7 +1219,7 @@ declare class ChunkGraph { chunkGroup: ChunkGroup ): void; disconnectChunkGroup(chunkGroup: ChunkGroup): void; - getModuleId(module: Module): ModuleId; + getModuleId(module: Module): null | string | number; setModuleId(module: Module, id: ModuleId): void; getRuntimeId(runtime: string): string | number; setRuntimeId(runtime: string, id: string | number): void; @@ -1609,7 +1611,7 @@ declare interface CodeGenerationResult { /** * the runtime requirements */ - runtimeRequirements: ReadonlySet; + runtimeRequirements: null | ReadonlySet; /** * a hash of the code generation result (will be automatically calculated from sources and runtimeRequirements if not provided) @@ -1711,10 +1713,10 @@ declare class Compilation { * inspect, analyze, and/or modify the chunk graph. */ afterChunks: SyncHook<[Iterable]>; - optimizeDependencies: SyncBailHook<[Iterable], any>; + optimizeDependencies: SyncBailHook<[Iterable], boolean | void>; afterOptimizeDependencies: SyncHook<[Iterable]>; optimize: SyncHook<[]>; - optimizeModules: SyncBailHook<[Iterable], any>; + optimizeModules: SyncBailHook<[Iterable], boolean | void>; afterOptimizeModules: SyncHook<[Iterable]>; optimizeChunks: SyncBailHook< [Iterable, ChunkGroup[]], @@ -1739,13 +1741,13 @@ declare class Compilation { [Module, Set, RuntimeRequirementsContext] >; runtimeRequirementInModule: HookMap< - SyncBailHook<[Module, Set, RuntimeRequirementsContext], any> + SyncBailHook<[Module, Set, RuntimeRequirementsContext], void> >; additionalTreeRuntimeRequirements: SyncHook< [Chunk, Set, RuntimeRequirementsContext] >; runtimeRequirementInTree: HookMap< - SyncBailHook<[Chunk, Set, RuntimeRequirementsContext], any> + SyncBailHook<[Chunk, Set, RuntimeRequirementsContext], void> >; runtimeModule: SyncHook<[RuntimeModule, Chunk]>; reviveModules: SyncHook<[Iterable, any]>; @@ -1830,7 +1832,9 @@ declare class Compilation { >; statsFactory: SyncHook<[StatsFactory, NormalizedStatsOptions]>; statsPrinter: SyncHook<[StatsPrinter, NormalizedStatsOptions]>; - get normalModuleLoader(): SyncHook<[object, NormalModule]>; + get normalModuleLoader(): SyncHook< + [LoaderContextNormalModule, NormalModule] + >; }>; name?: string; startTime?: number; @@ -2941,8 +2945,8 @@ declare interface ContextTimestampAndHash { resolved?: ResolvedContextTimestampAndHash; symlinks?: Set; } -type CreateStatsOptionsContext = KnownCreateStatsOptionsContext & - Record; +type CreateStatsOptionsContext = Record & + KnownCreateStatsOptionsContext; type CreateWriteStreamFSImplementation = FSImplementation & { write: (...args: any[]) => any; close?: (...args: any[]) => any; @@ -3122,8 +3126,8 @@ declare class DefinePlugin { ): RuntimeValue; } declare class DelegatedPlugin { - constructor(options?: any); - options: any; + constructor(options: Options); + options: Options; /** * Apply the plugin @@ -3620,6 +3624,9 @@ declare interface Effect { type: string; value: any; } +declare interface EffectData { + [index: string]: any; +} declare class ElectronTargetPlugin { constructor(context?: "main" | "preload" | "renderer"); @@ -3982,9 +3989,9 @@ declare interface Environment { templateLiteral?: boolean; } declare class EnvironmentPlugin { - constructor(...keys: any[]); - keys: any[]; - defaultValues: any; + constructor(...keys: (string | string[] | Record)[]); + keys: string[]; + defaultValues: Record; /** * Apply the plugin @@ -4219,7 +4226,7 @@ declare abstract class ExportInfo { findTarget( moduleGraph: ModuleGraph, validTargetModuleFilter: (arg0: Module) => boolean - ): undefined | false | { module: Module; export?: string[] }; + ): undefined | null | false | { module: Module; export?: string[] }; getTarget( moduleGraph: ModuleGraph, resolveTargetFilter?: (arg0: { @@ -4244,8 +4251,8 @@ declare abstract class ExportInfo { ): undefined | { module: Module; export?: string[] }; createNestedExportsInfo(): undefined | ExportsInfo; getNestedExportsInfo(): undefined | ExportsInfo; - hasInfo(baseInfo?: any, runtime?: any): boolean; - updateHash(hash?: any, runtime?: any): void; + hasInfo(baseInfo: ExportInfo, runtime: RuntimeSpec): boolean; + updateHash(hash: Hash, runtime: RuntimeSpec): void; getUsedInfo(): string; getProvidedInfo(): | "no provided info" @@ -4810,16 +4817,12 @@ declare interface FileSystem { declare abstract class FileSystemInfo { fs: InputFileSystem; logger?: WebpackLogger; - fileTimestampQueue: AsyncQueue; - fileHashQueue: AsyncQueue; - contextTimestampQueue: AsyncQueue< - string, - string, - null | ContextFileSystemInfoEntry - >; - contextHashQueue: AsyncQueue; - contextTshQueue: AsyncQueue; - managedItemQueue: AsyncQueue; + fileTimestampQueue: AsyncQueue; + fileHashQueue: AsyncQueue; + contextTimestampQueue: AsyncQueue; + contextHashQueue: AsyncQueue; + contextTshQueue: AsyncQueue; + managedItemQueue: AsyncQueue; managedItemDirectoryQueue: AsyncQueue>; unmanagedPathsWithSlash: string[]; unmanagedPathsRegExps: RegExp[]; @@ -4865,7 +4868,7 @@ declare abstract class FileSystemInfo { path: string, callback: ( arg0?: null | WebpackError, - arg1?: ResolvedContextTimestampAndHash + arg1?: null | ResolvedContextTimestampAndHash ) => void ): void; resolveBuildDependencies( @@ -4886,7 +4889,7 @@ declare abstract class FileSystemInfo { directories: null | Iterable, missing: null | Iterable, options: undefined | null | SnapshotOptionsFileSystemInfo, - callback: (arg0?: null | WebpackError, arg1?: null | Snapshot) => void + callback: (arg0: null | WebpackError, arg1: null | Snapshot) => void ): void; mergeSnapshots(snapshot1: Snapshot, snapshot2: Snapshot): Snapshot; checkSnapshotValid( @@ -5074,7 +5077,7 @@ declare class GetChunkFilenameRuntimeModule extends RuntimeModule { static STAGE_TRIGGER: number; } declare interface GroupConfig { - getKeys: (arg0?: any) => string[]; + getKeys: (arg0?: any) => undefined | string[]; createGroup: (arg0: string, arg1: any[], arg2: any[]) => object; getOptions?: (arg0: string, arg1: any[]) => GroupOptions; } @@ -6976,14 +6979,14 @@ declare interface KnownStatsChunk { recorded: boolean; reason?: string; size: number; - sizes?: Record; - names?: string[]; - idHints?: string[]; + sizes: Record; + names: string[]; + idHints: string[]; runtime?: string[]; - files?: string[]; - auxiliaryFiles?: string[]; + files: string[]; + auxiliaryFiles: string[]; hash: string; - childrenByOrder?: Record; + childrenByOrder: Record; id?: string | number; siblings?: (string | number)[]; parents?: (string | number)[]; @@ -7006,11 +7009,11 @@ declare interface KnownStatsChunkGroup { isOverSizeLimit?: boolean; } declare interface KnownStatsChunkOrigin { - module?: string; - moduleIdentifier?: string; - moduleName?: string; - loc?: string; - request?: string; + module: string; + moduleIdentifier: string; + moduleName: string; + loc: string; + request: string; moduleId?: string | number; } declare interface KnownStatsCompilation { @@ -7055,14 +7058,14 @@ declare interface KnownStatsError { } declare interface KnownStatsFactoryContext { type: string; - makePathsRelative?: (arg0: string) => string; - compilation?: Compilation; - rootModules?: Set; - compilationFileToChunks?: Map; - compilationAuxiliaryFileToChunks?: Map; - runtime?: RuntimeSpec; - cachedGetErrors?: (arg0: Compilation) => WebpackError[]; - cachedGetWarnings?: (arg0: Compilation) => WebpackError[]; + makePathsRelative: (arg0: string) => string; + compilation: Compilation; + rootModules: Set; + compilationFileToChunks: Map; + compilationAuxiliaryFileToChunks: Map; + runtime: RuntimeSpec; + cachedGetErrors: (arg0: Compilation) => WebpackError[]; + cachedGetWarnings: (arg0: Compilation) => WebpackError[]; } declare interface KnownStatsLogging { entries: StatsLoggingEntry[]; @@ -7071,7 +7074,7 @@ declare interface KnownStatsLogging { } declare interface KnownStatsLoggingEntry { type: string; - message: string; + message?: string; trace?: string[]; children?: StatsLoggingEntry[]; args?: any[]; @@ -7080,10 +7083,10 @@ declare interface KnownStatsLoggingEntry { declare interface KnownStatsModule { type?: string; moduleType?: string; - layer?: string; + layer?: null | string; identifier?: string; name?: string; - nameForCondition?: string; + nameForCondition?: null | string; index?: number; preOrderIndex?: number; index2?: number; @@ -7098,45 +7101,45 @@ declare interface KnownStatsModule { optional?: boolean; orphan?: boolean; id?: string | number; - issuerId?: string | number; + issuerId?: null | string | number; chunks?: (string | number)[]; assets?: (string | number)[]; dependent?: boolean; - issuer?: string; - issuerName?: string; + issuer?: null | string; + issuerName?: null | string; issuerPath?: StatsModuleIssuer[]; failed?: boolean; errors?: number; warnings?: number; profile?: StatsProfile; reasons?: StatsModuleReason[]; - usedExports?: boolean | string[]; - providedExports?: string[]; + usedExports?: null | boolean | string[]; + providedExports?: null | string[]; optimizationBailout?: string[]; - depth?: number; + depth?: null | number; modules?: StatsModule[]; filteredModules?: number; source?: string | Buffer; } declare interface KnownStatsModuleIssuer { - identifier?: string; - name?: string; + identifier: string; + name: string; id?: string | number; - profile?: StatsProfile; + profile: StatsProfile; } declare interface KnownStatsModuleReason { - moduleIdentifier?: string; - module?: string; - moduleName?: string; - resolvedModuleIdentifier?: string; - resolvedModule?: string; - type?: string; + moduleIdentifier: null | string; + module: null | string; + moduleName: null | string; + resolvedModuleIdentifier: null | string; + resolvedModule: null | string; + type: null | string; active: boolean; - explanation?: string; - userRequest?: string; - loc?: string; - moduleId?: string | number; - resolvedModuleId?: string | number; + explanation: null | string; + userRequest: null | string; + loc?: null | string; + moduleId?: null | string | number; + resolvedModuleId?: null | string | number; } declare interface KnownStatsModuleTraceDependency { loc?: string; @@ -7150,20 +7153,31 @@ declare interface KnownStatsModuleTraceItem { originId?: string | number; moduleId?: string | number; } -declare interface KnownStatsPrinterContext { - type?: string; - compilation?: StatsCompilation; - chunkGroup?: StatsChunkGroup; - asset?: StatsAsset; - module?: StatsModule; - chunk?: StatsChunk; - moduleReason?: StatsModuleReason; +declare interface KnownStatsPrinterColorFn { bold?: (str: string) => string; yellow?: (str: string) => string; red?: (str: string) => string; green?: (str: string) => string; magenta?: (str: string) => string; cyan?: (str: string) => string; +} +declare interface KnownStatsPrinterContext { + type?: string; + compilation?: StatsCompilation; + chunkGroup?: StatsChunkGroup; + chunkGroupKind?: string; + asset?: StatsAsset; + module?: StatsModule; + chunk?: StatsChunk; + moduleReason?: StatsModuleReason; + moduleIssuer?: StatsModuleIssuer; + error?: StatsError; + profile?: StatsProfile; + logging?: StatsLogging; + moduleTraceItem?: StatsModuleTraceItem; + moduleTraceDependency?: StatsModuleTraceDependency; +} +declare interface KnownStatsPrinterFormaters { formatFilename?: (file: string, oversize?: boolean) => string; formatModuleId?: (id: string) => string; formatChunkId?: ( @@ -7171,10 +7185,11 @@ declare interface KnownStatsPrinterContext { direction?: "parent" | "child" | "sibling" ) => string; formatSize?: (size: number) => string; + formatLayer?: (size: string) => string; formatDateTime?: (dateTime: number) => string; formatFlag?: (flag: string) => string; formatTime?: (time: number, boldQuantity?: boolean) => string; - chunkGroupKind?: string; + formatError?: (message: string) => string; } declare interface KnownStatsProfile { total: number; @@ -7551,8 +7566,13 @@ declare class LoadScriptRuntimeModule extends HelperRuntimeModule { declare interface Loader { [index: string]: any; } -type LoaderContext = NormalModuleLoaderContext & - LoaderRunnerLoaderContext & +type LoaderContextDeclarationsIndex = + NormalModuleLoaderContext & + LoaderRunnerLoaderContext & + LoaderPluginLoaderContext & + HotModuleReplacementPluginLoaderContext; +type LoaderContextNormalModule = NormalModuleLoaderContext & + LoaderRunnerLoaderContext & LoaderPluginLoaderContext & HotModuleReplacementPluginLoaderContext; type LoaderDefinition< @@ -7638,14 +7658,14 @@ declare interface LoaderPluginLoaderContext { request: string, callback: ( err: null | Error, - source: string, - sourceMap: any, - module: NormalModule + source?: string | Buffer, + sourceMap?: null | object, + module?: Module ) => void ): void; importModule( request: string, - options: ImportModuleOptions, + options: undefined | ImportModuleOptions, callback: (err?: null | Error, exports?: any) => any ): void; importModule(request: string, options?: ImportModuleOptions): Promise; @@ -8034,7 +8054,7 @@ declare class Module extends DependenciesBlock { buildInfo?: BuildInfo; presentationalDependencies?: Dependency[]; codeGenerationDependencies?: Dependency[]; - id: ModuleId; + id: null | string | number; get hash(): string; get renderedHash(): string; profile?: ModuleProfile; @@ -8737,8 +8757,8 @@ declare abstract class MultiStats { get hash(): string; hasErrors(): boolean; hasWarnings(): boolean; - toJson(options?: any): StatsCompilation; - toString(options?: any): string; + toJson(options?: string | boolean | StatsOptions): StatsCompilation; + toString(options?: string | boolean | StatsOptions): string; } declare abstract class MultiWatching { watchings: Watching[]; @@ -8921,14 +8941,18 @@ declare class NormalModule extends Module { static deserialize(context?: any): NormalModule; } declare interface NormalModuleCompilationHooks { - loader: SyncHook<[object, NormalModule]>; - beforeLoaders: SyncHook<[LoaderItem[], NormalModule, object]>; + loader: SyncHook<[LoaderContextNormalModule, NormalModule]>; + beforeLoaders: SyncHook< + [LoaderItem[], NormalModule, LoaderContextNormalModule] + >; beforeParse: SyncHook<[NormalModule]>; beforeSnapshot: SyncHook<[NormalModule]>; readResourceForScheme: HookMap< - AsyncSeriesBailHook<[string, NormalModule], string | Buffer> + AsyncSeriesBailHook<[string, NormalModule], null | string | Buffer> + >; + readResource: HookMap< + AsyncSeriesBailHook<[LoaderContextNormalModule], string | Buffer> >; - readResource: HookMap>; needBuild: AsyncSeriesBailHook<[NormalModule, NeedBuildContext], boolean>; } declare interface NormalModuleCreateData { @@ -9224,6 +9248,9 @@ declare interface ObjectEncodingOptions { | "base64url" | "hex"; } +declare interface ObjectForExtract { + [index: string]: any; +} declare interface ObjectSerializer { serialize: (arg0: any, arg1: ObjectSerializerContext) => void; deserialize: (arg0: ObjectDeserializerContext) => any; @@ -9676,6 +9703,42 @@ declare interface OptimizationSplitChunksOptions { */ usedExports?: boolean; } +declare interface Options { + /** + * source + */ + source: string; + + /** + * absolute context path to which lib ident is relative to + */ + context: string; + + /** + * content + */ + content: DllReferencePluginOptionsContent; + + /** + * type + */ + type?: "object" | "require"; + + /** + * extensions + */ + extensions?: string[]; + + /** + * scope + */ + scope?: string; + + /** + * object for caching + */ + associatedObjectForCache?: object; +} declare abstract class OptionsApply { process(options?: any, compiler?: any): void; } @@ -11489,7 +11552,7 @@ declare interface ResolveBuildDependenciesResult { /** * stored resolve results */ - resolveResults: Map; + resolveResults: Map; /** * dependencies of the resolving @@ -12068,7 +12131,7 @@ declare interface RuleSet { /** * execute the rule set */ - exec: (arg0: object) => Effect[]; + exec: (arg0: EffectData) => Effect[]; } type RuleSetCondition = | string @@ -13159,32 +13222,36 @@ declare abstract class Snapshot { children?: Set; hasStartTime(): boolean; setStartTime(value: number): void; - setMergedStartTime(value?: any, snapshot?: any): void; + setMergedStartTime(value: undefined | number, snapshot: Snapshot): void; hasFileTimestamps(): boolean; - setFileTimestamps(value?: any): void; + setFileTimestamps(value: Map): void; hasFileHashes(): boolean; - setFileHashes(value?: any): void; + setFileHashes(value: Map): void; hasFileTshs(): boolean; - setFileTshs(value?: any): void; + setFileTshs(value: Map): void; hasContextTimestamps(): boolean; - setContextTimestamps(value?: any): void; + setContextTimestamps( + value: Map + ): void; hasContextHashes(): boolean; - setContextHashes(value?: any): void; + setContextHashes(value: Map): void; hasContextTshs(): boolean; - setContextTshs(value?: any): void; + setContextTshs( + value: Map + ): void; hasMissingExistence(): boolean; - setMissingExistence(value?: any): void; + setMissingExistence(value: Map): void; hasManagedItemInfo(): boolean; - setManagedItemInfo(value?: any): void; + setManagedItemInfo(value: Map): void; hasManagedFiles(): boolean; - setManagedFiles(value?: any): void; + setManagedFiles(value: Set): void; hasManagedContexts(): boolean; - setManagedContexts(value?: any): void; + setManagedContexts(value: Set): void; hasManagedMissing(): boolean; - setManagedMissing(value?: any): void; + setManagedMissing(value: Set): void; hasChildren(): boolean; - setChildren(value?: any): void; - addChild(child?: any): void; + setChildren(value: Set): void; + addChild(child: Snapshot): void; serialize(__0: ObjectSerializerContext): void; deserialize(__0: ObjectDeserializerContext): void; getFileIterable(): Iterable; @@ -13285,7 +13352,7 @@ declare abstract class SortableSet extends Set { /** * Sort with a comparer function */ - sortWith(sortFn: SortFunction): void; + sortWith(sortFn?: SortFunction): void; sort(): SortableSet; /** @@ -13657,59 +13724,73 @@ declare class Stats { toJson(options?: string | boolean | StatsOptions): StatsCompilation; toString(options?: string | boolean | StatsOptions): string; } -type StatsAsset = KnownStatsAsset & Record; -type StatsChunk = KnownStatsChunk & Record; -type StatsChunkGroup = KnownStatsChunkGroup & Record; -type StatsChunkOrigin = KnownStatsChunkOrigin & Record; -type StatsCompilation = KnownStatsCompilation & Record; -type StatsError = KnownStatsError & Record; +type StatsAsset = Record & KnownStatsAsset; +type StatsChunk = Record & KnownStatsChunk; +type StatsChunkGroup = Record & KnownStatsChunkGroup; +type StatsChunkOrigin = Record & KnownStatsChunkOrigin; +type StatsCompilation = Record & KnownStatsCompilation; +type StatsError = Record & KnownStatsError; declare abstract class StatsFactory { - hooks: Readonly<{ - extract: HookMap>; - filter: HookMap< - SyncBailHook<[any, StatsFactoryContext, number, number], any> - >; - sort: HookMap< - SyncBailHook< - [((arg0?: any, arg1?: any) => number)[], StatsFactoryContext], - any - > - >; - filterSorted: HookMap< - SyncBailHook<[any, StatsFactoryContext, number, number], any> - >; - groupResults: HookMap< - SyncBailHook<[GroupConfig[], StatsFactoryContext], any> - >; - sortResults: HookMap< - SyncBailHook< - [((arg0?: any, arg1?: any) => number)[], StatsFactoryContext], - any - > - >; - filterResults: HookMap< - SyncBailHook<[any, StatsFactoryContext, number, number], any> - >; - merge: HookMap>; - result: HookMap>; - getItemName: HookMap>; - getItemFactory: HookMap>; - }>; + hooks: StatsFactoryHooks; create( type: string, data: any, baseContext: Omit ): any; } -type StatsFactoryContext = KnownStatsFactoryContext & Record; -type StatsLogging = KnownStatsLogging & Record; -type StatsLoggingEntry = KnownStatsLoggingEntry & Record; -type StatsModule = KnownStatsModule & Record; -type StatsModuleIssuer = KnownStatsModuleIssuer & Record; -type StatsModuleReason = KnownStatsModuleReason & Record; -type StatsModuleTraceDependency = KnownStatsModuleTraceDependency & - Record; -type StatsModuleTraceItem = KnownStatsModuleTraceItem & Record; +type StatsFactoryContext = Record & KnownStatsFactoryContext; +declare interface StatsFactoryHooks { + extract: HookMap< + SyncBailHook<[ObjectForExtract, any, StatsFactoryContext], undefined> + >; + filter: HookMap< + SyncBailHook< + [any, StatsFactoryContext, number, number], + undefined | boolean + > + >; + sort: HookMap< + SyncBailHook< + [((arg0?: any, arg1?: any) => 0 | 1 | -1)[], StatsFactoryContext], + undefined + > + >; + filterSorted: HookMap< + SyncBailHook< + [any, StatsFactoryContext, number, number], + undefined | boolean + > + >; + groupResults: HookMap< + SyncBailHook<[GroupConfig[], StatsFactoryContext], undefined> + >; + sortResults: HookMap< + SyncBailHook< + [((arg0?: any, arg1?: any) => 0 | 1 | -1)[], StatsFactoryContext], + undefined + > + >; + filterResults: HookMap< + SyncBailHook< + [any, StatsFactoryContext, number, number], + undefined | boolean + > + >; + merge: HookMap>; + result: HookMap>; + getItemName: HookMap>; + getItemFactory: HookMap< + SyncBailHook<[any, StatsFactoryContext], undefined | StatsFactory> + >; +} +type StatsLogging = Record & KnownStatsLogging; +type StatsLoggingEntry = Record & KnownStatsLoggingEntry; +type StatsModule = Record & KnownStatsModule; +type StatsModuleIssuer = Record & KnownStatsModuleIssuer; +type StatsModuleReason = Record & KnownStatsModuleReason; +type StatsModuleTraceDependency = Record & + KnownStatsModuleTraceDependency; +type StatsModuleTraceItem = Record & KnownStatsModuleTraceItem; /** * Stats options object. @@ -14158,22 +14239,28 @@ declare interface StatsOptions { */ warningsSpace?: number; } -declare abstract class StatsPrinter { - hooks: Readonly<{ - sortElements: HookMap>; - printElements: HookMap< - SyncBailHook<[PrintedElement[], StatsPrinterContext], string> - >; - sortItems: HookMap>; - getItemName: HookMap>; - printItems: HookMap>; - print: HookMap>; - result: HookMap>; - }>; - print(type: string, object: object, baseContext?: object): string; +declare interface StatsPrintHooks { + sortElements: HookMap>; + printElements: HookMap< + SyncBailHook<[PrintedElement[], StatsPrinterContext], undefined | string> + >; + sortItems: HookMap>; + getItemName: HookMap>; + printItems: HookMap< + SyncBailHook<[string[], StatsPrinterContext], undefined | string> + >; + print: HookMap>; + result: HookMap>; } -type StatsPrinterContext = KnownStatsPrinterContext & Record; -type StatsProfile = KnownStatsProfile & Record; +declare abstract class StatsPrinter { + hooks: StatsPrintHooks; + print(type: string, object?: any, baseContext?: StatsPrinterContext): string; +} +type StatsPrinterContext = Record & + KnownStatsPrinterColorFn & + KnownStatsPrinterFormaters & + KnownStatsPrinterContext; +type StatsProfile = Record & KnownStatsProfile; type StatsValue = | boolean | StatsOptions @@ -14202,7 +14289,7 @@ declare class SyncModuleIdsPlugin { /** * operation mode (defaults to merge) */ - mode?: "read" | "merge" | "create" | "update"; + mode?: "read" | "create" | "merge" | "update"; }); /** @@ -14310,6 +14397,7 @@ declare interface UpdateHashContextGenerator { runtimeTemplate?: RuntimeTemplate; } type UsageStateType = 0 | 1 | 2 | 3 | 4; +type Value = string | number | boolean | RegExp; declare abstract class VariableInfo { declaredScope: ScopeInfo; freeName?: string | true; @@ -14992,14 +15080,7 @@ declare namespace exports { export let processArguments: ( args: Record, config: any, - values: Record< - string, - | string - | number - | boolean - | RegExp - | (string | number | boolean | RegExp)[] - > + values: Record ) => null | Problem[]; } export namespace ModuleFilenameHelpers { @@ -15581,7 +15662,7 @@ declare namespace exports { LoaderDefinitionFunction, PitchLoaderDefinitionFunction, RawLoaderDefinitionFunction, - LoaderContext + LoaderContextDeclarationsIndex as LoaderContext }; } declare const topLevelSymbolTag: unique symbol;