diff --git a/lib/Chunk.js b/lib/Chunk.js index bc582e49b..774eb445a 100644 --- a/lib/Chunk.js +++ b/lib/Chunk.js @@ -24,12 +24,24 @@ const ERR_CHUNK_INITIAL = */ /** - * Compare two objects based on their ids for sorting - * @param {WithId} a object that contains an ID property - * @param {WithId} b object that contains an ID property + * Compare two Modules based on their ids for sorting + * @param {Module} a module + * @param {Module} b module * @returns {-1|0|1} sort value */ -const sortById = (a, b) => { +const sortModuleById = (a, b) => { + if (a.id < b.id) return -1; + if (b.id < a.id) return 1; + return 0; +}; + +/** + * Compare two ChunkGroups based on their ids for sorting + * @param {ChunkGroup} a chunk group + * @param {ChunkGroup} b chunk group + * @returns {-1|0|1} sort value + */ +const sortChunkGroupById = (a, b) => { if (a.id < b.id) return -1; if (b.id < a.id) return 1; return 0; @@ -103,7 +115,7 @@ class Chunk { /** @private @type {SortableSet} */ this._modules = new SortableSet(undefined, sortByIdentifier); /** @private @type {SortableSet} */ - this._groups = new SortableSet(undefined, sortById); + this._groups = new SortableSet(undefined, sortChunkGroupById); /** @type {Source[]} */ this.files = []; /** @type {boolean} */ @@ -492,7 +504,7 @@ class Chunk { * @returns {void} */ sortModules(sortByFn) { - this._modules.sortWith(sortByFn || sortById); + this._modules.sortWith(sortByFn || sortModuleById); } sortItems() { diff --git a/lib/ModuleTemplate.js b/lib/ModuleTemplate.js index 50697aecf..06e787ed9 100644 --- a/lib/ModuleTemplate.js +++ b/lib/ModuleTemplate.js @@ -6,6 +6,9 @@ const { Tapable, SyncWaterfallHook, SyncHook } = require("tapable"); +/** @typedef {import("webpack-sources").Source} Source */ +/** @typedef {import("./Module")} Module */ + module.exports = class ModuleTemplate extends Tapable { constructor(runtimeTemplate, type) { super(); @@ -40,6 +43,12 @@ module.exports = class ModuleTemplate extends Tapable { }; } + /** + * @param {Module} module the module + * @param {TODO} dependencyTemplates templates for dependencies + * @param {TODO} options render options + * @returns {Source} the source + */ render(module, dependencyTemplates, options) { try { const moduleSource = module.source( diff --git a/lib/Template.js b/lib/Template.js index b768d2ff0..443e35767 100644 --- a/lib/Template.js +++ b/lib/Template.js @@ -44,14 +44,6 @@ const stringifyIdSortPredicate = (a, b) => { return 0; }; -/** - * @param {Module} module the module to compare against - * @returns {boolean} return true if module.id is equal to type "number" - */ -const moduleIdIsNumber = module => { - return typeof module.id === "number"; -}; - class Template { /** * @@ -177,18 +169,20 @@ class Template { } /** - * - * @param {Module[]} modules a collection of modules to get array bounds for + * @typedef {Object} WithId + * @property {string|number} id + */ + + /** + * @param {WithId[]} modules a collection of modules to get array bounds for * @returns {[number, number] | false} returns the upper and lower array bounds * or false if not every module has a number based id */ static getModulesArrayBounds(modules) { - // Typeguards don't work for .every() with predicate functions - // https://github.com/Microsoft/TypeScript/issues/23799 - if (!modules.every(moduleIdIsNumber)) return false; var maxId = -Infinity; var minId = Infinity; for (const module of modules) { + if (typeof module.id !== "number") return false; if (maxId < module.id) maxId = /** @type {number} */ (module.id); if (minId > module.id) minId = /** @type {number} */ (module.id); } @@ -236,7 +230,6 @@ class Template { source.add("[]"); return source; } - /** @type {Module[]} */ var allModules = modules.map(module => { return { id: module.id,