Skip to content

Commit f798957

Browse files
fix: types and test
1 parent 9bc07c6 commit f798957

19 files changed

Lines changed: 369 additions & 155 deletions

declarations.d.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -408,25 +408,6 @@ declare module "webpack-sources" {
408408
}
409409
}
410410

411-
declare module "browserslist" {
412-
function browserslist(query: string): string[] | undefined;
413-
namespace browserslist {
414-
export function loadConfig(
415-
options:
416-
| {
417-
config: string;
418-
env?: string;
419-
}
420-
| {
421-
path: string;
422-
env?: string;
423-
}
424-
): string | undefined;
425-
export function findConfig(path: string): Record<string, string[]>;
426-
}
427-
export = browserslist;
428-
}
429-
430411
declare module "json-parse-even-better-errors" {
431412
function parseJson(
432413
text: string,
@@ -436,13 +417,6 @@ declare module "json-parse-even-better-errors" {
436417
export = parseJson;
437418
}
438419

439-
// TODO remove that when @types/estree is updated
440-
interface ImportAttributeNode {
441-
type: "ImportAttribute";
442-
key: import("estree").Identifier | import("estree").Literal;
443-
value: import("estree").Literal;
444-
}
445-
446420
type RecursiveArrayOrRecord<T> =
447421
| { [index: string]: RecursiveArrayOrRecord<T> }
448422
| Array<RecursiveArrayOrRecord<T>>

lib/Compilation.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const { isSourceEqual } = require("./util/source");
102102
/** @typedef {import("./Compiler").MemCache} MemCache */
103103
/** @typedef {import("./Compiler").WeakReferences} WeakReferences */
104104
/** @typedef {import("./Compiler").ModuleMemCachesItem} ModuleMemCachesItem */
105+
/** @typedef {import("./Compiler").Records} Records */
105106
/** @typedef {import("./DependenciesBlock")} DependenciesBlock */
106107
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
107108
/** @typedef {import("./Dependency").ReferencedExport} ReferencedExport */
@@ -391,8 +392,6 @@ const { isSourceEqual } = require("./util/source");
391392

392393
/** @typedef {Set<Module>} NotCodeGeneratedModules */
393394

394-
/** @typedef {Record<string, TODO>} Records */
395-
396395
/** @type {AssetInfo} */
397396
const EMPTY_ASSET_INFO = Object.freeze({});
398397

@@ -4089,7 +4088,7 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
40894088

40904089
/**
40914090
* @param {Module} module module relationship for removal
4092-
* @param {DependenciesBlockLike} block //TODO: good description
4091+
* @param {DependenciesBlockLike} block dependencies block
40934092
* @returns {void}
40944093
*/
40954094
removeReasonsOfDependencyBlock(module, block) {

lib/Compiler.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,19 @@ const { isSourceEqual } = require("./util/source");
4242
/** @typedef {import("./Chunk")} Chunk */
4343
/** @typedef {import("./ChunkGraph").ModuleId} ModuleId */
4444
/** @typedef {import("./Dependency")} Dependency */
45+
/** @typedef {import("./HotModuleReplacementPlugin").ChunkHashes} ChunkHashes */
46+
/** @typedef {import("./HotModuleReplacementPlugin").ChunkModuleHashes} ChunkModuleHashes */
47+
/** @typedef {import("./HotModuleReplacementPlugin").ChunkModuleIds} ChunkModuleIds */
48+
/** @typedef {import("./HotModuleReplacementPlugin").ChunkRuntime} ChunkRuntime */
49+
/** @typedef {import("./HotModuleReplacementPlugin").FullHashChunkModuleHashes} FullHashChunkModuleHashes */
50+
/** @typedef {import("./HotModuleReplacementPlugin").HotIndex} HotIndex */
4551
/** @typedef {import("./Module")} Module */
4652
/** @typedef {import("./Module").BuildInfo} BuildInfo */
53+
/** @typedef {import("./RecordIdsPlugin").RecordsChunks} RecordsChunks */
54+
/** @typedef {import("./RecordIdsPlugin").RecordsModules} RecordsModules */
4755
/** @typedef {import("./config/target").PlatformTargetProperties} PlatformTargetProperties */
4856
/** @typedef {import("./logging/createConsoleLogger").LoggingFunction} LoggingFunction */
57+
/** @typedef {import("./optimize/AggressiveSplittingPlugin").SplitData} SplitData */
4958
/** @typedef {import("./util/fs").IStats} IStats */
5059
/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
5160
/** @typedef {import("./util/fs").IntermediateFileSystem} IntermediateFileSystem */
@@ -80,6 +89,22 @@ const { isSourceEqual } = require("./util/source");
8089
* @param {Compilation=} compilation
8190
*/
8291

92+
/**
93+
* @typedef {object} KnownRecords
94+
* @property {SplitData[]=} aggressiveSplits
95+
* @property {RecordsChunks=} chunks
96+
* @property {RecordsModules=} modules
97+
* @property {string=} hash
98+
* @property {HotIndex=} hotIndex
99+
* @property {FullHashChunkModuleHashes=} fullHashChunkModuleHashes
100+
* @property {ChunkModuleHashes=} chunkModuleHashes
101+
* @property {ChunkHashes=} chunkHashes
102+
* @property {ChunkRuntime=} chunkRuntime
103+
* @property {ChunkModuleIds=} chunkModuleIds
104+
*/
105+
106+
/** @typedef {KnownRecords & Record<string, KnownRecords[]> & Record<string, EXPECTED_ANY>} Records */
107+
83108
/**
84109
* @typedef {object} AssetEmittedInfo
85110
* @property {Buffer} content
@@ -238,11 +263,11 @@ class Compiler {
238263
/** @type {WatchFileSystem | null} */
239264
this.watchFileSystem = null;
240265

241-
/** @type {string|null} */
266+
/** @type {string | null} */
242267
this.recordsInputPath = null;
243-
/** @type {string|null} */
268+
/** @type {string | null} */
244269
this.recordsOutputPath = null;
245-
/** @type {Record<string, TODO>} */
270+
/** @type {Records} */
246271
this.records = {};
247272
/** @type {Set<string | RegExp>} */
248273
this.managedPaths = new Set();
@@ -1075,8 +1100,10 @@ ${other}`);
10751100
};
10761101

10771102
const recordsOutputPathDirectory = dirname(
1078-
/** @type {OutputFileSystem} */ (this.outputFileSystem),
1079-
/** @type {string} */ (this.recordsOutputPath)
1103+
/** @type {OutputFileSystem} */
1104+
(this.outputFileSystem),
1105+
/** @type {string} */
1106+
(this.recordsOutputPath)
10801107
);
10811108
if (!recordsOutputPathDirectory) {
10821109
return writeFile();
@@ -1201,7 +1228,9 @@ ${other}`);
12011228
this.records[relativeCompilerName] = [];
12021229
}
12031230
if (this.records[relativeCompilerName][compilerIndex]) {
1204-
childCompiler.records = this.records[relativeCompilerName][compilerIndex];
1231+
childCompiler.records =
1232+
/** @type {Records} */
1233+
(this.records[relativeCompilerName][compilerIndex]);
12051234
} else {
12061235
this.records[relativeCompilerName].push((childCompiler.records = {}));
12071236
}

lib/ContextReplacementPlugin.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ class ContextReplacementPlugin {
131131
result.resource = newContentResource;
132132
} else {
133133
result.resource = join(
134-
/** @type {InputFileSystem} */ (compiler.inputFileSystem),
134+
/** @type {InputFileSystem} */
135+
(compiler.inputFileSystem),
135136
result.resource,
136137
newContentResource
137138
);
@@ -159,7 +160,8 @@ class ContextReplacementPlugin {
159160
) {
160161
// When the function changed it to an relative path
161162
result.resource = join(
162-
/** @type {InputFileSystem} */ (compiler.inputFileSystem),
163+
/** @type {InputFileSystem} */
164+
(compiler.inputFileSystem),
163165
origResource,
164166
result.resource
165167
);

lib/CssModule.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const makeSerializable = require("./util/makeSerializable");
1818
/** @typedef {string | undefined} Supports */
1919
/** @typedef {string | undefined} Media */
2020
/** @typedef {[CssLayer, Supports, Media]} InheritanceItem */
21-
/** @typedef {Array<InheritanceItem>} Inheritance */
21+
/** @typedef {InheritanceItem[]} Inheritance */
2222

23-
/** @typedef {NormalModuleCreateData & { cssLayer: CssLayer, supports: Supports, media: Media, inheritance: Inheritance }} CSSModuleCreateData */
23+
/** @typedef {NormalModuleCreateData & { cssLayer: CssLayer, supports: Supports, media: Media, inheritance?: Inheritance }} CSSModuleCreateData */
2424

2525
class CssModule extends NormalModule {
2626
/**

lib/HotModuleReplacementPlugin.js

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ const {
6666
* @property {SyncBailHook<[CallExpression, string[]], void>} hotAcceptWithoutCallback
6767
*/
6868

69+
/** @typedef {number} HotIndex */
70+
/** @typedef {Record<string, string>} FullHashChunkModuleHashes */
71+
/** @typedef {Record<string, string>} ChunkModuleHashes */
72+
/** @typedef {Record<ChunkId, string>} ChunkHashes */
73+
/** @typedef {Record<ChunkId, string>} ChunkRuntime */
74+
/** @typedef {Record<ChunkId, ModuleId[]>} ChunkModuleIds */
75+
6976
/** @typedef {{ updatedChunkIds: Set<ChunkId>, removedChunkIds: Set<ChunkId>, removedModules: Set<Module>, filename: string, assetInfo: AssetInfo }} HotUpdateMainContentByRuntimeItem */
7077
/** @typedef {Map<string, HotUpdateMainContentByRuntimeItem>} HotUpdateMainContentByRuntime */
7178

@@ -344,10 +351,11 @@ class HotModuleReplacementPlugin {
344351
);
345352
// #endregion
346353

354+
/** @type {HotIndex} */
347355
let hotIndex = 0;
348-
/** @type {Record<string, string>} */
356+
/** @type {FullHashChunkModuleHashes} */
349357
const fullHashChunkModuleHashes = {};
350-
/** @type {Record<string, string>} */
358+
/** @type {ChunkModuleHashes} */
351359
const chunkModuleHashes = {};
352360

353361
compilation.hooks.record.tap(PLUGIN_NAME, (compilation, records) => {
@@ -361,19 +369,20 @@ class HotModuleReplacementPlugin {
361369
records.chunkRuntime = {};
362370
for (const chunk of compilation.chunks) {
363371
const chunkId = /** @type {ChunkId} */ (chunk.id);
364-
records.chunkHashes[chunkId] = chunk.hash;
372+
records.chunkHashes[chunkId] = /** @type {string} */ (chunk.hash);
365373
records.chunkRuntime[chunkId] = getRuntimeKey(chunk.runtime);
366374
}
367375
records.chunkModuleIds = {};
368376
for (const chunk of compilation.chunks) {
369-
records.chunkModuleIds[/** @type {ChunkId} */ (chunk.id)] =
370-
Array.from(
371-
chunkGraph.getOrderedChunkModulesIterable(
372-
chunk,
373-
compareModulesById(chunkGraph)
374-
),
375-
m => chunkGraph.getModuleId(m)
376-
);
377+
const chunkId = /** @type {ChunkId} */ (chunk.id);
378+
379+
records.chunkModuleIds[chunkId] = Array.from(
380+
chunkGraph.getOrderedChunkModulesIterable(
381+
chunk,
382+
compareModulesById(chunkGraph)
383+
),
384+
m => /** @type {ModuleId} */ (chunkGraph.getModuleId(m))
385+
);
377386
}
378387
});
379388
/** @type {TupleSet<Module, Chunk>} */
@@ -418,10 +427,14 @@ class HotModuleReplacementPlugin {
418427
const hash = getModuleHash(module);
419428
if (
420429
fullHashModulesInThisChunk.has(
421-
/** @type {RuntimeModule} */ (module)
430+
/** @type {RuntimeModule} */
431+
(module)
422432
)
423433
) {
424-
if (records.fullHashChunkModuleHashes[key] !== hash) {
434+
if (
435+
/** @type {FullHashChunkModuleHashes} */
436+
(records.fullHashChunkModuleHashes)[key] !== hash
437+
) {
425438
updatedModules.add(module, chunk);
426439
}
427440
fullHashChunkModuleHashes[key] = hash;
@@ -504,8 +517,11 @@ class HotModuleReplacementPlugin {
504517
/** @type {HotUpdateMainContentByRuntime} */
505518
const hotUpdateMainContentByRuntime = new Map();
506519
let allOldRuntime;
507-
for (const key of Object.keys(records.chunkRuntime)) {
508-
const runtime = keyToRuntime(records.chunkRuntime[key]);
520+
const chunkRuntime =
521+
/** @type {ChunkRuntime} */
522+
(records.chunkRuntime);
523+
for (const key of Object.keys(chunkRuntime)) {
524+
const runtime = keyToRuntime(chunkRuntime[key]);
509525
allOldRuntime = mergeRuntimeOwned(allOldRuntime, runtime);
510526
}
511527
forEachRuntime(allOldRuntime, runtime => {
@@ -532,7 +548,7 @@ class HotModuleReplacementPlugin {
532548
if (hotUpdateMainContentByRuntime.size === 0) return;
533549

534550
// Create a list of all active modules to verify which modules are removed completely
535-
/** @type {Map<number|string, Module>} */
551+
/** @type {Map<number | string, Module>} */
536552
const allModules = new Map();
537553
for (const module of compilation.modules) {
538554
const id =
@@ -546,7 +562,10 @@ class HotModuleReplacementPlugin {
546562
const completelyRemovedModules = new Set();
547563

548564
for (const key of Object.keys(records.chunkHashes)) {
549-
const oldRuntime = keyToRuntime(records.chunkRuntime[key]);
565+
const oldRuntime = keyToRuntime(
566+
/** @type {ChunkRuntime} */
567+
(records.chunkRuntime)[key]
568+
);
550569
/** @type {Module[]} */
551570
const remainingModules = [];
552571
// Check which modules are removed
@@ -706,8 +725,8 @@ class HotModuleReplacementPlugin {
706725
}
707726
const renderManifest = compilation.getRenderManifest({
708727
chunk: hotUpdateChunk,
709-
hash: records.hash,
710-
fullHash: records.hash,
728+
hash: /** @type {string} */ (records.hash),
729+
fullHash: /** @type {string} */ (records.hash),
711730
outputOptions: compilation.outputOptions,
712731
moduleTemplates: compilation.moduleTemplates,
713732
dependencyTemplates: compilation.dependencyTemplates,

lib/NormalModuleFactory.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,10 @@ class NormalModuleFactory extends ModuleFactory {
635635
settings[/** @type {keyof ModuleSettings} */ (r.type)] !== null
636636
) {
637637
const type = /** @type {keyof ModuleSettings} */ (r.type);
638-
/** @type {TODO} */
639-
(settings)[type] = cachedCleverMerge(settings[type], r.value);
638+
settings[type] = cachedCleverMerge(settings[type], r.value);
640639
} else {
641640
const type = /** @type {keyof ModuleSettings} */ (r.type);
642-
/** @type {TODO} */
643-
(settings)[type] = r.value;
641+
settings[type] = r.value;
644642
}
645643
}
646644
}

lib/RecordIdsPlugin.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const identifierUtils = require("./util/identifier");
2222
/**
2323
* @typedef {object} RecordsModules
2424
* @property {Record<string, number>=} byIdentifier
25-
* @property {Record<string, number>=} bySource
2625
* @property {number[]=} usedIds
2726
*/
2827

lib/dependencies/AMDPlugin.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const UnsupportedDependency = require("./UnsupportedDependency");
4242

4343
const PLUGIN_NAME = "AMDPlugin";
4444

45-
/** @typedef {Record<string, TODO>} AmdOptions */
45+
/** @typedef {Exclude<Amd, false>} AmdOptions */
4646

4747
class AMDPlugin {
4848
/**
@@ -143,7 +143,7 @@ class AMDPlugin {
143143
/**
144144
* @param {string} optionExpr option expression
145145
* @param {string} rootName root name
146-
* @param {() => TODO} getMembers callback
146+
* @param {() => string[]} getMembers callback
147147
*/
148148
const tapOptionsHooks = (optionExpr, rootName, getMembers) => {
149149
parser.hooks.expression
@@ -156,10 +156,14 @@ class AMDPlugin {
156156
);
157157
parser.hooks.evaluateIdentifier
158158
.for(optionExpr)
159-
.tap(
160-
PLUGIN_NAME,
161-
evaluateToIdentifier(optionExpr, rootName, getMembers, true)
162-
);
159+
.tap(PLUGIN_NAME, function (expr) {
160+
return evaluateToIdentifier(
161+
optionExpr,
162+
rootName,
163+
getMembers,
164+
true
165+
)(expr);
166+
});
163167
parser.hooks.evaluateTypeof
164168
.for(optionExpr)
165169
.tap(PLUGIN_NAME, evaluateToString("object"));
@@ -176,7 +180,7 @@ class AMDPlugin {
176180
);
177181
new AMDDefineDependencyParserPlugin(parserOptions).apply(parser);
178182

179-
tapOptionsHooks("define.amd", "define", () => "amd");
183+
tapOptionsHooks("define.amd", "define", () => ["amd"]);
180184
tapOptionsHooks("require.amd", "require", () => ["amd"]);
181185
tapOptionsHooks(
182186
"__webpack_amd_options__",

0 commit comments

Comments
 (0)