From 112e49c0943c535d91a4f4c2e3e505860c61d948 Mon Sep 17 00:00:00 2001
From: Thomas Sileghem
Date: Sat, 16 Sep 2017 21:00:58 +0100
Subject: [PATCH] feat: add generate option
BREAKING CHANGE: reduce option replaced by generate option
---
README.md | 6 +++---
lib/plugin.js | 6 +++---
spec/plugin.spec.js | 34 +++++++++++++++++++---------------
3 files changed, 25 insertions(+), 21 deletions(-)
diff --git a/README.md b/README.md
index d3e36f1..89c180b 100644
--- a/README.md
+++ b/README.md
@@ -99,12 +99,12 @@ Type: `function`
Modify files details before the manifest is created. [more details](#hooks-options)
-### `options.reduce`
+### `options.generate`
Type: `function`
-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
diff --git a/lib/plugin.js b/lib/plugin.js
index 1ff78ef..a97cd51 100644
--- a/lib/plugin.js
+++ b/lib/plugin.js
@@ -14,7 +14,7 @@ function ManifestPlugin(opts) {
seed: null,
filter: null,
map: null,
- reduce: null,
+ generate: null,
}, opts || {});
}
@@ -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;
diff --git a/spec/plugin.spec.js b/spec/plugin.spec.js
index 35be1d2..42ea5a2 100644
--- a/spec/plugin.spec.js
+++ b/spec/plugin.spec.js
@@ -568,7 +568,7 @@ describe('ManifestPlugin', function() {
});
});
- describe('reduce', function() {
+ describe('generate', function() {
it('should generate custom manifest', function(done) {
webpackCompile({
context: __dirname,
@@ -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) {
@@ -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) {
@@ -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) {