Skip to content

Commit

Permalink
feat: add generate option (#90)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: reduce option replaced by generate option
  • Loading branch information
mastilver authored Sep 19, 2017
1 parent c3e4236 commit 32fb9a5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ Type: `function`
Modify files details before the manifest is created. [more details](#hooks-options)


### `options.reduce`
### `options.generate`

Type: `function`<br>
Default: `(manifest, {name, path}) => ({...manifest, [name]: path})`
Default: `(seed, files) => files.reduce((manifest, {name, path}) => ({...manifest, [name]: path}), seed)`

Create the manifest. It can return anything as long as it's serialisable by `JSON.stringify`. Use the `seed` options to populate `manifest`. [more details](#hooks-options)
Create the manifest. It can return anything as long as it's serialisable by `JSON.stringify`. [more details](#hooks-options)


## Hooks Options
Expand Down
6 changes: 3 additions & 3 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function ManifestPlugin(opts) {
seed: null,
filter: null,
map: null,
reduce: null,
generate: null,
}, opts || {});
}

Expand Down Expand Up @@ -150,8 +150,8 @@ ManifestPlugin.prototype.apply = function(compiler) {
});

var manifest;
if (this.opts.reduce) {
manifest = files.reduce(this.opts.reduce, seed);
if (this.opts.generate) {
manifest = this.opts.generate(seed, files);
} else {
manifest = files.reduce(function (manifest, file) {
manifest[file.name] = file.path;
Expand Down
34 changes: 19 additions & 15 deletions spec/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ describe('ManifestPlugin', function() {
});
});

describe('reduce', function() {
describe('generate', function() {
it('should generate custom manifest', function(done) {
webpackCompile({
context: __dirname,
Expand All @@ -578,12 +578,14 @@ describe('ManifestPlugin', function() {
}
}, {
manifestOptions: {
reduce: function (manifest, file) {
manifest[file.name] = {
file: file.path,
hash: file.chunk.hash
};
return manifest;
generate: function(seed, files) {
return files.reduce(function(manifest, file) {
manifest[file.name] = {
file: file.path,
hash: file.chunk.hash
};
return manifest;
}, seed);
}
}
}, function(manifest, stats) {
Expand All @@ -610,11 +612,11 @@ describe('ManifestPlugin', function() {
seed: {
key: 'value'
},
reduce: function (manifest, file) {
expect(manifest).toEqual({
generate: function (seed) {
expect(seed).toEqual({
key: 'value'
});
return manifest;
return seed;
}
}
}, function(manifest, stats) {
Expand All @@ -636,11 +638,13 @@ describe('ManifestPlugin', function() {
}, {
manifestOptions: {
seed: [],
reduce: function (manifest, file) {
return manifest.concat({
name: file.name,
file: file.path
});
generate: function (seed, files) {
return seed.concat(files.map(function(file) {
return {
name: file.name,
file: file.path
};
}));
}
}
}, function(manifest, stats) {
Expand Down

0 comments on commit 32fb9a5

Please sign in to comment.