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

add outputFormatter option #123

Merged
merged 2 commits into from
Feb 26, 2018
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: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ All entries in `files` correspond to the object structure described in the `Hook

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

### `options.serialize`

Type: `function`<br>
Default: `(manifest) => JSON.stringify(manifest, null, 2)`

Output manifest file in different format then json (i.e. yaml).

## Hooks Options

Expand Down
11 changes: 7 additions & 4 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ function ManifestPlugin(opts) {
map: null,
generate: null,
sort: null,
serialize: function(manifest) {
return JSON.stringify(manifest, null, 2);
},
}, opts || {});
}

Expand Down Expand Up @@ -151,23 +154,23 @@ ManifestPlugin.prototype.apply = function(compiler) {
}, seed);
}

var json = JSON.stringify(manifest, null, 2);
var output = this.opts.serialize(manifest);

var outputFolder = compilation.options.output.path;
var outputFile = path.resolve(compilation.options.output.path, this.opts.fileName);
var outputName = path.relative(outputFolder, outputFile);

compilation.assets[outputName] = {
source: function() {
return json;
return output;
},
size: function() {
return json.length;
return output.length;
}
};

if (this.opts.writeToFileEmit) {
fse.outputFileSync(outputFile, json);
fse.outputFileSync(outputFile, output);
}

// NOTE: make sure webpack is not writing multiple manifests simultaneously
Expand Down
27 changes: 27 additions & 0 deletions spec/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,5 +759,32 @@ describe('ManifestPlugin', function() {
done();
});
});

it('supports custom serializer using serialize option', function(done) {
webpackCompile({
context: __dirname,
entry: './fixtures/file.js'
}, {
manifestOptions: {
fileName: 'webpack.manifest.yml',
serialize: function(manifest) {
var output = '';
for (var key in manifest) {
output += '- ' + key + ': "' + manifest[key] + '"\n';
}
return output;
},
}
}, function(manifest, stats, fs) {
var OUTPUT_DIR = path.join(__dirname, './webpack-out');
var manifestPath = path.join(OUTPUT_DIR, 'webpack.manifest.yml');

var manifest =fs.readFileSync(manifestPath).toString();

expect(manifest).toEqual('- main.js: "main.js"\n');

done();
});
});
});
});