Skip to content

Commit d92633d

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 bf84a6f commit d92633d

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
@@ -18497,6 +18497,17 @@ declare interface KnownStatsAsset {
1849718497
isOverSizeLimit?: boolean;
1849818498
}
1849918499

18500+
/**
18501+
* A group of assets that `groupResults` creates, listing them in `children` or counting hidden ones in `filteredChildren`.
18502+
*/
18503+
declare interface KnownStatsAssetGroup {
18504+
type: string;
18505+
size: number;
18506+
name?: string;
18507+
children?: StatsAsset[];
18508+
filteredChildren?: number;
18509+
}
18510+
1850018511
/**
1850118512
* Defines the known stats chunk type used by this module.
1850218513
*/
@@ -18690,6 +18701,20 @@ declare interface KnownStatsModule {
1869018701
source?: string | Buffer;
1869118702
}
1869218703

18704+
/**
18705+
* A group of modules that `groupResults` creates, listing them in `children` or counting hidden ones in `filteredChildren`.
18706+
*/
18707+
declare interface KnownStatsModuleGroup {
18708+
type: string;
18709+
size: number;
18710+
sizes: Record<string, number>;
18711+
name?: string;
18712+
moduleType?: string;
18713+
layer?: string;
18714+
children?: StatsModule[];
18715+
filteredChildren?: number;
18716+
}
18717+
1869318718
/**
1869418719
* Defines the known stats module issuer type used by this module.
1869518720
*/
@@ -18718,6 +18743,16 @@ declare interface KnownStatsModuleReason {
1871818743
resolvedModuleId?: null | string | number;
1871918744
}
1872018745

18746+
/**
18747+
* A group of reasons from one origin module that `groupResults` creates.
18748+
*/
18749+
declare interface KnownStatsModuleReasonGroup {
18750+
type: string;
18751+
module: string;
18752+
active: boolean;
18753+
children: StatsModuleReason[];
18754+
}
18755+
1872118756
/**
1872218757
* Defines the known stats module trace dependency type used by this module.
1872318758
*/
@@ -32706,6 +32741,7 @@ declare class Stats {
3270632741
): string;
3270732742
}
3270832743
type StatsAsset = KnownStatsAsset & Record<string, StatsJsonValue>;
32744+
type StatsAssetGroup = KnownStatsAssetGroup & Record<string, StatsJsonValue>;
3270932745
type StatsChunk = KnownStatsChunk & Record<string, StatsJsonValue>;
3271032746
type StatsChunkGroup = KnownStatsChunkGroup & Record<string, StatsJsonValue>;
3271132747
type StatsChunkOrigin = KnownStatsChunkOrigin & Record<string, StatsJsonValue>;
@@ -32760,11 +32796,18 @@ type StatsFactoryGetItemNameHookMap = {
3276032796
>;
3276132797
for(key: string): SyncBailHook<[any, StatsFactoryContext], string | void>;
3276232798
} & HookMap<SyncBailHook<[any, StatsFactoryContext], string | void>>;
32799+
type StatsFactoryGroup<K extends keyof exports.StatsFactoryItemKeys> =
32800+
K extends keyof exports.StatsFactoryGroupKeys
32801+
? exports.StatsFactoryGroupKeys[K]
32802+
: object;
3276332803
type StatsFactoryGroupResultsHookMap = {
32764-
for<K extends keyof exports.StatsFactoryItemKeys, G = object>(
32804+
for<K extends keyof exports.StatsFactoryItemKeys>(
3276532805
key: K
3276632806
): SyncBailHook<
32767-
[GroupConfig<exports.StatsFactoryItemKeys[K][1], G>[], StatsFactoryContext],
32807+
[
32808+
GroupConfig<exports.StatsFactoryItemKeys[K][1], StatsFactoryGroup<K>>[],
32809+
StatsFactoryContext
32810+
],
3276832811
void
3276932812
>;
3277032813
for(
@@ -32877,10 +32920,13 @@ type StatsLogging = KnownStatsLogging & Record<string, StatsJsonValue>;
3287732920
type StatsLoggingEntry = KnownStatsLoggingEntry &
3287832921
Record<string, StatsJsonValue>;
3287932922
type StatsModule = KnownStatsModule & Record<string, StatsJsonValue>;
32923+
type StatsModuleGroup = KnownStatsModuleGroup & Record<string, StatsJsonValue>;
3288032924
type StatsModuleIssuer = KnownStatsModuleIssuer &
3288132925
Record<string, StatsJsonValue>;
3288232926
type StatsModuleReason = KnownStatsModuleReason &
3288332927
Record<string, StatsJsonValue>;
32928+
type StatsModuleReasonGroup = KnownStatsModuleReasonGroup &
32929+
Record<string, StatsJsonValue>;
3288432930
type StatsModuleTraceDependency = KnownStatsModuleTraceDependency &
3288532931
Record<string, StatsJsonValue>;
3288632932
type StatsModuleTraceItem = KnownStatsModuleTraceItem &
@@ -35836,6 +35882,20 @@ declare namespace exports {
3583635882
moduleTraceDependency: [StatsModuleTraceDependency, Dependency];
3583735883
}
3583835884

35885+
/**
35886+
* The group object `groupResults` taps create for each array path of `StatsFactoryItemKeys` they group.
35887+
* A plugin types its own groups by declaration merging: `declare module "webpack" { interface StatsFactoryGroupKeys { "myPlugin.items": MyGroup } }`.
35888+
*/
35889+
export interface StatsFactoryGroupKeys {
35890+
"compilation.assets": StatsAssetGroup;
35891+
"asset.related": StatsAssetGroup;
35892+
"compilation.modules": StatsModuleGroup;
35893+
"chunk.modules": StatsModuleGroup;
35894+
"chunk.rootModules": StatsModuleGroup;
35895+
"module.modules": StatsModuleGroup;
35896+
"module.reasons": StatsModuleReasonGroup;
35897+
}
35898+
3583935899
/**
3584035900
* 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.
3584135901
* A plugin types its own paths by declaration merging: `declare module "webpack" { interface StatsFactoryItemKeys { "myPlugin.items": [MyItem, MyCreatedItem] } }`.
@@ -38741,6 +38801,7 @@ declare namespace exports {
3874138801
ColorsOptions,
3874238802
DefineConfigInput,
3874338803
StatsAsset,
38804+
StatsAssetGroup,
3874438805
StatsChunk,
3874538806
StatsChunkGroup,
3874638807
StatsChunkOrigin,
@@ -38749,8 +38810,10 @@ declare namespace exports {
3874938810
StatsLogging,
3875038811
StatsLoggingEntry,
3875138812
StatsModule,
38813+
StatsModuleGroup,
3875238814
StatsModuleIssuer,
3875338815
StatsModuleReason,
38816+
StatsModuleReasonGroup,
3875438817
StatsModuleTraceDependency,
3875538818
StatsModuleTraceItem,
3875638819
StatsProfile,

0 commit comments

Comments
 (0)