Skip to content

Commit e06b485

Browse files
committed
Add compiler typings
1 parent 6913e3b commit e06b485

File tree

11 files changed

+315
-101
lines changed

11 files changed

+315
-101
lines changed

declarations/WebpackOptions.d.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -396,26 +396,7 @@ export interface WebpackOptions {
396396
/**
397397
* Options for the watcher
398398
*/
399-
watchOptions?: {
400-
/**
401-
* Delay the rebuilt after the first change. Value is a time in ms.
402-
*/
403-
aggregateTimeout?: number;
404-
/**
405-
* Ignore some files from watching
406-
*/
407-
ignored?: {
408-
[k: string]: any;
409-
};
410-
/**
411-
* Enable polling mode for watching
412-
*/
413-
poll?: boolean | number;
414-
/**
415-
* Stop watching when stdin stream has ended
416-
*/
417-
stdin?: boolean;
418-
};
399+
watchOptions?: WatchOptions;
419400
}
420401
/**
421402
* This interface was referenced by `WebpackOptions`'s JSON-Schema
@@ -1421,3 +1402,27 @@ export interface StatsOptions {
14211402
*/
14221403
warningsFilter?: FilterTypes;
14231404
}
1405+
/**
1406+
* This interface was referenced by `WebpackOptions`'s JSON-Schema
1407+
* via the `definition` "WatchOptions".
1408+
*/
1409+
export interface WatchOptions {
1410+
/**
1411+
* Delay the rebuilt after the first change. Value is a time in ms.
1412+
*/
1413+
aggregateTimeout?: number;
1414+
/**
1415+
* Ignore some files from watching
1416+
*/
1417+
ignored?: {
1418+
[k: string]: any;
1419+
};
1420+
/**
1421+
* Enable polling mode for watching
1422+
*/
1423+
poll?: boolean | number;
1424+
/**
1425+
* Stop watching when stdin stream has ended
1426+
*/
1427+
stdin?: boolean;
1428+
}

lib/Compilation.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,32 @@ const { arrayToSetDeprecation } = require("./util/deprecation");
6868
/** @typedef {import("./dependencies/EntryDependency")} EntryDependency */
6969
/** @typedef {import("./util/createHash").Hash} Hash */
7070

71-
// TODO use @callback
72-
/** @typedef {{[assetName: string]: Source}} CompilationAssets */
73-
/** @typedef {(err?: WebpackError|null, result?: Module) => void } ModuleCallback */
74-
/** @typedef {(err?: Error|null) => void} Callback */
75-
/** @typedef {(d: Dependency) => any} DepBlockVarDependenciesCallback */
71+
/**
72+
* @callback Callback
73+
* @param {WebpackError=} err
74+
* @returns {void}
75+
*/
76+
77+
/**
78+
* @callback ModuleCallback
79+
* @param {WebpackError=} err
80+
* @param {Module=} result
81+
* @returns {void}
82+
*/
83+
84+
/**
85+
* @callback DepBlockVarDependenciesCallback
86+
* @param {Dependency} dependency
87+
* @returns {any}
88+
*/
89+
90+
/**
91+
* @typedef {Object} Plugin
92+
* @property {() => void} apply
93+
*/
94+
7695
/** @typedef {new (...args: any[]) => Dependency} DepConstructor */
77-
/** @typedef {{apply: () => void}} Plugin */
96+
/** @typedef {Record<string, Source>} CompilationAssets */
7897

7998
/**
8099
* @typedef {Object} ModuleFactoryCreateDataContextInfo
@@ -536,12 +555,6 @@ class Compilation {
536555
return new Stats(this);
537556
}
538557

539-
/**
540-
* @typedef {Object} AddModuleResult
541-
* @property {Module} module the added or existing module
542-
* @property {boolean} issuer was this the first request for this module
543-
*/
544-
545558
/**
546559
* @param {Module} module module to be added that was created
547560
* @param {ModuleCallback} callback returns the module in the compilation,
@@ -631,7 +644,7 @@ class Compilation {
631644
*
632645
* @param {Module} module module to be built
633646
* @param {ModuleCallback} callback the callback
634-
* @returns {TODO} returns the callback function with results
647+
* @returns {void}
635648
*/
636649
_buildModule(module, callback) {
637650
const currentProfile = this.profile
@@ -1116,7 +1129,7 @@ class Compilation {
11161129
}
11171130

11181131
/**
1119-
* @param {Callback} callback signals when the seal method is finishes
1132+
* @param {Callback} callback signals when the call finishes
11201133
* @returns {void}
11211134
*/
11221135
seal(callback) {
@@ -2294,6 +2307,10 @@ class Compilation {
22942307
}
22952308
}
22962309

2310+
/**
2311+
* @param {Callback} callback signals when the call finishes
2312+
* @returns {void}
2313+
*/
22972314
createChunkAssets(callback) {
22982315
const outputOptions = this.outputOptions;
22992316
const cachedSourceMap = new WeakMap();

lib/Compiler.js

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ const Watching = require("./Watching");
2828
const { makePathsRelative } = require("./util/identifier");
2929

3030
/** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
31+
/** @typedef {import("../declarations/WebpackOptions").OutputOptions} OutputOptions */
32+
/** @typedef {import("../declarations/WebpackOptions").WatchOptions} WatchOptions */
3133
/** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
34+
/** @typedef {import("../declarations/WebpackOptions").WebpackPluginInstance} WebpackPluginInstance */
35+
/** @typedef {import("./Chunk")} Chunk */
3236
/** @typedef {import("./FileSystemInfo").FileSystemInfoEntry} FileSystemInfoEntry */
37+
/** @typedef {import("./Stats")} Stats */
3338

3439
/**
3540
* @typedef {Object} CompilationParams
@@ -38,7 +43,24 @@ const { makePathsRelative } = require("./util/identifier");
3843
* @property {Set<string>} compilationDependencies
3944
*/
4045

46+
/**
47+
* @template T
48+
* @callback Callback
49+
* @param {Error=} err
50+
* @param {T=} result
51+
*/
52+
53+
/**
54+
* @callback RunAsChildCallback
55+
* @param {Error=} err
56+
* @param {Chunk[]=} entries
57+
* @param {Compilation=} compilation
58+
*/
59+
4160
class Compiler {
61+
/**
62+
* @param {string} context the compilation path
63+
*/
4264
constructor(context) {
4365
this.hooks = Object.freeze({
4466
/** @type {SyncBailHook<Compilation>} */
@@ -142,16 +164,29 @@ class Compiler {
142164
this.watchMode = false;
143165
}
144166

167+
/**
168+
* @param {WatchOptions} watchOptions the watcher's options
169+
* @param {Callback<Stats>} handler signals when the call finishes
170+
* @returns {Watching} a compiler watcher
171+
*/
145172
watch(watchOptions, handler) {
146-
if (this.running) return handler(new ConcurrentCompilationError());
173+
if (this.running) {
174+
return handler(new ConcurrentCompilationError());
175+
}
147176

148177
this.running = true;
149178
this.watchMode = true;
150179
return new Watching(this, watchOptions, handler);
151180
}
152181

182+
/**
183+
* @param {Callback<Stats>} callback signals when the call finishes
184+
* @returns {void}
185+
*/
153186
run(callback) {
154-
if (this.running) return callback(new ConcurrentCompilationError());
187+
if (this.running) {
188+
return callback(new ConcurrentCompilationError());
189+
}
155190

156191
const finalCallback = (err, stats) => {
157192
this.cache.beginIdle();
@@ -231,6 +266,10 @@ class Compiler {
231266
});
232267
}
233268

269+
/**
270+
* @param {RunAsChildCallback} callback signals when the call finishes
271+
* @returns {void}
272+
*/
234273
runAsChild(callback) {
235274
this.compile((err, compilation) => {
236275
if (err) return callback(err);
@@ -257,6 +296,11 @@ class Compiler {
257296
}
258297
}
259298

299+
/**
300+
* @param {Compilation} compilation the compilation
301+
* @param {Callback<void>} callback signals when the assets are emitted
302+
* @returns {void}
303+
*/
260304
emitAssets(compilation, callback) {
261305
let outputPath;
262306

@@ -322,6 +366,10 @@ class Compiler {
322366
});
323367
}
324368

369+
/**
370+
* @param {Callback<void>} callback signals when the call finishes
371+
* @returns {void}
372+
*/
325373
emitRecords(callback) {
326374
if (!this.recordsOutputPath) return callback();
327375
const idx1 = this.recordsOutputPath.lastIndexOf("/");
@@ -350,6 +398,10 @@ class Compiler {
350398
});
351399
}
352400

401+
/**
402+
* @param {Callback<void>} callback signals when the call finishes
403+
* @returns {void}
404+
*/
353405
readRecords(callback) {
354406
if (!this.recordsInputPath) {
355407
this.records = {};
@@ -375,6 +427,14 @@ class Compiler {
375427
});
376428
}
377429

430+
/**
431+
* @param {Compilation} compilation the compilation
432+
* @param {string} compilerName the compiler's name
433+
* @param {number} compilerIndex the compiler's index
434+
* @param {OutputOptions} outputOptions the output options
435+
* @param {WebpackPluginInstance[]} plugins the plugins to apply
436+
* @returns {Compiler} a child compiler
437+
*/
378438
createChildCompiler(
379439
compilation,
380440
compilerName,
@@ -454,6 +514,10 @@ class Compiler {
454514
return new Compilation(this);
455515
}
456516

517+
/**
518+
* @param {CompilationParams} params the compilation parameters
519+
* @returns {Compilation} the created compilation
520+
*/
457521
newCompilation(params) {
458522
const compilation = this.createCompilation();
459523
compilation.name = this.name;
@@ -489,6 +553,10 @@ class Compiler {
489553
return params;
490554
}
491555

556+
/**
557+
* @param {Callback<Compilation>} callback signals when the compilation finishes
558+
* @returns {void}
559+
*/
492560
compile(callback) {
493561
const params = this.newCompilationParams();
494562
this.hooks.beforeCompile.callAsync(params, err => {
@@ -516,6 +584,10 @@ class Compiler {
516584
});
517585
}
518586

587+
/**
588+
* @param {Callback<void>} callback signals when the compiler closes
589+
* @returns {void}
590+
*/
519591
close(callback) {
520592
this.cache.shutdown(callback);
521593
}

0 commit comments

Comments
 (0)