diff --git a/lib/Dependency.js b/lib/Dependency.js index 2f0c8fe0905..6d27b1ed6d2 100644 --- a/lib/Dependency.js +++ b/lib/Dependency.js @@ -18,13 +18,13 @@ const DependencyReference = require("./dependencies/DependencyReference"); */ /** @typedef {Object} SourcePosition - * @property {number} column * @property {number} line + * @property {number=} column */ /** @typedef {Object} RealDependencyLocation * @property {SourcePosition} start - * @property {SourcePosition} end + * @property {SourcePosition=} end * @property {number=} index */ diff --git a/lib/SingleEntryPlugin.js b/lib/SingleEntryPlugin.js index e049abc2ac8..4a1703b1f85 100644 --- a/lib/SingleEntryPlugin.js +++ b/lib/SingleEntryPlugin.js @@ -48,9 +48,14 @@ class SingleEntryPlugin { ); } + /** + * @param {string} entry entry request + * @param {string} name entry name + * @returns {SingleEntryDependency} the dependency + */ static createDependency(entry, name) { const dep = new SingleEntryDependency(entry); - dep.loc = name; + dep.loc = { name }; return dep; } } diff --git a/lib/dependencies/LoaderPlugin.js b/lib/dependencies/LoaderPlugin.js index a3ab3f14104..c781d063605 100644 --- a/lib/dependencies/LoaderPlugin.js +++ b/lib/dependencies/LoaderPlugin.js @@ -7,6 +7,16 @@ const LoaderDependency = require("./LoaderDependency"); const NormalModule = require("../NormalModule"); +/** @typedef {import("../Module")} Module */ + +/** + * @callback LoadModuleCallback + * @param {Error=} err error object + * @param {string=} source source code + * @param {object=} map source map + * @param {Module=} module loaded module if successful + */ + class LoaderPlugin { apply(compiler) { compiler.hooks.compilation.tap( @@ -23,9 +33,16 @@ class LoaderPlugin { compilation.hooks.normalModuleLoader.tap( "LoaderPlugin", (loaderContext, module) => { + /** + * @param {string} request the request string to load the module from + * @param {LoadModuleCallback} callback callback returning the loaded module or error + * @returns {void} + */ loaderContext.loadModule = (request, callback) => { const dep = new LoaderDependency(request); - dep.loc = request; + dep.loc = { + name: request + }; const factory = compilation.dependencyFactories.get( dep.constructor ); diff --git a/lib/formatLocation.js b/lib/formatLocation.js index 8bd574d0102..f608cd496dc 100644 --- a/lib/formatLocation.js +++ b/lib/formatLocation.js @@ -5,57 +5,71 @@ "use strict"; +/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */ +/** @typedef {import("./Dependency").SourcePosition} SourcePosition */ + +// TODO webpack 5: pos must be SourcePosition +/** + * @param {SourcePosition|DependencyLocation|string} pos position + * @returns {string} formatted position + */ const formatPosition = pos => { if (pos === null) return ""; - const typeOfPos = typeof pos; - switch (typeOfPos) { - case "string": - return pos; - case "number": - return `${pos}`; - case "object": - if (typeof pos.line === "number" && typeof pos.column === "number") { - return `${pos.line}:${pos.column}`; - } else if (typeof pos.line === "number") { - return `${pos.line}:?`; - } else if (typeof pos.index === "number") { - return `+${pos.index}`; - } else { - return ""; - } - default: + // TODO webpack 5: Simplify this + if (typeof pos === "string") return pos; + if (typeof pos === "number") return `${pos}`; + if (typeof pos === "object") { + if ("line" in pos && "column" in pos) { + return `${pos.line}:${pos.column}`; + } else if ("line" in pos) { + return `${pos.line}:?`; + } else if ("index" in pos) { + // TODO webpack 5 remove this case + return `+${pos.index}`; + } else { return ""; + } } + return ""; }; +// TODO webpack 5: loc must be DependencyLocation +/** + * @param {DependencyLocation|SourcePosition|string} loc location + * @returns {string} formatted location + */ const formatLocation = loc => { if (loc === null) return ""; - const typeOfLoc = typeof loc; - switch (typeOfLoc) { - case "string": - return loc; - case "number": - return `${loc}`; - case "object": - if (loc.start && loc.end) { - if ( - typeof loc.start.line === "number" && - typeof loc.end.line === "number" && - typeof loc.end.column === "number" && - loc.start.line === loc.end.line - ) { - return `${formatPosition(loc.start)}-${loc.end.column}`; - } else { - return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`; - } - } - if (loc.start) { - return formatPosition(loc.start); + // TODO webpack 5: Simplify this + if (typeof loc === "string") return loc; + if (typeof loc === "number") return `${loc}`; + if (typeof loc === "object") { + if ("start" in loc && loc.start && "end" in loc && loc.end) { + if ( + typeof loc.start === "object" && + typeof loc.start.line === "number" && + typeof loc.end === "object" && + typeof loc.end.line === "number" && + typeof loc.end.column === "number" && + loc.start.line === loc.end.line + ) { + return `${formatPosition(loc.start)}-${loc.end.column}`; + } else { + return `${formatPosition(loc.start)}-${formatPosition(loc.end)}`; } - return formatPosition(loc); - default: - return ""; + } + if ("start" in loc && loc.start) { + return formatPosition(loc.start); + } + if ("name" in loc && "index" in loc) { + return `${loc.name}[${loc.index}]`; + } + if ("name" in loc) { + return loc.name; + } + return formatPosition(loc); } + return ""; }; module.exports = formatLocation;