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

feat: add generate option #90

Merged
merged 1 commit into from
Sep 19, 2017
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,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