Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 23, 2024
1 parent e11f2ea commit 5ac0434
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 46 deletions.
2 changes: 1 addition & 1 deletion declarations/WebpackOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ const { isSourceEqual } = require("./util/source");
*/

/** @typedef {new (...args: any[]) => Dependency} DepConstructor */

/** @typedef {Record<string, Source>} CompilationAssets */

/**
Expand Down
9 changes: 7 additions & 2 deletions lib/WebpackOptionsApply.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/container/HoistContainerReferencesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand Down
15 changes: 14 additions & 1 deletion lib/hmr/LazyCompilationPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<BackendApi>} backend
*/

class LazyCompilationPlugin {
/**
* @param {object} options options
* @param {(function(Compiler, function(Error=, BackendApi?): void): void) | function(Compiler): Promise<BackendApi>} 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
Expand Down
8 changes: 1 addition & 7 deletions lib/hmr/lazyCompilationBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<LazyCompilationDefaultBackendOptions, "client"> & { client: NonNullable<LazyCompilationDefaultBackendOptions["client"]>}} options additional options for the backend
Expand Down
4 changes: 2 additions & 2 deletions lib/util/create-schema-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
112 changes: 85 additions & 27 deletions lib/util/deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,21 @@ module.exports.arrayToSetDeprecation = (set, name) => {
set[Symbol.isConcatSpreadable] = true;
};

/**
* @template T
* @param {string} name name
* @returns {{ new <T = any>(values?: readonly T[] | null): SetDeprecatedArray<T> }} 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) {
Expand All @@ -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<object>} frozen object with deprecation when modifying
* @returns {Proxy<T>} 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<T>} */ (
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
)
})
);
};

/**
Expand Down Expand Up @@ -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
});
}
Expand Down
2 changes: 1 addition & 1 deletion schemas/WebpackOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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<import(\"../lib/hmr/LazyCompilationPlugin\").BackendApi>))"
"tsType": "(((compiler: import('../lib/Compiler'), callback: (err: Error | null, api?: import(\"../lib/hmr/LazyCompilationPlugin\").BackendApi) => void) => void) | ((compiler: import('../lib/Compiler')) => Promise<import(\"../lib/hmr/LazyCompilationPlugin\").BackendApi>))"
},
{
"$ref": "#/definitions/LazyCompilationDefaultBackendOptions"
Expand Down
6 changes: 3 additions & 3 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2310,8 +2310,8 @@ declare interface CompilationHooksJavascriptModulesPlugin {
useSourceMap: SyncBailHook<[Chunk, RenderContext], boolean>;
}
declare interface CompilationHooksModuleFederationPlugin {
addContainerEntryDependency: SyncHook<any>;
addFederationRuntimeDependency: SyncHook<any>;
addContainerEntryDependency: SyncHook<Dependency>;
addFederationRuntimeDependency: SyncHook<Dependency>;
}
declare interface CompilationHooksRealContentHashPlugin {
updateHash: SyncBailHook<[Buffer[], string], string>;
Expand Down Expand Up @@ -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<BackendApi>)
| LazyCompilationDefaultBackendOptions;
Expand Down

0 comments on commit 5ac0434

Please sign in to comment.