Skip to content

Commit 10282ea

Browse files
authored
Merge pull request webpack#6862 from mohsen1/ts
Add TypeScript type checking
2 parents 40a089c + a3df74b commit 10282ea

17 files changed

+234
-24
lines changed

declarations.d.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
declare module "*.json";
2+
declare module "webpack-cli";
3+
4+
// Deprecated NodeJS API usages in Webpack
5+
declare namespace NodeJS {
6+
interface Process {
7+
binding(internalModule: string): any;
8+
}
9+
}
10+
11+
// There are no typings for chrome-trace-event
12+
declare module 'chrome-trace-event' {
13+
interface Event {
14+
name: string
15+
id?: number
16+
cat: string[]
17+
args?: Object
18+
}
19+
20+
export class Tracer {
21+
constructor(options: {
22+
noStream: boolean
23+
})
24+
pipe(stream: NodeJS.WritableStream) : void
25+
instantEvent(event: Event) : void
26+
counter: number
27+
trace: {
28+
begin(event: Event) : void
29+
end(event: Event) : void
30+
}
31+
}
32+
}
33+
34+
/**
35+
* Global variable declarations
36+
* @todo Once this issue is resolved, remove these globals and add JSDoc onsite instead
37+
* https://github.com/Microsoft/TypeScript/issues/15626
38+
*/
39+
declare const $hash$;
40+
declare const $requestTimeout$;
41+
declare const installedModules;
42+
declare const $require$;
43+
declare const hotDownloadManifest;
44+
declare const hotDownloadUpdateChunk;
45+
declare const hotDisposeChunk;
46+
declare const modules;
47+
declare const installedChunks;
48+
declare const hotAddUpdateChunk;
49+
declare const parentHotUpdateCallback;
50+
declare const $hotChunkFilename$;
51+
declare const $hotMainFilename$;
52+
declare const WebAssembly;
53+
declare const importScripts;
54+
declare const $crossOriginLoading$;
55+
declare const chunkId;

lib/EvalSourceMapDevToolModuleTemplatePlugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class EvalSourceMapDevToolModuleTemplatePlugin {
4040
return source;
4141
}
4242

43+
/** @type {{ [key: string]: any; }} */
4344
let sourceMap;
4445
let content;
4546
if (source.sourceAndMap) {

lib/HotModuleReplacement.runtime.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ module.exports = function() {
362362
for (var id in hotUpdate) {
363363
if (Object.prototype.hasOwnProperty.call(hotUpdate, id)) {
364364
moduleId = toModuleId(id);
365+
/** @type {any} */
365366
var result;
366367
if (hotUpdate[id]) {
367368
result = getAffectedStuff(moduleId);
@@ -371,6 +372,7 @@ module.exports = function() {
371372
moduleId: id
372373
};
373374
}
375+
/** @type {Error|false} */
374376
var abortError = false;
375377
var doApply = false;
376378
var doDispose = false;

lib/HotUpdateChunk.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
const Chunk = require("./Chunk");
8+
9+
class HotUpdateChunk extends Chunk {
10+
constructor() {
11+
super();
12+
this.removedModules = undefined;
13+
}
14+
}
15+
16+
module.exports = HotUpdateChunk;

lib/HotUpdateChunkTemplate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"use strict";
66

77
const Template = require("./Template");
8-
const Chunk = require("./Chunk");
8+
const HotUpdateChunk = require("./HotUpdateChunk");
99
const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable");
1010

1111
module.exports = class HotUpdateChunkTemplate extends Tapable {
@@ -41,7 +41,7 @@ module.exports = class HotUpdateChunkTemplate extends Tapable {
4141
moduleTemplate,
4242
dependencyTemplates
4343
) {
44-
const hotUpdateChunk = new Chunk();
44+
const hotUpdateChunk = new HotUpdateChunk();
4545
hotUpdateChunk.id = id;
4646
hotUpdateChunk.setModules(modules);
4747
hotUpdateChunk.removedModules = removedModules;

lib/Module.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const ModuleReason = require("./ModuleReason");
1111
const SortableSet = require("./util/SortableSet");
1212
const Template = require("./Template");
1313

14+
/** @typedef {typeof import("./Chunk")} Chunk */
15+
16+
/** @typedef {typeof import("./RequestShortener")} RequestShortener */
17+
1418
const EMPTY_RESOLVE_OPTIONS = {};
1519

1620
let debugId = 1000;
@@ -23,51 +27,79 @@ const sortByDebugId = (a, b) => {
2327
return a.debugId - b.debugId;
2428
};
2529

30+
/** @typedef {(requestShortener: RequestShortener) => string} OptimizationBailoutFunction */
31+
2632
class Module extends DependenciesBlock {
2733
constructor(type, context = null) {
2834
super();
35+
/** @type {string} */
2936
this.type = type;
37+
/** @type {string} */
3038
this.context = context;
3139

3240
// Unique Id
41+
/** @type {number} */
3342
this.debugId = debugId++;
3443

3544
// Hash
45+
/** @type {string} */
3646
this.hash = undefined;
47+
/** @type {string} */
3748
this.renderedHash = undefined;
3849

3950
// Info from Factory
51+
/** @type {object} */
4052
this.resolveOptions = EMPTY_RESOLVE_OPTIONS;
53+
/** @type {object} */
4154
this.factoryMeta = {};
4255

4356
// Info from Build
57+
/** @type {Error[]} */
4458
this.warnings = [];
59+
/** @type {Error[]} */
4560
this.errors = [];
61+
/** @type {object} */
4662
this.buildMeta = undefined;
63+
/** @type {object} */
4764
this.buildInfo = undefined;
4865

4966
// Graph (per Compilation)
67+
/** @type {ModuleReason[]} */
5068
this.reasons = [];
69+
/** @type {SortableSet} */
5170
this._chunks = new SortableSet(undefined, sortById);
5271

5372
// Info from Compilation (per Compilation)
73+
/** @type {number | string} */
5474
this.id = null;
75+
/** @type {number} */
5576
this.index = null;
77+
/** @type {number} */
5678
this.index2 = null;
79+
/** @type {number} */
5780
this.depth = null;
81+
/** @type {Module} */
5882
this.issuer = null;
83+
/** @type {undefined | object} */
5984
this.profile = undefined;
85+
/** @type {boolean} */
6086
this.prefetched = false;
87+
/** @type {boolean} */
6188
this.built = false;
6289

6390
// Info from Optimization (per Compilation)
91+
/** @type {null | boolean} */
6492
this.used = null;
93+
/** @type {false | true | string[]} */
6594
this.usedExports = null;
95+
/** @type {(string | OptimizationBailoutFunction)[]} */
6696
this.optimizationBailout = [];
6797

6898
// delayed operations
99+
/** @type {undefined | {oldChunk: Chunk, newChunks: Chunk[]}[] } */
69100
this._rewriteChunkInReasons = undefined;
70101

102+
/** @type {boolean} */
71103
this.useSourceMap = false;
72104
}
73105

lib/NormalModule.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ class NormalModule extends Module {
154154
this.errors.push(new ModuleError(this, error));
155155
},
156156
exec: (code, filename) => {
157+
// @ts-ignore Argument of type 'this' is not assignable to parameter of type 'Module'.
157158
const module = new NativeModule(filename, this);
159+
// @ts-ignore _nodeModulePaths is deprecated and undocumented Node.js API
158160
module.paths = NativeModule._nodeModulePaths(this.context);
159161
module.filename = filename;
160162
module._compile(code, filename);

lib/RuntimeTemplate.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ module.exports = class RuntimeTemplate {
1212
this.requestShortener = requestShortener;
1313
}
1414

15+
/**
16+
* Add a comment
17+
* @param {object} options Information content of the comment
18+
* @param {string=} options.request request string used originally
19+
* @param {string=} options.chunkName name of the chunk referenced
20+
* @param {string=} options.chunkReason reason information of the chunk
21+
* @param {string=} options.message additional message
22+
* @param {string=} options.exportName name of the export
23+
* @return {string} comment
24+
*/
1525
comment({ request, chunkName, chunkReason, message, exportName }) {
1626
let content;
1727
if (this.outputOptions.pathinfo) {

lib/debug/ProfilingPlugin.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,15 @@ const makeInterceptorFor = (instance, tracer) => hookName => ({
320320
}
321321
});
322322

323+
/** @typedef {(...args: any[]) => void | Promise<any>} PluginFunction */
324+
323325
/**
324326
* @param {string} hookName Name of the hook to profile.
325-
* @param {{counter: number, trace: *, profiler: *}} tracer Instance of tracer.
326-
* @param {{name: string, type: string, fn: Function}} opts Options for the profiled fn.
327+
* @param {Tracer} tracer Instance of tracer.
328+
* @param {object} options Options for the profiled fn.
329+
* @param {string} options.name Plugin name
330+
* @param {string} options.type Plugin type (sync | async | promise)
331+
* @param {PluginFunction} options.fn Plugin function
327332
* @returns {*} Chainable hooked function.
328333
*/
329334
const makeNewProfiledTapFn = (hookName, tracer, { name, type, fn }) => {
@@ -339,7 +344,8 @@ const makeNewProfiledTapFn = (hookName, tracer, { name, type, fn }) => {
339344
id,
340345
cat: defaultCategory
341346
});
342-
return fn(...args).then(r => {
347+
const promise = /** @type {Promise<*>} */ (fn(...args));
348+
return promise.then(r => {
343349
tracer.trace.end({
344350
name,
345351
id,

lib/dependencies/ContextDependency.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class ContextDependency extends Dependency {
1414
super();
1515
this.options = options;
1616
this.userRequest = this.options.request;
17+
/** @type {false | string} */
1718
this.critical = false;
1819
this.hadGlobalOrStickyRegExp = false;
1920
if (this.options.regExp.global || this.options.regExp.sticky) {

0 commit comments

Comments
 (0)