v2.8.0: Add chunk sort mode dependency

This commit is contained in:
Jan Nicklas 2016-02-03 18:00:27 +01:00 committed by Jan Nicklas
parent 5fb1eca651
commit 3f38901758
3 changed files with 17 additions and 37 deletions

View File

@ -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');
};

View File

@ -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);
};
/**

View File

@ -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": [