Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds sort option #79

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: in dependency order

Sort files before they are passed to reduce. [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
44 changes: 31 additions & 13 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,34 @@ 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 +145,9 @@ 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 (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
if (this.opts.sort) {
files = files.sort(this.opts.sort);
}

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
1 change: 1 addition & 0 deletions spec/fixtures/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
2 changes: 2 additions & 0 deletions spec/fixtures/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./common.js');
console.log('main');
2 changes: 2 additions & 0 deletions spec/fixtures/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./common.js');
console.log('util');
55 changes: 55 additions & 0 deletions spec/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ describe('ManifestPlugin', function() {
});
});

it('multiple file output is dependency ordered', function(done) {
webpackCompile({
context: __dirname,
entry: {
main: './fixtures/main.js',
vendor: './fixtures/util.js'
},
plugins: [
new plugin({
seed: [],
reduce: function (manifest, file) {
return manifest.concat(file.name);
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'common',
filename: 'common.js'
})
]
}, {}, function(manifest) {
expect(manifest).toEqual(['common.js', 'vendor.js', 'main.js']);

done();
});
});

it('works with hashes in the filename', function(done) {
webpackCompile({
context: __dirname,
Expand Down Expand Up @@ -581,6 +607,35 @@ describe('ManifestPlugin', function() {
});
});

describe('sort', function() {
it('should allow ordering of output', function(done) {
webpackCompile({
context: __dirname,
entry: {
one: './fixtures/file.js',
two: './fixtures/file-two.js'
},
output: {
filename: '[name].js'
}
}, {
manifestOptions: {
seed: [],
sort: function(file, i) {
return 1;
},
reduce: function (manifest, file) {
return manifest.concat(file.name);
}
}
}, function(manifest, stats) {
expect(manifest).toEqual(['two.js', 'one.js']);

done();
});
});
});

describe('reduce', function() {
it('should generate custom manifest', function(done) {
webpackCompile({
Expand Down