diff --git a/index.js b/index.js index df2fbef..0fcb575 100644 --- a/index.js +++ b/index.js @@ -280,28 +280,21 @@ HtmlWebpackPlugin.prototype.addFileToAssets = function (filename, compilation) { */ HtmlWebpackPlugin.prototype.sortChunks = function (chunks, sortMode) { // Sort mode auto by default: - if (typeof sortMode === 'undefined' || sortMode === 'auto') { - return chunkSorter.auto(chunks); - } - // Sort mode 'dependency': - if (sortMode === 'dependency') { - var sortResult = chunkSorter.dependency(chunks); - - if (!sortResult) { - throw new Error('Chunk sorting based on dependencies failed. Please consider custom sort mode.'); - } - - return sortResult; - } - // Disabled sorting: - if (sortMode === 'none') { - return chunkSorter.none(chunks); + if (typeof sortMode === 'undefined') { + sortMode = 'auto'; } // Custom function if (typeof sortMode === 'function') { return chunks.sort(sortMode); } - // Invalid sort mode + // Disabled sorting: + if (sortMode === 'none') { + return chunkSorter.none(chunks); + } + // Check if the given sort mode is a valid chunkSorter sort mode + if (typeof chunkSorter[sortMode] !== 'undefined') { + return chunkSorter[sortMode](chunks); + } throw new Error('"' + sortMode + '" is not a valid chunk sort mode'); }; diff --git a/lib/chunksorter.js b/lib/chunksorter.js index 0ef52d2..ca8753e 100644 --- a/lib/chunksorter.js +++ b/lib/chunksorter.js @@ -17,19 +17,17 @@ var toposort = require('toposort'); @param {Array} chunks an array of chunks as generated by the html-webpack-plugin. It is assumed that each entry contains at least the properties "id" (containing the chunk id) and "parents" (array containing the ids of the - parent chunks). Must not be null/undefined or empty + parent chunks). - @return {Array} A topologically sorted version of the input chunks, or null if - no such order could be calculated (e.g. because the chunks and their - parent relations did not define an directed acyclic graph). + @return {Array} A topologically sorted version of the input chunks */ module.exports.dependency = function (chunks) { if (!chunks) { - return null; + return chunks; } // We build a map (chunk-id -> chunk) for faster access during graph building. - var nodeMap = []; + var nodeMap = {}; chunks.forEach(function (chunk) { nodeMap[chunk.id] = chunk; @@ -43,26 +41,15 @@ module.exports.dependency = function (chunks) { // Add an edge for each parent (parent -> child) chunk.parents.forEach(function (parentId) { var parentChunk = nodeMap[parentId]; - if (!parentChunk) { - return null; // We haven't found the referenced chunk in our map! + throw new Error('Can not find chunk parent during dependency sort'); } - edges.push([parentChunk, chunk]); }); } }); - // We now perform a topological sorting on the input chunks and built edges - var sortedVertices = null; - - try { - sortedVertices = toposort.array(chunks, edges); - } catch (err) { - return null; // Error during sort - } - - return sortedVertices; + return toposort.array(chunks, edges); }; /** diff --git a/package.json b/package.json index 5680bdd..326dfc7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "html-webpack-plugin", - "version": "2.7.2", + "version": "2.8.0", "description": "Simplifies creation of HTML files to serve your webpack bundles", "main": "index.js", "files": [