Allow to disable sorting or pass a custom sort function

This commit is contained in:
Jan Nicklas 2015-11-23 12:00:38 +01:00 committed by Jan Nicklas
parent 83ad4de679
commit 0b0e0205fc
2 changed files with 29 additions and 7 deletions

View File

@ -78,6 +78,7 @@ Allowed values are as follows:
included scripts and css files. This is useful for cache busting.
- `chunks`: Allows you to add only some chunks (e.g. only the unit-test chunk)
- `excludeChunks`: Allows you to skip some chunks (e.g. don't add the unit-test chunk)
- `chunksSortMode`: Allows to controll how chunks should be sorted before they are included to the html. Allowed values: 'none' | 'default' | {function} - default: 'auto'
Here's an example webpack config illustrating how to use these options:
```javascript

View File

@ -170,6 +170,32 @@ HtmlWebpackPlugin.prototype.addFileToAssets = function(compilation, filename) {
});
};
/**
* Helper to sort chunks
*/
HtmlWebpackPlugin.prototype.sortChunks = function(chunks, sortMode) {
// Sort mode auto by default:
if (typeof sortMode === 'undefined' || sortMode === 'auto') {
return chunks.sort(function orderEntryLast(a, b) {
if (a.entry !== b.entry) {
return b.entry ? 1 : -1;
} else {
return b.id - a.id;
}
});
}
// Disabled sorting:
if (sortMode === 'none') {
return chunks;
}
// Custom function
if (typeof sortMode === 'function') {
return chunks.sort(sortMode);
}
// Invalid sort mode
throw new Error('"' + sortMode + '" is not a valid chunk sort mode');
};
HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function(compilation, webpackStatsJson, includedChunks, excludedChunks) {
var self = this;
@ -203,13 +229,8 @@ HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function(compilation, webp
assets.favicon = self.appendHash(assets.favicon, webpackStatsJson.hash);
}
var chunks = webpackStatsJson.chunks.sort(function orderEntryLast(a, b) {
if (a.entry !== b.entry) {
return b.entry ? 1 : -1;
} else {
return b.id - a.id;
}
});
// Get sorted chunks
var chunks = HtmlWebpackPlugin.prototype.sortChunks(webpackStatsJson.chunks, this.options.chunksSortMode);
for (var i = 0; i < chunks.length; i++) {
var chunk = chunks[i];