Skip to content

Commit b532986

Browse files
samuelmeulimastilver
authored andcommitted
Pass entrypoints param to generate function (#192)
* Pass `entrypoints` param to `generate` function * Fix `entrypoints` compatibility with Webpack 4+
1 parent bcca890 commit b532986

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ Sort files before they are passed to `generate`. [FileDescriptor typings](#filed
108108

109109
### `options.generate`
110110

111-
Type: `Function(Object, FileDescriptor): Object`<br>
112-
Default: `(seed, files) => files.reduce((manifest, {name, path}) => ({...manifest, [name]: path}), seed)`
111+
Type: `Function(Object, FileDescriptor, string[]): Object`<br>
112+
Default: `(seed, files, entrypoints) => files.reduce((manifest, {name, path}) => ({...manifest, [name]: path}), seed)`
113113

114114
Create the manifest. It can return anything as long as it's serialisable by `JSON.stringify`. [FileDescriptor typings](#filedescriptor)
115115

lib/plugin.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,18 @@ ManifestPlugin.prototype.apply = function(compiler) {
161161

162162
var manifest;
163163
if (this.opts.generate) {
164-
manifest = this.opts.generate(seed, files);
164+
const entrypointsArray = Array.from(
165+
compilation.entrypoints instanceof Map ?
166+
// Webpack 4+
167+
compilation.entrypoints.entries() :
168+
// Webpack 3
169+
Object.entries(compilation.entrypoints)
170+
);
171+
const entrypoints = entrypointsArray.reduce((e, [name, entrypoint]) => ({
172+
...e,
173+
[name]: entrypoint.getFiles()
174+
}), {});
175+
manifest = this.opts.generate(seed, files, entrypoints);
165176
} else {
166177
manifest = files.reduce(function (manifest, file) {
167178
manifest[file.name] = file.path;

spec/plugin.spec.js

+38
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,44 @@ describe('ManifestPlugin', function() {
745745
});
746746
});
747747

748+
it('should generate manifest with "entrypoints" key', done => {
749+
webpackCompile({
750+
context: __dirname,
751+
entry: {
752+
one: './fixtures/file.js',
753+
two: './fixtures/file-two.js'
754+
}
755+
},
756+
{
757+
manifestOptions: {
758+
generate: (seed, files, entrypoints) => {
759+
const manifestFiles = files.reduce((manifest, { name, path }) => ({
760+
...manifest,
761+
[name]: path
762+
}), seed);
763+
return {
764+
files: manifestFiles,
765+
entrypoints
766+
};
767+
}
768+
}
769+
},
770+
(manifest, stats) => {
771+
expect(manifest).toEqual({
772+
entrypoints: {
773+
one: ['one.js'],
774+
two: ['two.js']
775+
},
776+
files: {
777+
'one.js': 'one.js',
778+
'two.js': 'two.js'
779+
}
780+
});
781+
782+
done();
783+
});
784+
});
785+
748786
describe('with CopyWebpackPlugin', function () {
749787
it('works when including copied assets', function (done) {
750788
webpackCompile({

0 commit comments

Comments
 (0)