Skip to content

Commit

Permalink
adds sort option
Browse files Browse the repository at this point in the history
allows users to sort the file list before it is passed to reduce, can disable aphabetical sorting by passing () => -1

array passed to sort is presorted based on the dependency graph using topological sort on [parentChunk, chunk], this was adapted from html-webpack-plugin
  • Loading branch information
SeeThruHead committed Sep 1, 2017
1 parent de15a87 commit 641b30f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ Type: `function`

Modify files details before the manifest is created. [more details](#hooks-options)

### `sort`

Type: `function`<br>
Default: alphebetical

Sort files before they are passed to reduce. (unsorted array is in order of dependency graph) [more details](#hooks-options)

### `reduce`

Expand All @@ -119,7 +125,7 @@ Create the manifest. It can return anything as long as it's serialisable by `JSO

## Hooks Options

`filter`, `map`, `reduce` takes as an input an Object with the following properties:
`filter`, `map`, `sort`, `reduce` takes as an input an Object with the following properties:

### `path`

Expand Down
48 changes: 39 additions & 9 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var path = require('path');
var fse = require('fs-extra');
var _ = require('lodash');
var toposort = require('toposort');

function ManifestPlugin(opts) {
this.opts = _.assign({
Expand Down Expand Up @@ -44,8 +45,33 @@ ManifestPlugin.prototype.apply = function(compiler) {

compiler.plugin('emit', function(compilation, compileCallback) {
var stats = compilation.getStats().toJson();
var nodeMap = {};

var files = compilation.chunks.reduce(function(files, chunk) {
compilation.chunks.forEach(function (chunk) {
nodeMap[chunk.id] = chunk;
});

// Next, we add an edge for each parent relationship into the graph
var edges = [];

compilation.chunks.forEach(function (chunk) {
if (chunk.parents) {
// Add an edge for each parent (parent -> child)
chunk.parents.forEach(function (parentId) {
// webpack2 chunk.parents are chunks instead of string id(s)
var parentChunk = _.isObject(parentId) ? parentId : nodeMap[parentId];
// If the parent chunk does not exist (e.g. because of an excluded chunk)
// we ignore that parent
if (parentChunk) {
edges.push([parentChunk, chunk]);
}
});
}
});
// We now perform a topological sorting on the input chunks and built edges
var chunks = toposort.array(compilation.chunks, edges);

var files = chunks.reduce(function(files, chunk) {
return chunk.files.reduce(function (files, path) {
// Don't add hot updates to manifest
if (path.indexOf('hot-update') >= 0) {
Expand Down Expand Up @@ -118,18 +144,22 @@ ManifestPlugin.prototype.apply = function(compiler) {
files = files.map(this.opts.map);
}

files = files.sort(function (fileA, fileB) {
var a = fileA.name;
var b = fileB.name;
if (this.opts.sort) {
files = files.sort(this.opts.sort);
} else {
files = files.sort(function (fileA, fileB) {
var a = fileA.name;
var b = fileB.name;

if (a < b) {
if (a < b) {
return -1;
} else if (a > b) {
} else if (a > b) {
return 1;
} else {
} else {
return 0;
}
});
}
});
}

var manifest;
if (this.opts.reduce) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"homepage": "https://github.com/danethurber/webpack-manifest-plugin",
"dependencies": {
"fs-extra": "^0.30.0",
"lodash": ">=3.5 <5"
"lodash": ">=3.5 <5",
"toposort": "^1.0.3"
},
"nyc": {
"reporter": [
Expand Down

0 comments on commit 641b30f

Please sign in to comment.