Skip to content

Commit f07eba4

Browse files
fix: types for multi compiler
1 parent c1f2249 commit f07eba4

10 files changed

Lines changed: 434 additions & 76 deletions

generate-types-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module.exports = {
44
nameMapping: {
55
FsStats: /^Stats Import fs/,
66
validateFunction: /^validate Import/,
7-
Configuration: /^WebpackOptions /
7+
Configuration: /^WebpackOptions /,
8+
MultiConfiguration: /^MultiWebpackOptions /
89
},
910
exclude: [/^devServer in WebpackOptions /],
1011
include: [/^(_module|_compilation|_compiler) in NormalModuleLoaderContext /]

lib/MultiCompiler.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const ArrayQueue = require("./util/ArrayQueue");
1616

1717
/** @template T @typedef {import("tapable").AsyncSeriesHook<T>} AsyncSeriesHook<T> */
1818
/** @template T @template R @typedef {import("tapable").SyncBailHook<T, R>} SyncBailHook<T, R> */
19+
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
1920
/** @typedef {import("../declarations/WebpackOptions").WatchOptions} WatchOptions */
2021
/** @typedef {import("./Compiler")} Compiler */
2122
/** @typedef {import("./Stats")} Stats */
@@ -44,6 +45,8 @@ const ArrayQueue = require("./util/ArrayQueue");
4445
* @property {number=} parallelism how many Compilers are allows to run at the same time in parallel
4546
*/
4647

48+
/** @typedef {ReadonlyArray<WebpackOptions> & MultiCompilerOptions} MultiWebpackOptions */
49+
4750
const CLASS_NAME = "MultiCompiler";
4851

4952
module.exports = class MultiCompiler {
@@ -576,7 +579,7 @@ module.exports = class MultiCompiler {
576579
}
577580

578581
/**
579-
* @param {WatchOptions|WatchOptions[]} watchOptions the watcher's options
582+
* @param {WatchOptions | WatchOptions[]} watchOptions the watcher's options
580583
* @param {Callback<MultiStats>} handler signals when the call finishes
581584
* @returns {MultiWatching} a compiler watcher
582585
*/

lib/MultiStats.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const indent = (str, prefix) => {
2525
return prefix + rem;
2626
};
2727

28+
/** @typedef {undefined | string | boolean | StatsOptions} ChildrenStatsOptions */
29+
/** @typedef {Omit<StatsOptions, "children"> & { children?: ChildrenStatsOptions | ChildrenStatsOptions[] }} MultiStatsOptions */
2830
/** @typedef {{ version: boolean, hash: boolean, errorsCount: boolean, warningsCount: boolean, errors: boolean, warnings: boolean, children: NormalizedStatsOptions[] }} ChildOptions */
2931

3032
class MultiStats {
@@ -54,7 +56,7 @@ class MultiStats {
5456
}
5557

5658
/**
57-
* @param {string | boolean | StatsOptions | undefined} options stats options
59+
* @param {undefined | string | boolean | MultiStatsOptions} options stats options
5860
* @param {CreateStatsOptionsContext} context context
5961
* @returns {ChildOptions} context context
6062
*/
@@ -80,6 +82,9 @@ class MultiStats {
8082
const childOptions = Array.isArray(childrenOptions)
8183
? childrenOptions[idx]
8284
: childrenOptions;
85+
if (typeof childOptions === "boolean") {
86+
return stat.compilation.createStatsOptions(childOptions, context);
87+
}
8388
return stat.compilation.createStatsOptions(
8489
{
8590
...baseOptions,
@@ -104,7 +109,7 @@ class MultiStats {
104109
}
105110

106111
/**
107-
* @param {(string | boolean | StatsOptions)=} options stats options
112+
* @param {(string | boolean | MultiStatsOptions)=} options stats options
108113
* @returns {StatsCompilation} json output
109114
*/
110115
toJson(options) {
@@ -179,7 +184,7 @@ class MultiStats {
179184
}
180185

181186
/**
182-
* @param {(string | boolean | StatsOptions)=} options stats options
187+
* @param {(string | boolean | MultiStatsOptions)=} options stats options
183188
* @returns {string} string output
184189
*/
185190
toString(options) {

lib/cli.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,12 @@ const init = (open, close, replace) =>
840840
}} Colors */
841841

842842
/**
843-
* @param {{ useColor?: boolean }=} options options
843+
* @typedef {object} ColorsOptions
844+
* @property {boolean=} useColor force use colors
845+
*/
846+
847+
/**
848+
* @param {ColorsOptions=} options options
844849
* @returns {Colors} colors
845850
*/
846851
const createColors = ({ useColor = isColorSupported() } = {}) => ({

lib/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,18 @@ const memoize = require("./util/memoize");
4949
/** @typedef {import("./Compiler").AssetEmittedInfo} AssetEmittedInfo */
5050
/** @typedef {import("./Entrypoint")} Entrypoint */
5151
/** @typedef {import("./MultiCompiler").MultiCompilerOptions} MultiCompilerOptions */
52+
/** @typedef {import("./MultiCompiler").MultiWebpackOptions} MultiConfiguration */
5253
/** @typedef {import("./MultiStats")} MultiStats */
54+
/** @typedef {import("./MultiStats").MultiStatsOptions} MultiStatsOptions */
5355
/** @typedef {import("./NormalModuleFactory").ResolveData} ResolveData */
5456
/** @typedef {import("./Parser").ParserState} ParserState */
5557
/** @typedef {import("./ResolverFactory").ResolvePluginInstance} ResolvePluginInstance */
5658
/** @typedef {import("./ResolverFactory").Resolver} Resolver */
5759
/** @typedef {import("./Watching")} Watching */
5860
/** @typedef {import("./cli").Argument} Argument */
5961
/** @typedef {import("./cli").Problem} Problem */
62+
/** @typedef {import("./cli").Colors} Colors */
63+
/** @typedef {import("./cli").ColorsOptions} ColorsOptions */
6064
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsAsset} StatsAsset */
6165
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunk} StatsChunk */
6266
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunkGroup} StatsChunkGroup */
@@ -132,16 +136,16 @@ module.exports = mergeExports(fn, {
132136
return require("./webpack");
133137
},
134138
/**
135-
* @returns {(configuration: Configuration | Configuration[]) => void} validate fn
139+
* @returns {(configuration: Configuration | MultiConfiguration) => void} validate fn
136140
*/
137141
get validate() {
138142
const webpackOptionsSchemaCheck =
139-
/** @type {(configuration: Configuration | Configuration[]) => boolean} */
143+
/** @type {(configuration: Configuration | MultiConfiguration) => boolean} */
140144
(require("../schemas/WebpackOptions.check"));
141145

142146
const getRealValidate = memoize(
143147
/**
144-
* @returns {(configuration: Configuration | Configuration[]) => void} validate fn
148+
* @returns {(configuration: Configuration | MultiConfiguration) => void} validate fn
145149
*/
146150
() => {
147151
const validateSchema = require("./validateSchema");

lib/stats/DefaultStatsPresetPlugin.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,31 +144,31 @@ const NAMED_PRESETS = {
144144
};
145145

146146
/**
147-
* @param {Partial<NormalizedStatsOptions>} all stats option
147+
* @param {Partial<NormalizedStatsOptions>} all stats options
148148
* @returns {boolean} true when enabled, otherwise false
149149
*/
150150
const NORMAL_ON = ({ all }) => all !== false;
151151
/**
152-
* @param {Partial<NormalizedStatsOptions>} all stats option
152+
* @param {Partial<NormalizedStatsOptions>} all stats options
153153
* @returns {boolean} true when enabled, otherwise false
154154
*/
155155
const NORMAL_OFF = ({ all }) => all === true;
156156
/**
157-
* @param {Partial<NormalizedStatsOptions>} all stats option
157+
* @param {Partial<NormalizedStatsOptions>} all stats options
158158
* @param {CreateStatsOptionsContext} forToString stats options context
159159
* @returns {boolean} true when enabled, otherwise false
160160
*/
161161
const ON_FOR_TO_STRING = ({ all }, { forToString }) =>
162162
forToString ? all !== false : all === true;
163163
/**
164-
* @param {Partial<NormalizedStatsOptions>} all stats option
164+
* @param {Partial<NormalizedStatsOptions>} all stats options
165165
* @param {CreateStatsOptionsContext} forToString stats options context
166166
* @returns {boolean} true when enabled, otherwise false
167167
*/
168168
const OFF_FOR_TO_STRING = ({ all }, { forToString }) =>
169169
forToString ? all === true : all !== false;
170170
/**
171-
* @param {Partial<NormalizedStatsOptions>} all stats option
171+
* @param {Partial<NormalizedStatsOptions>} all stats options
172172
* @param {CreateStatsOptionsContext} forToString stats options context
173173
* @returns {boolean | "auto"} true when enabled, otherwise false
174174
*/

lib/webpack.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const memoize = require("./util/memoize");
2323
/** @typedef {import("../declarations/WebpackOptions").WebpackPluginFunction} WebpackPluginFunction */
2424
/** @typedef {import("./Compiler").WatchOptions} WatchOptions */
2525
/** @typedef {import("./MultiCompiler").MultiCompilerOptions} MultiCompilerOptions */
26+
/** @typedef {import("./MultiCompiler").MultiWebpackOptions} MultiWebpackOptions */
2627
/** @typedef {import("./MultiStats")} MultiStats */
2728
/** @typedef {import("./Stats")} Stats */
2829

@@ -100,14 +101,14 @@ const createCompiler = (rawOptions, compilerIndex) => {
100101
* @callback WebpackFunctionSingle
101102
* @param {WebpackOptions} options options object
102103
* @param {Callback<Stats>=} callback callback
103-
* @returns {Compiler} the compiler object
104+
* @returns {Compiler | null} the compiler object
104105
*/
105106

106107
/**
107108
* @callback WebpackFunctionMulti
108-
* @param {ReadonlyArray<WebpackOptions> & MultiCompilerOptions} options options objects
109+
* @param {MultiWebpackOptions} options options objects
109110
* @param {Callback<MultiStats>=} callback callback
110-
* @returns {MultiCompiler} the multi compiler object
111+
* @returns {MultiCompiler | null} the multi compiler object
111112
*/
112113

113114
/**
@@ -118,12 +119,15 @@ const createCompiler = (rawOptions, compilerIndex) => {
118119
const asArray = (options) =>
119120
Array.isArray(options) ? [...options] : [options];
120121

122+
/**
123+
* @callback WebpackCallback
124+
* @param {WebpackOptions | MultiWebpackOptions} options options
125+
* @param {Callback<Stats> & Callback<MultiStats>=} callback callback
126+
* @returns {Compiler | MultiCompiler | null} Compiler or MultiCompiler
127+
*/
128+
121129
const webpack = /** @type {WebpackFunctionSingle & WebpackFunctionMulti} */ (
122-
/**
123-
* @param {WebpackOptions | (ReadonlyArray<WebpackOptions> & MultiCompilerOptions)} options options
124-
* @param {Callback<Stats> & Callback<MultiStats>=} callback callback
125-
* @returns {Compiler | MultiCompiler | null} Compiler or MultiCompiler
126-
*/
130+
/** @type {WebpackCallback} */
127131
(options, callback) => {
128132
const create = () => {
129133
if (!asArray(options).every(webpackOptionsSchemaCheck)) {

0 commit comments

Comments
 (0)