Skip to content

Commit f3c40d0

Browse files
refactor(types): type stats groups by path through StatsFactoryGroupKeys
groupResults taps now check createGroup against the group shape of their array path (assets, modules, reasons) instead of object, and plugins declare their own group shapes by declaration merging.
1 parent 105335d commit f3c40d0

4 files changed

Lines changed: 141 additions & 11 deletions

File tree

‎lib/index.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const getCli = memoize(() => ({
109109
/** @typedef {import("./config/defineConfig").ConfigurationFactory} ConfigurationFactory */
110110
/** @typedef {import("./config/defineConfig").DefineConfigInput} DefineConfigInput */
111111
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsAsset} StatsAsset */
112+
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsAssetGroup} StatsAssetGroup */
112113
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunk} StatsChunk */
113114
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunkGroup} StatsChunkGroup */
114115
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsChunkOrigin} StatsChunkOrigin */
@@ -117,8 +118,10 @@ const getCli = memoize(() => ({
117118
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsLogging} StatsLogging */
118119
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsLoggingEntry} StatsLoggingEntry */
119120
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModule} StatsModule */
121+
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModuleGroup} StatsModuleGroup */
120122
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModuleIssuer} StatsModuleIssuer */
121123
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModuleReason} StatsModuleReason */
124+
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModuleReasonGroup} StatsModuleReasonGroup */
122125
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModuleTraceDependency} StatsModuleTraceDependency */
123126
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsModuleTraceItem} StatsModuleTraceItem */
124127
/** @typedef {import("./stats/DefaultStatsFactoryPlugin").StatsProfile} StatsProfile */

‎lib/stats/DefaultStatsFactoryPlugin.js‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
3434
* @import StatsFactory, {
3535
* StatsFactoryContext,
3636
* StatsFactoryExtractKeys,
37+
* StatsFactoryGroup,
38+
* StatsFactoryGroupKeys,
3739
* StatsFactoryItemKeys,
3840
* StatsFactoryItemNameKeys,
3941
* StatsFactoryMergedItems
@@ -165,6 +167,18 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
165167
* @property {boolean=} isOverSizeLimit
166168
*/
167169

170+
/** @typedef {KnownStatsAssetGroup & Record<string, StatsJsonValue>} StatsAssetGroup */
171+
172+
/**
173+
* A group of assets that `groupResults` creates, listing them in `children` or counting hidden ones in `filteredChildren`.
174+
* @typedef {object} KnownStatsAssetGroup
175+
* @property {string} type
176+
* @property {number} size
177+
* @property {string=} name
178+
* @property {StatsAsset[]=} children
179+
* @property {number=} filteredChildren
180+
*/
181+
168182
/** @typedef {KnownStatsChunkGroup & Record<string, StatsJsonValue>} StatsChunkGroup */
169183
/**
170184
* Defines the known stats chunk group type used by this module.
@@ -255,6 +269,32 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
255269
* @property {StatsProfile} profile
256270
*/
257271

272+
/** @typedef {KnownStatsModuleGroup & Record<string, StatsJsonValue>} StatsModuleGroup */
273+
274+
/**
275+
* A group of modules that `groupResults` creates, listing them in `children` or counting hidden ones in `filteredChildren`.
276+
* @typedef {object} KnownStatsModuleGroup
277+
* @property {string} type
278+
* @property {number} size
279+
* @property {Record<string, number>} sizes
280+
* @property {string=} name
281+
* @property {string=} moduleType
282+
* @property {string=} layer
283+
* @property {StatsModule[]=} children
284+
* @property {number=} filteredChildren
285+
*/
286+
287+
/** @typedef {KnownStatsModuleReasonGroup & Record<string, StatsJsonValue>} StatsModuleReasonGroup */
288+
289+
/**
290+
* A group of reasons from one origin module that `groupResults` creates.
291+
* @typedef {object} KnownStatsModuleReasonGroup
292+
* @property {string} type
293+
* @property {string} module
294+
* @property {boolean} active
295+
* @property {StatsModuleReason[]} children
296+
*/
297+
258298
/** @typedef {KnownStatsModuleReason & Record<string, StatsJsonValue>} StatsModuleReason */
259299
/**
260300
* Defines the known stats module reason type used by this module.
@@ -384,9 +424,9 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
384424
*/
385425

386426
/**
387-
* The `groupResults` taps of one array path, by option, over its created item; the group type is the hook's default.
388-
* @template I
389-
* @typedef {Record<string, (groupConfigs: GroupConfig<I, EXPECTED_OBJECT>[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} GroupersByOption
427+
* The `groupResults` taps of one array path, by option, over its created item and group.
428+
* @template I, G
429+
* @typedef {Record<string, (groupConfigs: GroupConfig<I, G>[], context: StatsFactoryContext, options: NormalizedStatsOptions) => void>} GroupersByOption
390430
*/
391431

392432
/**
@@ -2179,7 +2219,7 @@ const GROUP_PATH_REGEXP = /(.+)[/\\][^/\\]+?(?:\?|(?: \+ \d+ modules?)?$)/;
21792219

21802220
/** @typedef {(name: string, asset: StatsAsset) => boolean} AssetFilterItemFn */
21812221

2182-
/** @type {GroupersByOption<StatsAsset>} */
2222+
/** @type {GroupersByOption<StatsAsset, StatsAssetGroup>} */
21832223
const ASSETS_GROUPERS = {
21842224
_: (groupConfigs, context, options) => {
21852225
/**
@@ -2328,7 +2368,7 @@ const ASSETS_GROUPERS = {
23282368
/** @typedef {(name: string, module: StatsModule, type: "module" | "chunk" | "root-of-chunk" | "nested") => boolean} ModuleFilterItemTypeFn */
23292369

23302370
/**
2331-
* @type {(type: ExcludeModulesType) => GroupersByOption<StatsModule>}
2371+
* @type {(type: ExcludeModulesType) => GroupersByOption<StatsModule, StatsModuleGroup>}
23322372
*/
23332373
const MODULES_GROUPERS = (type) => ({
23342374
_: (groupConfigs, context, options) => {
@@ -2514,7 +2554,7 @@ const MODULES_GROUPERS = (type) => ({
25142554
}
25152555
});
25162556

2517-
/** @type {GroupersByOption<StatsModuleReason>} */
2557+
/** @type {GroupersByOption<StatsModuleReason, StatsModuleReasonGroup>} */
25182558
const MODULE_REASONS_GROUPERS = {
25192559
groupReasonsByOrigin: (groupConfigs) => {
25202560
groupConfigs.push({
@@ -2529,7 +2569,7 @@ const MODULE_REASONS_GROUPERS = {
25292569
}
25302570
};
25312571

2532-
/** @type {{ [K in "compilation.assets" | "asset.related" | "compilation.modules" | "chunk.modules" | "chunk.rootModules" | "module.modules" | "module.reasons"]: GroupersByOption<StatsFactoryItemKeys[K][1]> }} */
2572+
/** @type {{ [K in keyof StatsFactoryGroupKeys]: GroupersByOption<StatsFactoryItemKeys[K][1], StatsFactoryGroup<K>> }} */
25332573
const RESULT_GROUPERS = {
25342574
"compilation.assets": ASSETS_GROUPERS,
25352575
"asset.related": ASSETS_GROUPERS,

‎lib/stats/StatsFactory.js‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ const smartGrouping = require("./smartGrouping");
3333
* ModuleTrace,
3434
* PreprocessedAsset,
3535
* StatsAsset,
36+
* StatsAssetGroup,
3637
* StatsChunk,
3738
* StatsChunkGroup,
3839
* StatsChunkOrigin,
3940
* StatsCompilation,
4041
* StatsError,
4142
* StatsModule,
43+
* StatsModuleGroup,
4244
* StatsModuleIssuer,
4345
* StatsModuleReason,
46+
* StatsModuleReasonGroup,
4447
* StatsModuleTraceDependency,
4548
* StatsModuleTraceItem,
4649
* StatsProfile
@@ -138,6 +141,27 @@ const smartGrouping = require("./smartGrouping");
138141
* @typedef {StatsFactoryItemKeys[K] extends [EXPECTED_ANY, EXPECTED_ANY, infer M] ? M : StatsFactoryItemKeys[K][1][]} StatsFactoryMergedItems
139142
*/
140143

144+
/**
145+
* The group object `groupResults` taps create for each array path of `StatsFactoryItemKeys` they group.
146+
* A plugin types its own groups by declaration merging: `declare module "webpack" { interface StatsFactoryGroupKeys { "myPlugin.items": MyGroup } }`.
147+
* @augmentable
148+
* @typedef {{
149+
* "compilation.assets": StatsAssetGroup,
150+
* "asset.related": StatsAssetGroup,
151+
* "compilation.modules": StatsModuleGroup,
152+
* "chunk.modules": StatsModuleGroup,
153+
* "chunk.rootModules": StatsModuleGroup,
154+
* "module.modules": StatsModuleGroup,
155+
* "module.reasons": StatsModuleReasonGroup,
156+
* }} StatsFactoryGroupKeys
157+
*/
158+
159+
/**
160+
* The group `groupResults` taps create for an array path: its `StatsFactoryGroupKeys` entry, else any object.
161+
* @template {keyof StatsFactoryItemKeys} K
162+
* @typedef {K extends keyof StatsFactoryGroupKeys ? StatsFactoryGroupKeys[K] : EXPECTED_OBJECT} StatsFactoryGroup
163+
*/
164+
141165
/**
142166
* The item `getItemName` receives for each `<array path>[]` key: the first element of that path's `StatsFactoryItemKeys` entry.
143167
* A plugin types its own keys by declaration merging: `declare module "webpack" { interface StatsFactoryItemNameKeys { "myPlugin.items[]": MyItem } }`.
@@ -216,9 +240,9 @@ const smartGrouping = require("./smartGrouping");
216240
*/
217241

218242
/**
219-
* `groupResults` hooks: group configs over the exact created item for array paths in `StatsFactoryItemKeys`, open for others.
243+
* `groupResults` hooks: group configs over the exact created item and its `StatsFactoryGroup` for array paths in `StatsFactoryItemKeys`, open for others.
220244
* @typedef {{
221-
* for<K extends keyof StatsFactoryItemKeys, G = object>(key: K): SyncBailHook<[GroupConfig<StatsFactoryItemKeys[K][1], G>[], StatsFactoryContext], void>,
245+
* for<K extends keyof StatsFactoryItemKeys>(key: K): SyncBailHook<[GroupConfig<StatsFactoryItemKeys[K][1], StatsFactoryGroup<K>>[], StatsFactoryContext], void>,
222246
* for(key: string): SyncBailHook<[GroupConfig<StatsPluginValue, StatsPluginValue>[], StatsFactoryContext], void>
223247
* } & HookMap<SyncBailHook<[GroupConfig<StatsPluginValue, StatsPluginValue>[], StatsFactoryContext], void>>} StatsFactoryGroupResultsHookMap
224248
*/

‎types.d.ts‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18880,6 +18880,17 @@ declare interface KnownStatsAsset {
1888018880
isOverSizeLimit?: boolean;
1888118881
}
1888218882

18883+
/**
18884+
* A group of assets that `groupResults` creates, listing them in `children` or counting hidden ones in `filteredChildren`.
18885+
*/
18886+
declare interface KnownStatsAssetGroup {
18887+
type: string;
18888+
size: number;
18889+
name?: string;
18890+
children?: StatsAsset[];
18891+
filteredChildren?: number;
18892+
}
18893+
1888318894
/**
1888418895
* Defines the known stats chunk type used by this module.
1888518896
*/
@@ -19073,6 +19084,20 @@ declare interface KnownStatsModule {
1907319084
source?: string | Buffer;
1907419085
}
1907519086

19087+
/**
19088+
* A group of modules that `groupResults` creates, listing them in `children` or counting hidden ones in `filteredChildren`.
19089+
*/
19090+
declare interface KnownStatsModuleGroup {
19091+
type: string;
19092+
size: number;
19093+
sizes: Record<string, number>;
19094+
name?: string;
19095+
moduleType?: string;
19096+
layer?: string;
19097+
children?: StatsModule[];
19098+
filteredChildren?: number;
19099+
}
19100+
1907619101
/**
1907719102
* Defines the known stats module issuer type used by this module.
1907819103
*/
@@ -19101,6 +19126,16 @@ declare interface KnownStatsModuleReason {
1910119126
resolvedModuleId?: null | string | number;
1910219127
}
1910319128

19129+
/**
19130+
* A group of reasons from one origin module that `groupResults` creates.
19131+
*/
19132+
declare interface KnownStatsModuleReasonGroup {
19133+
type: string;
19134+
module: string;
19135+
active: boolean;
19136+
children: StatsModuleReason[];
19137+
}
19138+
1910419139
/**
1910519140
* Defines the known stats module trace dependency type used by this module.
1910619141
*/
@@ -33251,6 +33286,7 @@ declare class Stats {
3325133286
): string;
3325233287
}
3325333288
type StatsAsset = KnownStatsAsset & Record<string, StatsJsonValue>;
33289+
type StatsAssetGroup = KnownStatsAssetGroup & Record<string, StatsJsonValue>;
3325433290
type StatsChunk = KnownStatsChunk & Record<string, StatsJsonValue>;
3325533291
type StatsChunkGroup = KnownStatsChunkGroup & Record<string, StatsJsonValue>;
3325633292
type StatsChunkOrigin = KnownStatsChunkOrigin & Record<string, StatsJsonValue>;
@@ -33305,11 +33341,18 @@ type StatsFactoryGetItemNameHookMap = {
3330533341
>;
3330633342
for(key: string): SyncBailHook<[any, StatsFactoryContext], string | void>;
3330733343
} & HookMap<SyncBailHook<[any, StatsFactoryContext], string | void>>;
33344+
type StatsFactoryGroup<K extends keyof exports.StatsFactoryItemKeys> =
33345+
K extends keyof exports.StatsFactoryGroupKeys
33346+
? exports.StatsFactoryGroupKeys[K]
33347+
: object;
3330833348
type StatsFactoryGroupResultsHookMap = {
33309-
for<K extends keyof exports.StatsFactoryItemKeys, G = object>(
33349+
for<K extends keyof exports.StatsFactoryItemKeys>(
3331033350
key: K
3331133351
): SyncBailHook<
33312-
[GroupConfig<exports.StatsFactoryItemKeys[K][1], G>[], StatsFactoryContext],
33352+
[
33353+
GroupConfig<exports.StatsFactoryItemKeys[K][1], StatsFactoryGroup<K>>[],
33354+
StatsFactoryContext
33355+
],
3331333356
void
3331433357
>;
3331533358
for(
@@ -33422,10 +33465,13 @@ type StatsLogging = KnownStatsLogging & Record<string, StatsJsonValue>;
3342233465
type StatsLoggingEntry = KnownStatsLoggingEntry &
3342333466
Record<string, StatsJsonValue>;
3342433467
type StatsModule = KnownStatsModule & Record<string, StatsJsonValue>;
33468+
type StatsModuleGroup = KnownStatsModuleGroup & Record<string, StatsJsonValue>;
3342533469
type StatsModuleIssuer = KnownStatsModuleIssuer &
3342633470
Record<string, StatsJsonValue>;
3342733471
type StatsModuleReason = KnownStatsModuleReason &
3342833472
Record<string, StatsJsonValue>;
33473+
type StatsModuleReasonGroup = KnownStatsModuleReasonGroup &
33474+
Record<string, StatsJsonValue>;
3342933475
type StatsModuleTraceDependency = KnownStatsModuleTraceDependency &
3343033476
Record<string, StatsJsonValue>;
3343133477
type StatsModuleTraceItem = KnownStatsModuleTraceItem &
@@ -36447,6 +36493,20 @@ declare namespace exports {
3644736493
moduleTraceDependency: [StatsModuleTraceDependency, Dependency];
3644836494
}
3644936495

36496+
/**
36497+
* The group object `groupResults` taps create for each array path of `StatsFactoryItemKeys` they group.
36498+
* A plugin types its own groups by declaration merging: `declare module "webpack" { interface StatsFactoryGroupKeys { "myPlugin.items": MyGroup } }`.
36499+
*/
36500+
export interface StatsFactoryGroupKeys {
36501+
"compilation.assets": StatsAssetGroup;
36502+
"asset.related": StatsAssetGroup;
36503+
"compilation.modules": StatsModuleGroup;
36504+
"chunk.modules": StatsModuleGroup;
36505+
"chunk.rootModules": StatsModuleGroup;
36506+
"module.modules": StatsModuleGroup;
36507+
"module.reasons": StatsModuleReasonGroup;
36508+
}
36509+
3645036510
/**
3645136511
* The `[item, created item]` of each array path, typing its item hooks (`filter`, `sort`, `filterSorted`) and created item hooks (`sortResults`, `groupResults`, `filterResults`, `merge`); an optional third element is what `merge` makes of the created items, which `result` then gets instead of their array, so a key declaring one must register a `merge` tap returning that type.
3645236512
* A plugin types its own paths by declaration merging: `declare module "webpack" { interface StatsFactoryItemKeys { "myPlugin.items": [MyItem, MyCreatedItem] } }`.
@@ -39851,6 +39911,7 @@ declare namespace exports {
3985139911
ColorsOptions,
3985239912
DefineConfigInput,
3985339913
StatsAsset,
39914+
StatsAssetGroup,
3985439915
StatsChunk,
3985539916
StatsChunkGroup,
3985639917
StatsChunkOrigin,
@@ -39859,8 +39920,10 @@ declare namespace exports {
3985939920
StatsLogging,
3986039921
StatsLoggingEntry,
3986139922
StatsModule,
39923+
StatsModuleGroup,
3986239924
StatsModuleIssuer,
3986339925
StatsModuleReason,
39926+
StatsModuleReasonGroup,
3986439927
StatsModuleTraceDependency,
3986539928
StatsModuleTraceItem,
3986639929
StatsProfile,

0 commit comments

Comments
 (0)