Skip to content

Commit ae03fbd

Browse files
SeeThruHeadmastilver
authored andcommitted
feat: add sort option
BREAKING CHANGE: will not order keys by default anymore
1 parent a03b56c commit ae03fbd

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ Type: `function`
100100

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

103+
### `options.sort`
104+
105+
Type: `function`
106+
107+
Sort files before they are passed to `generate`. [more details](#hooks-options)
103108

104109
### `options.generate`
105110

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

114119
## Hooks Options
115120

116-
`filter`, `map` take as an input an Object with the following properties:
121+
`filter`, `map`, `sort` takes as an input an Object with the following properties:
117122

118123
### `path`
119124

lib/plugin.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,9 @@ ManifestPlugin.prototype.apply = function(compiler) {
136136
files = files.map(this.opts.map);
137137
}
138138

139-
files = files.sort(function (fileA, fileB) {
140-
var a = fileA.name;
141-
var b = fileB.name;
142-
143-
if (a < b) {
144-
return -1;
145-
} else if (a > b) {
146-
return 1;
147-
} else {
148-
return 0;
149-
}
150-
});
139+
if (this.opts.sort) {
140+
files = files.sort(this.opts.sort);
141+
}
151142

152143
var manifest;
153144
if (this.opts.generate) {

spec/plugin.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,36 @@ describe('ManifestPlugin', function() {
568568
});
569569
});
570570

571+
describe('sort', function() {
572+
it('should allow ordering of output', function(done) {
573+
webpackCompile({
574+
context: __dirname,
575+
entry: {
576+
one: './fixtures/file.js',
577+
two: './fixtures/file-two.js'
578+
},
579+
output: {
580+
filename: '[name].js'
581+
}
582+
}, {
583+
manifestOptions: {
584+
seed: [],
585+
sort: function(a, b) {
586+
// make sure one is the latest
587+
return a.name === 'one.js' ? 1 : -1;
588+
},
589+
generate: function (seed, files) {
590+
return files.map(file => file.name);
591+
}
592+
}
593+
}, function(manifest, stats) {
594+
expect(manifest).toEqual(['two.js', 'one.js']);
595+
596+
done();
597+
});
598+
});
599+
});
600+
571601
describe('generate', function() {
572602
it('should generate custom manifest', function(done) {
573603
webpackCompile({

0 commit comments

Comments
 (0)