Skip to content

Commit 6bfa221

Browse files
refactor(types): let plugins type stats fields and code generation data
The stats output types, normalized stats options and code generation schemas become augmentable, and CodeGenerationResults.getData returns the type its key declares.
1 parent 281d29c commit 6bfa221

6 files changed

Lines changed: 511 additions & 495 deletions

File tree

‎.changeset/210-plugin-declared-types.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Let plugins declare typed build info, build meta, asset info and parser tag data.
5+
Let plugins type their build info, stats fields, code generation data and tags.

‎lib/Compilation.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ const { toError } = ErrorHelpers;
503503

504504
/**
505505
* Defines the known normalized stats options type used by this module.
506+
* A plugin types its own stats options by declaration merging: `declare module "webpack" { interface KnownNormalizedStatsOptions { myField?: MyType } }`.
507+
* @augmentable
506508
* @typedef {object} KnownNormalizedStatsOptions
507509
* @property {string} context
508510
* @property {RequestShortener} requestShortener

‎lib/module/CodeGenerationResults.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const { RuntimeSpecMap, runtimeToString } = require("../util/runtime");
1616
* @import Module, {
1717
* SourceType,
1818
* CodeGenerationResult,
19-
* CodeGenerationResultData,
19+
* CodeGenValue,
2020
* ReadOnlyRuntimeRequirements
2121
* } from "./Module"
2222
*/
@@ -135,10 +135,11 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
135135

136136
/**
137137
* Returns an arbitrary metadata entry recorded during code generation.
138+
* @template {string} K
138139
* @param {Module} module the module
139140
* @param {RuntimeSpec} runtime runtime(s)
140-
* @param {string} key data key
141-
* @returns {ReturnType<CodeGenerationResultData["get"]>} data generated by code generation
141+
* @param {K} key data key
142+
* @returns {CodeGenValue<K> | undefined} data generated by code generation
142143
*/
143144
getData(module, runtime, key) {
144145
const data = this.get(module, runtime).data;

‎lib/module/Module.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ const {
133133

134134
/**
135135
* Defines the all code generation schemas type used by this module.
136+
* A plugin types its own code generation data by declaration merging: `declare module "webpack" { interface AllCodeGenerationSchemas { myField?: MyType } }`.
137+
* @augmentable
136138
* @typedef {object} AllCodeGenerationSchemas
137139
* @property {Set<string>} topLevelDeclarations top level declarations for javascript modules
138140
* @property {Set<string>} freeNames free identifier names in the rendered source for javascript modules

‎lib/stats/DefaultStatsFactoryPlugin.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
9393
/** @typedef {KnownStatsCompilation & Record<string, StatsJsonValue>} StatsCompilation */
9494
/**
9595
* Defines the known stats compilation type used by this module.
96+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsCompilation { myField?: MyType } }`.
97+
* @augmentable
9698
* @typedef {object} KnownStatsCompilation
9799
* @property {StatsJsonValue=} env
98100
* @property {string=} name
@@ -126,6 +128,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
126128
/** @typedef {KnownStatsLogging & Record<string, StatsJsonValue>} StatsLogging */
127129
/**
128130
* Defines the known stats logging type used by this module.
131+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsLogging { myField?: MyType } }`.
132+
* @augmentable
129133
* @typedef {object} KnownStatsLogging
130134
* @property {StatsLoggingEntry[]} entries
131135
* @property {number} filteredEntries
@@ -135,6 +139,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
135139
/** @typedef {KnownStatsLoggingEntry & Record<string, StatsJsonValue>} StatsLoggingEntry */
136140
/**
137141
* Defines the known stats logging entry type used by this module.
142+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsLoggingEntry { myField?: MyType } }`.
143+
* @augmentable
138144
* @typedef {object} KnownStatsLoggingEntry
139145
* @property {string} type
140146
* @property {string=} message
@@ -148,6 +154,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
148154
/** @typedef {string[]} ChunkIdHints */
149155
/**
150156
* Defines the known stats asset type used by this module.
157+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsAsset { myField?: MyType } }`.
158+
* @augmentable
151159
* @typedef {object} KnownStatsAsset
152160
* @property {string} type
153161
* @property {string} name
@@ -171,6 +179,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
171179

172180
/**
173181
* A group of assets that `groupResults` creates, listing them in `children` or counting hidden ones in `filteredChildren`.
182+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsAssetGroup { myField?: MyType } }`.
183+
* @augmentable
174184
* @typedef {object} KnownStatsAssetGroup
175185
* @property {string} type
176186
* @property {number} size
@@ -182,6 +192,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
182192
/** @typedef {KnownStatsChunkGroup & Record<string, StatsJsonValue>} StatsChunkGroup */
183193
/**
184194
* Defines the known stats chunk group type used by this module.
195+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsChunkGroup { myField?: MyType } }`.
196+
* @augmentable
185197
* @typedef {object} KnownStatsChunkGroup
186198
* @property {ChunkName=} name
187199
* @property {ChunkId[]=} chunks
@@ -200,6 +212,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
200212
/** @typedef {KnownStatsModule & Record<string, StatsJsonValue>} StatsModule */
201213
/**
202214
* Defines the known stats module type used by this module.
215+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsModule { myField?: MyType } }`.
216+
* @augmentable
203217
* @typedef {object} KnownStatsModule
204218
* @property {string=} type
205219
* @property {string=} moduleType
@@ -246,6 +260,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
246260
/** @typedef {KnownStatsProfile & Record<string, StatsJsonValue>} StatsProfile */
247261
/**
248262
* Defines the known stats profile type used by this module.
263+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsProfile { myField?: MyType } }`.
264+
* @augmentable
249265
* @typedef {object} KnownStatsProfile
250266
* @property {number} total
251267
* @property {number} resolving
@@ -262,6 +278,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
262278
/** @typedef {KnownStatsModuleIssuer & Record<string, StatsJsonValue>} StatsModuleIssuer */
263279
/**
264280
* Defines the known stats module issuer type used by this module.
281+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsModuleIssuer { myField?: MyType } }`.
282+
* @augmentable
265283
* @typedef {object} KnownStatsModuleIssuer
266284
* @property {string} identifier
267285
* @property {string} name
@@ -273,6 +291,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
273291

274292
/**
275293
* A group of modules that `groupResults` creates, listing them in `children` or counting hidden ones in `filteredChildren`.
294+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsModuleGroup { myField?: MyType } }`.
295+
* @augmentable
276296
* @typedef {object} KnownStatsModuleGroup
277297
* @property {string} type
278298
* @property {number} size
@@ -288,6 +308,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
288308

289309
/**
290310
* A group of reasons from one origin module that `groupResults` creates.
311+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsModuleReasonGroup { myField?: MyType } }`.
312+
* @augmentable
291313
* @typedef {object} KnownStatsModuleReasonGroup
292314
* @property {string} type
293315
* @property {string} module
@@ -298,6 +320,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
298320
/** @typedef {KnownStatsModuleReason & Record<string, StatsJsonValue>} StatsModuleReason */
299321
/**
300322
* Defines the known stats module reason type used by this module.
323+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsModuleReason { myField?: MyType } }`.
324+
* @augmentable
301325
* @typedef {object} KnownStatsModuleReason
302326
* @property {string | null} moduleIdentifier
303327
* @property {string | null} module
@@ -316,6 +340,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
316340
/** @typedef {KnownStatsChunk & Record<string, StatsJsonValue>} StatsChunk */
317341
/**
318342
* Defines the known stats chunk type used by this module.
343+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsChunk { myField?: MyType } }`.
344+
* @augmentable
319345
* @typedef {object} KnownStatsChunk
320346
* @property {boolean} rendered
321347
* @property {boolean} initial
@@ -343,6 +369,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
343369
/** @typedef {KnownStatsChunkOrigin & Record<string, StatsJsonValue>} StatsChunkOrigin */
344370
/**
345371
* Defines the known stats chunk origin type used by this module.
372+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsChunkOrigin { myField?: MyType } }`.
373+
* @augmentable
346374
* @typedef {object} KnownStatsChunkOrigin
347375
* @property {string} module
348376
* @property {string} moduleIdentifier
@@ -355,6 +383,8 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
355383
/** @typedef {KnownStatsModuleTraceItem & Record<string, StatsJsonValue>} StatsModuleTraceItem */
356384
/**
357385
* Defines the known stats module trace item type used by this module.
386+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsModuleTraceItem { myField?: MyType } }`.
387+
* @augmentable
358388
* @typedef {object} KnownStatsModuleTraceItem
359389
* @property {string=} originIdentifier
360390
* @property {string=} originName
@@ -368,13 +398,17 @@ const { makePathsRelative, parseResource } = require("../util/identifier");
368398
/** @typedef {KnownStatsModuleTraceDependency & Record<string, StatsJsonValue>} StatsModuleTraceDependency */
369399
/**
370400
* Defines the known stats module trace dependency type used by this module.
401+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsModuleTraceDependency { myField?: MyType } }`.
402+
* @augmentable
371403
* @typedef {object} KnownStatsModuleTraceDependency
372404
* @property {string=} loc
373405
*/
374406

375407
/** @typedef {KnownStatsError & Record<string, StatsJsonValue>} StatsError */
376408
/**
377409
* Defines the known stats error type used by this module.
410+
* A plugin types its own stats fields by declaration merging: `declare module "webpack" { interface KnownStatsError { myField?: MyType } }`.
411+
* @augmentable
378412
* @typedef {object} KnownStatsError
379413
* @property {string} message
380414
* @property {string=} chunkName

0 commit comments

Comments
 (0)