From 5ac043418494fac1a6e384b1a0b9c7339b25adb6 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Thu, 24 Oct 2024 00:41:15 +0300 Subject: [PATCH] fix: types --- declarations/WebpackOptions.d.ts | 2 +- lib/Compilation.js | 1 + lib/WebpackOptionsApply.js | 9 +- .../HoistContainerReferencesPlugin.js | 4 +- lib/hmr/LazyCompilationPlugin.js | 15 ++- lib/hmr/lazyCompilationBackend.js | 8 +- lib/util/create-schema-validation.js | 4 +- lib/util/deprecation.js | 112 +++++++++++++----- schemas/WebpackOptions.json | 2 +- types.d.ts | 6 +- 10 files changed, 117 insertions(+), 46 deletions(-) diff --git a/declarations/WebpackOptions.d.ts b/declarations/WebpackOptions.d.ts index 137cb45d37f..d1473b2a364 100644 --- a/declarations/WebpackOptions.d.ts +++ b/declarations/WebpackOptions.d.ts @@ -3354,7 +3354,7 @@ export interface LazyCompilationOptions { | (( compiler: import("../lib/Compiler"), callback: ( - err?: Error, + err: Error | null, api?: import("../lib/hmr/LazyCompilationPlugin").BackendApi ) => void ) => void) diff --git a/lib/Compilation.js b/lib/Compilation.js index 178a032200e..a6270fb6e7f 100644 --- a/lib/Compilation.js +++ b/lib/Compilation.js @@ -167,6 +167,7 @@ const { isSourceEqual } = require("./util/source"); */ /** @typedef {new (...args: any[]) => Dependency} DepConstructor */ + /** @typedef {Record} CompilationAssets */ /** diff --git a/lib/WebpackOptionsApply.js b/lib/WebpackOptionsApply.js index 1ebc6fbd6b6..499b34b16d0 100644 --- a/lib/WebpackOptionsApply.js +++ b/lib/WebpackOptionsApply.js @@ -56,6 +56,8 @@ const DefaultStatsPrinterPlugin = require("./stats/DefaultStatsPrinterPlugin"); const { cleverMerge } = require("./util/cleverMerge"); /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */ +/** @typedef {import("../declarations/WebpackOptions").WebpackPluginFunction} WebpackPluginFunction */ +/** @typedef {import("../declarations/WebpackOptions").WebpackPluginInstance} WebpackPluginInstance */ /** @typedef {import("./Compiler")} Compiler */ /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ /** @typedef {import("./util/fs").IntermediateFileSystem} IntermediateFileSystem */ @@ -598,9 +600,12 @@ class WebpackOptionsApply extends OptionsApply { }).apply(compiler); } if (options.optimization.minimize) { - for (const minimizer of options.optimization.minimizer) { + for (const minimizer of /** @type {(WebpackPluginInstance | WebpackPluginFunction | "...")[]} */ ( + options.optimization.minimizer + )) { if (typeof minimizer === "function") { - minimizer.call(compiler, compiler); + /** @type {WebpackPluginFunction} */ + (minimizer).call(compiler, compiler); } else if (minimizer !== "..." && minimizer) { minimizer.apply(compiler); } diff --git a/lib/container/HoistContainerReferencesPlugin.js b/lib/container/HoistContainerReferencesPlugin.js index 3a6c7fd9b68..3435c98ef2f 100644 --- a/lib/container/HoistContainerReferencesPlugin.js +++ b/lib/container/HoistContainerReferencesPlugin.js @@ -11,9 +11,9 @@ const { STAGE_ADVANCED } = require("../OptimizationStages"); const memoize = require("../util/memoize"); const { forEachRuntime } = require("../util/runtime"); -/** @typedef {import("../Dependency")} Dependency */ -/** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Compilation")} Compilation */ +/** @typedef {import("../Compiler")} Compiler */ +/** @typedef {import("../Dependency")} Dependency */ /** @typedef {import("../Module")} Module */ const getModuleFederationPlugin = memoize(() => diff --git a/lib/hmr/LazyCompilationPlugin.js b/lib/hmr/LazyCompilationPlugin.js index 4b2d5c29990..e69bc3989a8 100644 --- a/lib/hmr/LazyCompilationPlugin.js +++ b/lib/hmr/LazyCompilationPlugin.js @@ -329,10 +329,23 @@ class LazyCompilationDependencyFactory extends ModuleFactory { } } +/** + * @callback BackendHandler + * @param {Compiler} compiler compiler + * @param {function(Error | null, BackendApi=): void} callback callback + * @returns {void} + */ + +/** + * @callback PromiseBackendHandler + * @param {Compiler} compiler compiler + * @returns {Promise} backend + */ + class LazyCompilationPlugin { /** * @param {object} options options - * @param {(function(Compiler, function(Error=, BackendApi?): void): void) | function(Compiler): Promise} options.backend the backend + * @param {BackendHandler | PromiseBackendHandler} options.backend the backend * @param {boolean} options.entries true, when entries are lazy compiled * @param {boolean} options.imports true, when import() modules are lazy compiled * @param {RegExp | string | (function(Module): boolean) | undefined} options.test additional filter for lazy compiled entrypoint modules diff --git a/lib/hmr/lazyCompilationBackend.js b/lib/hmr/lazyCompilationBackend.js index 9e21e6c6e42..383a16dd499 100644 --- a/lib/hmr/lazyCompilationBackend.js +++ b/lib/hmr/lazyCompilationBackend.js @@ -16,13 +16,7 @@ /** @typedef {import("../Compiler")} Compiler */ /** @typedef {import("../Module")} Module */ /** @typedef {import("./LazyCompilationPlugin").BackendApi} BackendApi */ - -/** - * @callback BackendHandler - * @param {Compiler} compiler compiler - * @param {function(Error | null, BackendApi=): void} callback callback - * @returns {void} - */ +/** @typedef {import("./LazyCompilationPlugin").BackendHandler} BackendHandler */ /** * @param {Omit & { client: NonNullable}} options additional options for the backend diff --git a/lib/util/create-schema-validation.js b/lib/util/create-schema-validation.js index 8f0eb5b1bba..79b2d1d9a63 100644 --- a/lib/util/create-schema-validation.js +++ b/lib/util/create-schema-validation.js @@ -7,11 +7,11 @@ const memoize = require("./memoize"); -const getValidate = memoize(() => require("schema-utils").validate); - /** @typedef {import("schema-utils/declarations/validate").ValidationErrorConfiguration} ValidationErrorConfiguration */ /** @typedef {import("./fs").JsonObject} JsonObject */ +const getValidate = memoize(() => require("schema-utils").validate); + /** * @template {object | object[]} T * @param {(function(T): boolean) | undefined} check check diff --git a/lib/util/deprecation.js b/lib/util/deprecation.js index a2692143882..d59cbd78f0f 100644 --- a/lib/util/deprecation.js +++ b/lib/util/deprecation.js @@ -179,9 +179,21 @@ module.exports.arrayToSetDeprecation = (set, name) => { set[Symbol.isConcatSpreadable] = true; }; +/** + * @template T + * @param {string} name name + * @returns {{ new (values?: readonly T[] | null): SetDeprecatedArray }} SetDeprecatedArray + */ module.exports.createArrayToSetDeprecationSet = name => { let initialized = false; + + /** + * @template T + */ class SetDeprecatedArray extends Set { + /** + * @param {readonly T[] | null=} items items + */ constructor(items) { super(items); if (!initialized) { @@ -197,40 +209,77 @@ module.exports.createArrayToSetDeprecationSet = name => { }; /** - * @param {object} obj object + * @template {object} T + * @param {T} obj object * @param {string} name property name * @param {string} code deprecation code * @param {string} note additional note - * @returns {Proxy} frozen object with deprecation when modifying + * @returns {Proxy} frozen object with deprecation when modifying */ module.exports.soonFrozenObjectDeprecation = (obj, name, code, note = "") => { const message = `${name} will be frozen in future, all modifications are deprecated.${ note && `\n${note}` }`; - return new Proxy(obj, { - set: util.deprecate( - (target, property, value, receiver) => - Reflect.set(target, property, value, receiver), - message, - code - ), - defineProperty: util.deprecate( - (target, property, descriptor) => - Reflect.defineProperty(target, property, descriptor), - message, - code - ), - deleteProperty: util.deprecate( - (target, property) => Reflect.deleteProperty(target, property), - message, - code - ), - setPrototypeOf: util.deprecate( - (target, proto) => Reflect.setPrototypeOf(target, proto), - message, - code - ) - }); + return /** @type {Proxy} */ ( + new Proxy(/** @type {object} */ (obj), { + set: util.deprecate( + /** + * @param {T} target target + * @param {string | symbol} property property + * @param {any} value value + * @param {any} receiver receiver + * @returns {boolean} result + */ + (target, property, value, receiver) => + Reflect.set( + /** @type {object} */ (target), + property, + value, + receiver + ), + message, + code + ), + defineProperty: util.deprecate( + /** + * @param {T} target target + * @param {string | symbol} property property + * @param {PropertyDescriptor} descriptor descriptor + * @returns {boolean} result + */ + (target, property, descriptor) => + Reflect.defineProperty( + /** @type {object} */ (target), + property, + descriptor + ), + message, + code + ), + deleteProperty: util.deprecate( + /** + * @param {T} target target + * @param {string | symbol} property property + * @returns {boolean} result + */ + (target, property) => + Reflect.deleteProperty(/** @type {object} */ (target), property), + message, + code + ), + setPrototypeOf: util.deprecate( + /** + * @param {T} target target + * @param {object | null} proto proto + * @returns {boolean} result + */ + (target, proto) => + Reflect.setPrototypeOf(/** @type {object} */ (target), proto), + message, + code + ) + }) + ); }; /** @@ -263,7 +312,16 @@ const deprecateAllProperties = (obj, message, code) => { enumerable: descriptor.enumerable, get: util.deprecate(() => value, message, code), set: descriptor.writable - ? util.deprecate(v => (value = v), message, code) + ? util.deprecate( + /** + * @template T + * @param {T} v value + * @returns {T} result + */ + v => (value = v), + message, + code + ) : undefined }); } diff --git a/schemas/WebpackOptions.json b/schemas/WebpackOptions.json index 340330fc61c..ca3d45b3d29 100644 --- a/schemas/WebpackOptions.json +++ b/schemas/WebpackOptions.json @@ -2066,7 +2066,7 @@ { "description": "A custom backend.", "instanceof": "Function", - "tsType": "(((compiler: import('../lib/Compiler'), callback: (err?: Error, api?: import(\"../lib/hmr/LazyCompilationPlugin\").BackendApi) => void) => void) | ((compiler: import('../lib/Compiler')) => Promise))" + "tsType": "(((compiler: import('../lib/Compiler'), callback: (err: Error | null, api?: import(\"../lib/hmr/LazyCompilationPlugin\").BackendApi) => void) => void) | ((compiler: import('../lib/Compiler')) => Promise))" }, { "$ref": "#/definitions/LazyCompilationDefaultBackendOptions" diff --git a/types.d.ts b/types.d.ts index 67ca534aa58..306349af62c 100644 --- a/types.d.ts +++ b/types.d.ts @@ -2310,8 +2310,8 @@ declare interface CompilationHooksJavascriptModulesPlugin { useSourceMap: SyncBailHook<[Chunk, RenderContext], boolean>; } declare interface CompilationHooksModuleFederationPlugin { - addContainerEntryDependency: SyncHook; - addFederationRuntimeDependency: SyncHook; + addContainerEntryDependency: SyncHook; + addFederationRuntimeDependency: SyncHook; } declare interface CompilationHooksRealContentHashPlugin { updateHash: SyncBailHook<[Buffer[], string], string>; @@ -7512,7 +7512,7 @@ declare interface LazyCompilationOptions { backend?: | (( compiler: Compiler, - callback: (err?: Error, api?: BackendApi) => void + callback: (err: null | Error, api?: BackendApi) => void ) => void) | ((compiler: Compiler) => Promise) | LazyCompilationDefaultBackendOptions;