Skip to content

Commit

Permalink
feat: add sort option
Browse files Browse the repository at this point in the history
BREAKING CHANGE: will not order keys by default anymore
  • Loading branch information
SeeThruHead authored and mastilver committed Sep 21, 2017
1 parent a03b56c commit ae03fbd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ Type: `function`

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

### `options.sort`

Type: `function`

Sort files before they are passed to `generate`. [more details](#hooks-options)

### `options.generate`

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

## Hooks Options

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

### `path`

Expand Down
15 changes: 3 additions & 12 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,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.generate) {
Expand Down
30 changes: 30 additions & 0 deletions spec/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,36 @@ 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(a, b) {
// make sure one is the latest
return a.name === 'one.js' ? 1 : -1;
},
generate: function (seed, files) {
return files.map(file => file.name);
}
}
}, function(manifest, stats) {
expect(manifest).toEqual(['two.js', 'one.js']);

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

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

1 comment on commit ae03fbd

@joscha
Copy link
Contributor

@joscha joscha commented on ae03fbd Sep 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.