Add types on module files

This commit is contained in:
Florent Cailhol 2018-11-07 14:03:25 +01:00
parent 4178b18196
commit be8c6bbe71
12 changed files with 127 additions and 32 deletions

View File

@ -35,6 +35,20 @@ const makeUnserializable = require("./util/makeUnserializable");
/** @typedef {"sync" | "eager" | "weak" | "async-weak" | "lazy" | "lazy-once"} ContextMode Context mode */
/**
* @typedef {Object} ContextModuleOptions
* @property {ContextMode} mode
* @property {string} resource
* @property {boolean} recursive
* @property {RegExp} regExp
* @property {string=} addon
* @property {string=} chunkName
* @property {RegExp=} include
* @property {RegExp=} exclude
* @property {TODO} groupOptions
* @property {TODO} resolveOptions
*/
/**
* @callback ResolveDependenciesCallback
* @param {Error=} err
@ -49,13 +63,9 @@ const makeUnserializable = require("./util/makeUnserializable");
*/
class ContextModule extends Module {
// type ContextMode = "sync" | "eager" | "weak" | "async-weak" | "lazy" | "lazy-once"
// type ContextOptions = { resource: string, recursive: boolean, regExp: RegExp, addon?: string, mode?: ContextMode, chunkName?: string, include?: RegExp, exclude?: RegExp, groupOptions?: Object }
// resolveDependencies: (fs: FS, options: ContextOptions, (err: Error?, dependencies: Dependency[]) => void) => void
// options: ContextOptions
/**
* @param {ResolveDependencies} resolveDependencies function to get dependencies in this context
* @param {TODO} options options object
* @param {ContextModuleOptions} options options object
*/
constructor(resolveDependencies, options) {
let resource;

View File

@ -74,8 +74,13 @@ const getJoinedString = set => {
/** @typedef {(requestShortener: RequestShortener) => string} OptimizationBailoutFunction */
class Module extends DependenciesBlock {
/**
* @param {string} type the module type
* @param {string=} context an optional context
*/
constructor(type, context = null) {
super();
/** @type {string} */
this.type = type;
/** @type {string} */

View File

@ -10,6 +10,10 @@ const WebpackError = require("./WebpackError");
const makeSerializable = require("./util/makeSerializable");
class ModuleBuildError extends WebpackError {
/**
* @param {string | Error&any} err error thrown
* @param {TODO} info additional info
*/
constructor(err, { from = null } = {}) {
let message = "Module build failed";
let details = undefined;
@ -38,10 +42,10 @@ class ModuleBuildError extends WebpackError {
} else if (typeof err.message === "string" && err.message) {
message += err.message;
} else {
message += err;
message += String(err);
}
} else {
message = err;
message += String(err);
}
super(message);

View File

@ -7,7 +7,14 @@
const WebpackError = require("./WebpackError");
/** @typedef {import("./Module")} Module */
module.exports = class ModuleDependencyWarning extends WebpackError {
/**
* @param {Module} module module tied to dependency
* @param {Error} err error thrown
* @param {TODO} loc location of dependency
*/
constructor(module, err, loc) {
super(err.message);

View File

@ -10,6 +10,10 @@ const WebpackError = require("./WebpackError");
const makeSerializable = require("./util/makeSerializable");
class ModuleError extends WebpackError {
/**
* @param {Error} err error thrown
* @param {TODO} info additional info
*/
constructor(err, { from = null } = {}) {
let message = "Module Error";

View File

@ -21,10 +21,17 @@ class ModuleGraphConnection {
this.dependency = dependency;
this.resolvedModule = module;
this.module = module;
/** @type {Set<string>} */
this.explanations = new Set();
if (explanation) this.explanations.add(explanation);
if (explanation) {
this.explanations.add(explanation);
}
}
/**
* @param {string} explanation the explanation to add
* @returns {void}
*/
addExplanation(explanation) {
this.explanations.add(explanation);
}

View File

@ -7,7 +7,14 @@
const WebpackError = require("./WebpackError");
/** @typedef {import("./Module")} Module */
class ModuleNotFoundError extends WebpackError {
/**
* @param {Module} module module tied to dependency
* @param {Error&any} err error thrown
* @param {TODO} loc location of dependency
*/
constructor(module, err, loc) {
super("Module not found: " + err);

View File

@ -7,7 +7,13 @@
const WebpackError = require("./WebpackError");
/** @typedef {import("./Module")} Module */
class ModuleRestoreError extends WebpackError {
/**
* @param {Module} module module tied to dependency
* @param {string | Error} err error thrown
*/
constructor(module, err) {
let message = "Module restore failed: ";
let details = undefined;
@ -21,7 +27,7 @@ class ModuleRestoreError extends WebpackError {
message += err;
}
} else {
message = err;
message = String(err);
}
super(message);

View File

@ -26,6 +26,10 @@ const { SyncWaterfallHook, SyncHook } = require("tapable");
*/
module.exports = class ModuleTemplate {
/**
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {string} type the module template type
*/
constructor(runtimeTemplate, type) {
this.runtimeTemplate = runtimeTemplate;
this.type = type;

View File

@ -10,6 +10,10 @@ const WebpackError = require("./WebpackError");
const makeSerializable = require("./util/makeSerializable");
class ModuleWarning extends WebpackError {
/**
* @param {Error} warning error thrown
* @param {TODO} info additional info
*/
constructor(warning, { from = null } = {}) {
let message = "Module Warning";
@ -22,7 +26,7 @@ class ModuleWarning extends WebpackError {
if (warning && typeof warning === "object" && warning.message) {
message += warning.message;
} else if (warning) {
message += warning;
message += String(warning);
}
super(message);

View File

@ -43,18 +43,26 @@ const makeSerializable = require("./util/makeSerializable");
const EARLY_RETURN_ERROR = new Error("flags early return is not an error");
const asString = buf => {
if (Buffer.isBuffer(buf)) {
return buf.toString("utf-8");
/**
* @param {string | Buffer} input the input
* @returns {string} the converted string
*/
const asString = input => {
if (Buffer.isBuffer(input)) {
return input.toString("utf-8");
}
return buf;
return input;
};
const asBuffer = str => {
if (!Buffer.isBuffer(str)) {
return Buffer.from(str, "utf-8");
/**
* @param {string | Buffer} input the input
* @returns {Buffer} the converted buffer
*/
const asBuffer = input => {
if (!Buffer.isBuffer(input)) {
return Buffer.from(input, "utf-8");
}
return str;
return input;
};
class NonErrorEmittedError extends WebpackError {
@ -96,21 +104,31 @@ class NormalModule extends Module {
super(type, getContext(resource));
// Info from Factory
/** @type {string} */
this.request = request;
/** @type {string} */
this.userRequest = userRequest;
/** @type {string} */
this.rawRequest = rawRequest;
/** @type {boolean} */
this.binary = type.startsWith("webassembly");
this.parser = parser;
this.generator = generator;
this.resource = resource;
this.matchResource = matchResource;
this.loaders = loaders;
if (resolveOptions !== undefined) this.resolveOptions = resolveOptions;
if (resolveOptions !== undefined) {
this.resolveOptions = resolveOptions;
}
// Info from Build
/** @type {WebpackError=} */
this.error = null;
/** @private @type {Source=} */
this._source = null;
/** @private @type {string} */
this._buildHash = "";
/** @type {number=} */
this.buildTimestamp = undefined;
/** @private @type {Map<string, CachedSourceEntry>} */
this._cachedSources = new Map();
@ -178,6 +196,12 @@ class NormalModule extends Module {
this.loaders = m.loaders;
}
/**
* @param {string} name the asset name
* @param {string} content the content
* @param {string | TODO} sourceMap an optional source map
* @returns {Source} the created source
*/
createSourceForAsset(name, content, sourceMap) {
if (!sourceMap) {
return new RawSource(content);
@ -259,10 +283,22 @@ class NormalModule extends Module {
return null;
}
createSource(source, resourceBuffer, sourceMap) {
/**
* @param {string | Buffer} content the content
* @param {string | Buffer} resourceBuffer TODO
* @param {string | TODO} sourceMap an optional source map
* @returns {Source} the created source
*/
createSource(content, resourceBuffer, sourceMap) {
if (Buffer.isBuffer(content)) {
// @ts-ignore
// TODO We need to fix @types/webpack-sources to allow RawSource to take a Buffer | string
return new RawSource(content);
}
// if there is no identifier return raw source
if (!this.identifier) {
return new RawSource(source);
return new RawSource(content);
}
// from here on we assume we have an identifier
@ -270,23 +306,17 @@ class NormalModule extends Module {
if (this.lineToLine && resourceBuffer) {
return new LineToLineMappedSource(
source,
content,
identifier,
asString(resourceBuffer)
);
}
if (this.useSourceMap && sourceMap) {
return new SourceMapSource(source, identifier, sourceMap);
return new SourceMapSource(content, identifier, sourceMap);
}
if (Buffer.isBuffer(source)) {
// @ts-ignore
// TODO We need to fix @types/webpack-sources to allow RawSource to take a Buffer | string
return new RawSource(source);
}
return new OriginalSource(source, identifier);
return new OriginalSource(content, identifier);
}
doBuild(options, compilation, resolver, fs, callback) {
@ -364,12 +394,16 @@ class NormalModule extends Module {
);
}
/**
* @param {WebpackError} error the error
* @returns {void}
*/
markModuleAsErrored(error) {
// Restore build meta from successful build to keep importing state
this.buildMeta = Object.assign({}, this._lastSuccessfulBuildMeta);
this.error = error;
this.errors.push(this.error);
this.errors.push(error);
this._source = new RawSource(
"throw new Error(" + JSON.stringify(this.error.message) + ");"
);
@ -421,7 +455,7 @@ class NormalModule extends Module {
const hash = createHash(compilation.outputOptions.hashFunction);
if (this._source) {
hash.update("source");
this._source.updateHash(hash);
this._source.updateHash(/** @type {TODO} */ (hash));
}
hash.update("meta");
hash.update(JSON.stringify(this.buildMeta));

View File

@ -23,8 +23,11 @@ const makeSerializable = require("./util/makeSerializable");
class RawModule extends Module {
constructor(source, identifier, readableIdentifier) {
super("javascript/dynamic", null);
/** @type {string} */
this.sourceStr = source;
/** @type {string} */
this.identifierStr = identifier || this.sourceStr;
/** @type {string} */
this.readableIdentifierStr = readableIdentifier || this.identifierStr;
}