Skip to content

Commit 7cff6da

Browse files
refactor(types): use real types instead of unknown and Record<string, unknown>
Type the minimizer options, transform switches, profiler parameters, CLI configuration values, HMR handlers and the deprecated externals function precisely.
1 parent 86d08ff commit 7cff6da

17 files changed

Lines changed: 55 additions & 37 deletions

‎lib/config/cli.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,9 @@ const getArguments = (schema = webpackSchema) => {
503503
return flags;
504504
};
505505

506-
/** @type {WeakMap<unknown[], number>} */
506+
/** @typedef {ObjectConfiguration | ParsedValue | undefined} ConfigurationValue */
507+
508+
/** @type {WeakMap<ConfigurationValue[], number>} */
507509
const cliAddedItems = new WeakMap();
508510

509511
/** @typedef {string | number} Property */
@@ -521,7 +523,7 @@ const isUnsafeKey = (name) =>
521523
* @param {ObjectConfiguration} config configuration
522524
* @param {string} schemaPath path in the config
523525
* @param {number | undefined} index index of value when multiple values are provided, otherwise undefined
524-
* @returns {{ problem?: LocalProblem, object?: ObjectConfiguration, property?: Property, value?: unknown }} problem or object with property and value
526+
* @returns {{ problem?: LocalProblem, object?: ObjectConfiguration, property?: Property, value?: ConfigurationValue }} problem or object with property and value
525527
*/
526528
const getObjectAndProperty = (config, schemaPath, index = 0) => {
527529
if (!schemaPath) return { value: config };

‎lib/config/defaults.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2594,7 +2594,7 @@ const applyOptimizationDefaults = (
25942594
}
25952595
/** @type {(typeof javascriptMinify | string)[]} */
25962596
const minify = [];
2597-
/** @type {Record<string, unknown>[]} */
2597+
/** @type {(typeof javascriptOptions | NonNullable<Parameters<typeof import("../css/cssMinify")>[1]> | NonNullable<Parameters<typeof import("../html/htmlMinify")>[1]> | Record<string, never>)[]} */
25982598
const minimizerOptions = [];
25992599
/** @type {string[]} */
26002600
const extensions = [];

‎lib/config/getClaimedAssetTypes.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { matchObject } = require("../devtool/ModuleFilenameHelpers");
99

1010
/** @import Compiler from "../Compiler" */
1111
/** @import { MatchObject } from "../devtool/ModuleFilenameHelpers" */
12-
/** @typedef {MatchObject & { minimizer?: unknown }} MinimizerOptions */
12+
/** @typedef {MatchObject & { minimizer?: import("minimizer-webpack-plugin").InternalPluginOptions<EXPECTED_ANY>["minimizer"] }} MinimizerOptions */
1313

1414
/**
1515
* @param {unknown} candidate a configured plugin or minimizer entry

‎lib/css/syntax-parser.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7563,14 +7563,14 @@ const _keptComments = (comments) => {
75637563
* `optimization.minimize.css` names beside `environment` and the two rewrites
75647564
* that are off until asked for. Exported so the minify functions the minimizer
75657565
* plugin ships to its workers can pick them without repeating the names.
7566-
* @param {Record<string, unknown>} options an options object that may carry them
7566+
* @param {CssTransformOptions} options an options object that may carry them
75677567
* @returns {CssTransformOptions | undefined} the switches, or undefined when none is set
75687568
*/
75697569
const pickTransforms = (options) => {
75707570
/** @type {EXPECTED_ANY} */
75717571
let out;
75727572
for (const name of Object.keys(_ALL_TRANSFORMS)) {
7573-
const value = options[name];
7573+
const value = options[/** @type {keyof CssTransformOptions} */ (name)];
75747574
if (value === undefined) continue;
75757575
if (out === undefined) out = {};
75767576
out[name] = value;

‎lib/debug/ProfilingPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Profiler {
118118
/**
119119
* Returns promise for the result.
120120
* @param {string} method method name
121-
* @param {Record<string, unknown>=} params params
121+
* @param {import("inspector").Profiler.SetSamplingIntervalParameterType=} params params
122122
* @returns {Promise<EXPECTED_ANY | void>} Promise for the result
123123
*/
124124
sendCommand(method, params) {

‎lib/externals/ExternalModuleFactoryPlugin.js‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const ExternalModule = require("./ExternalModule");
1818
* ResolveOptions,
1919
* ExternalsType,
2020
* ExternalItem,
21+
* ExternalItemFunction,
2122
* ExternalItemValue,
2223
* ExternalItemObjectKnown,
2324
* ExternalItemObjectUnknown,
@@ -71,20 +72,17 @@ const getAlternateCoreModuleRequest = (request) => {
7172
return coreModules.has(request) ? NODE_PREFIX + request : undefined;
7273
};
7374

74-
/** @typedef {(context: string, request: string, callback: (err: Error | null | undefined, value: ExternalValue | undefined, type: ExternalsType | undefined) => void) => void} DeprecatedExternalItemFunction */
75-
7675
// TODO in the next major release: remove this
7776
const callDeprecatedExternals = util.deprecate(
7877
/**
7978
* Handles the callback logic for this hook.
80-
* @param {DeprecatedExternalItemFunction} externalsFunction externals function
79+
* @param {ExternalItemFunction} externalsFunction externals function, written in the deprecated `(context, request, callback)` form
8180
* @param {string} context context
8281
* @param {string} request request
8382
* @param {(err: Error | null | undefined, value: ExternalValue | undefined, ty: ExternalsType | undefined) => void} cb cb
8483
*/
8584
(externalsFunction, context, request, cb) => {
86-
// eslint-disable-next-line no-useless-call
87-
externalsFunction.call(null, context, request, cb);
85+
Reflect.apply(externalsFunction, null, [context, request, cb]);
8886
},
8987
"The externals-function should be defined like ({context, request}, cb) => { ... }",
9088
"DEP_WEBPACK_EXTERNALS_FUNCTION_PARAMETERS"
@@ -386,14 +384,7 @@ class ExternalModuleFactoryPlugin {
386384
};
387385
if (externals.length === 3) {
388386
// TODO in the next major release: remove this
389-
callDeprecatedExternals(
390-
/** @type {DeprecatedExternalItemFunction} */ (
391-
/** @type {unknown} */ (externals)
392-
),
393-
context,
394-
request,
395-
cb
396-
);
387+
callDeprecatedExternals(externals, context, request, cb);
397388
} else {
398389
const promise = externals(
399390
{

‎lib/hmr/JavascriptHotModuleReplacement.runtime.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
/** @typedef {(data: HotData) => void} DisposeHandler */
1111
/** @typedef {(outdatedDependencies: ModuleId[]) => void} AcceptCallback */
1212
/** @typedef {(err: unknown, context: { moduleId: ModuleId, dependencyId: ModuleId }) => void} AcceptErrorHandler */
13-
/** @typedef {(err: unknown, context: { moduleId: ModuleId, module: HotModule }) => void} SelfAcceptErrorHandler */
14-
/** @typedef {(module: HotModule, exports: unknown, require: (id: ModuleId) => unknown) => void} ModuleFactory */
13+
/** @typedef {(err: Error, context: { moduleId: ModuleId, module: HotModule }) => void} SelfAcceptErrorHandler */
14+
/** @typedef {(module: HotModule, exports: EXPECTED_ANY, require: typeof __webpack_require__) => void} ModuleFactory */
1515
/** @typedef {{ type: string, moduleId: ModuleId, dependencyId?: ModuleId, error: unknown, originalError?: unknown }} HotErrorEvent */
1616
/** @typedef {(reportError: (err: unknown) => void) => Promise<ModuleId[]>} ApplyFn */
1717

@@ -513,7 +513,7 @@ module.exports = function () {
513513
} catch (err) {
514514
if (typeof item.errorHandler === "function") {
515515
try {
516-
item.errorHandler(err, {
516+
item.errorHandler(/** @type {Error} */ (err), {
517517
moduleId: moduleId,
518518
module: $moduleCache$[moduleId]
519519
});

‎lib/html/htmlMinify.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
"use strict";
77

8-
/** @import { HtmlPrintOptions } from "../html/syntax-printer" */
9-
/** @import { CssEnvironment } from "../css/syntax-parser" */
8+
/** @import { HtmlPrintOptions, HtmlTransformOptions } from "../html/syntax-printer" */
9+
/** @import { CssEnvironment, CssTransformOptions } from "../css/syntax-parser" */
1010

1111
/**
1212
* What a renderer made of one embedded body: the minified text, and anything it
@@ -37,7 +37,7 @@
3737
* re-resolves in the worker — via the installed package, or the dev self-link.
3838
* @param {{ [file: string]: string | Buffer }} input a single `{ filename: code }` entry; a `Buffer` is read as UTF-8
3939
* @param {(RawSourceMap | undefined)=} sourceMap input source map (unused: safe serialize keeps positions token-coarse, so no map is produced yet)
40-
* @param {Omit<HtmlPrintOptions, "renderEmbeddedSource" | "deferEmbeddedSource"> & { environment?: CssEnvironment, css?: { convertLengthUnits?: boolean, convertApproximateColors?: boolean, dropOverriddenDeclarations?: boolean, rewriteCustomProperties?: boolean, unusedSymbols?: string[], pseudoClasses?: { [name: string]: string } }, minifyConditionalComments?: boolean, extractComments?: MinimizeExtractComments, renderEmbeddedSource?: AsyncEmbeddedSourceRenderer }=} minimizerOptions minimizer options — `environment` and `css` (`optimization.minimize.css`, whole) reach the CSS minifier this runs over inline CSS; the rest is `optimization.minimize.html`. The two stand apart rather than merged: both languages name a `quotes` and a `comments`, and one flat object would hand each the other's answer. `renderEmbeddedSource` minifies source this document embeds and may be asynchronous — one parse serves both it and the output. What it declines this minifies itself, for the languages webpack ships a minifier for
40+
* @param {Omit<HtmlPrintOptions, "renderEmbeddedSource" | "deferEmbeddedSource"> & { environment?: CssEnvironment, css?: { convertLengthUnits?: boolean, convertApproximateColors?: boolean, dropOverriddenDeclarations?: boolean, rewriteCustomProperties?: boolean, unusedSymbols?: string[], pseudoClasses?: { [name: string]: string } } & CssTransformOptions, minifyConditionalComments?: boolean, extractComments?: MinimizeExtractComments, renderEmbeddedSource?: AsyncEmbeddedSourceRenderer } & HtmlTransformOptions=} minimizerOptions minimizer options — `environment` and `css` (`optimization.minimize.css`, whole) reach the CSS minifier this runs over inline CSS; the rest is `optimization.minimize.html`. The two stand apart rather than merged: both languages name a `quotes` and a `comments`, and one flat object would hand each the other's answer. `renderEmbeddedSource` minifies source this document embeds and may be asynchronous — one parse serves both it and the output. What it declines this minifies itself, for the languages webpack ships a minifier for
4141
* @param {ExtractCommentsOption=} extractComments the plugin's `extractComments`, read where `minimize.html.extractComments` is not set
4242
* @returns {Promise<{ code: string, extractedComments?: string[], warnings?: (Error | string)[], errors?: (Error | string)[] }>} the minified HTML, the comments taken out of it, and what a renderer reported over what it embeds
4343
*/

‎lib/html/syntax-parser.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9534,14 +9534,14 @@ let _transforms = _DEFAULT_TRANSFORMS;
95349534
* `optimization.minimize.html` names beside the transforms that are off until
95359535
* asked for. Exported so the minify function the minimizer plugin ships to its
95369536
* workers can pick them without repeating the names.
9537-
* @param {Record<string, unknown>} options an options object that may carry them
9537+
* @param {HtmlTransformOptions} options an options object that may carry them
95389538
* @returns {HtmlTransformOptions | undefined} the switches, or undefined when none is set
95399539
*/
95409540
const pickTransforms = (options) => {
95419541
/** @type {EXPECTED_ANY} */
95429542
let out;
95439543
for (const name of Object.keys(_ALL_TRANSFORMS)) {
9544-
const value = options[name];
9544+
const value = options[/** @type {keyof HtmlTransformOptions} */ (name)];
95459545
if (value === undefined) continue;
95469546
if (out === undefined) out = {};
95479547
out[name] = value;

‎lib/javascript/syntax-printer.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14509,7 +14509,7 @@ const installFusedParse = (modules) => {
1450914509
* @param {string} source the source being parsed
1451014510
* @param {string | null} filename what its tokens name as their file
1451114511
* @param {number} base the number of this parse's first token
14512-
* @returns {{ onComment: import("./syntax-parser").OnComment, onToken: (parser: EXPECTED_ANY) => void, shebang: (text: string) => void, finish: () => void, at: (pos: number, ending: boolean, forward?: number) => number | undefined, table: TokenTable }} where to report comments and tokens, the token at an offset, what narrows the tables once the parse is over, and the table its numbers are read through
14512+
* @returns {{ onComment: import("./syntax-parser").OnComment, onToken: NonNullable<import("./syntax-parser").Options["onTokenRead"]>, shebang: (text: string) => void, finish: () => void, at: (pos: number, ending: boolean, forward?: number) => number | undefined, table: TokenTable }} where to report comments and tokens, the token at an offset, what narrows the tables once the parse is over, and the table its numbers are read through
1451314513
*/
1451414514
const collectTokens = (source, filename, base) => {
1451514515
// Where each token sits, its kind as an index into `KINDS`, and whether a

0 commit comments

Comments
 (0)