From 5c7b1b1686d7b945ccf61c25ed7e4e170ad1bc5a Mon Sep 17 00:00:00 2001 From: Stanislav Fifik Date: Wed, 21 Feb 2018 15:32:03 +0100 Subject: [PATCH 1/2] feat: add outputFormatter option --- README.md | 6 ++++++ lib/plugin.js | 11 +++++++---- spec/plugin.spec.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index edbd36c..d9d43f5 100644 --- a/README.md +++ b/README.md @@ -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.outputFormatter` + +Type: `function`
+Default: `(manifest) => JSON.stringify(manifest, null, 2)` + +Output manifest file in different format then json (i.e. yaml). ## Hooks Options diff --git a/lib/plugin.js b/lib/plugin.js index 9ef9d8f..57c190a 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -16,6 +16,9 @@ function ManifestPlugin(opts) { map: null, generate: null, sort: null, + outputFormatter: function(manifest) { + return JSON.stringify(manifest, null, 2); + }, }, opts || {}); } @@ -151,7 +154,7 @@ ManifestPlugin.prototype.apply = function(compiler) { }, seed); } - var json = JSON.stringify(manifest, null, 2); + var output = this.opts.outputFormatter(manifest); var outputFolder = compilation.options.output.path; var outputFile = path.resolve(compilation.options.output.path, this.opts.fileName); @@ -159,15 +162,15 @@ ManifestPlugin.prototype.apply = function(compiler) { 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 diff --git a/spec/plugin.spec.js b/spec/plugin.spec.js index 995b4ef..261ebd7 100644 --- a/spec/plugin.spec.js +++ b/spec/plugin.spec.js @@ -759,5 +759,34 @@ describe('ManifestPlugin', function() { done(); }); }); + + describe('using relative path', function() { + it('should use output to the correct location', function(done) { + webpackCompile({ + context: __dirname, + entry: './fixtures/file.js' + }, { + manifestOptions: { + fileName: 'webpack.manifest.yml', + outputFormatter: 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(); + }); + }); + }); }); }); From 7635d5dcdd17c20e92bee4e894ab1c2727d62e7b Mon Sep 17 00:00:00 2001 From: Stanislav Fifik Date: Wed, 21 Feb 2018 16:33:44 +0100 Subject: [PATCH 2/2] refactor: rename outputFormatter to serialize --- README.md | 2 +- lib/plugin.js | 4 ++-- spec/plugin.spec.js | 44 +++++++++++++++++++++----------------------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index d9d43f5..7d38fce 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ 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.outputFormatter` +### `options.serialize` Type: `function`
Default: `(manifest) => JSON.stringify(manifest, null, 2)` diff --git a/lib/plugin.js b/lib/plugin.js index 57c190a..d5f6dc9 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -16,7 +16,7 @@ function ManifestPlugin(opts) { map: null, generate: null, sort: null, - outputFormatter: function(manifest) { + serialize: function(manifest) { return JSON.stringify(manifest, null, 2); }, }, opts || {}); @@ -154,7 +154,7 @@ ManifestPlugin.prototype.apply = function(compiler) { }, seed); } - var output = this.opts.outputFormatter(manifest); + var output = this.opts.serialize(manifest); var outputFolder = compilation.options.output.path; var outputFile = path.resolve(compilation.options.output.path, this.opts.fileName); diff --git a/spec/plugin.spec.js b/spec/plugin.spec.js index 261ebd7..97bde50 100644 --- a/spec/plugin.spec.js +++ b/spec/plugin.spec.js @@ -760,32 +760,30 @@ describe('ManifestPlugin', function() { }); }); - describe('using relative path', function() { - it('should use output to the correct location', function(done) { - webpackCompile({ - context: __dirname, - entry: './fixtures/file.js' - }, { - manifestOptions: { - fileName: 'webpack.manifest.yml', - outputFormatter: 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'); + 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(); + var manifest =fs.readFileSync(manifestPath).toString(); - expect(manifest).toEqual('- main.js: "main.js"\n'); + expect(manifest).toEqual('- main.js: "main.js"\n'); - done(); - }); + done(); }); }); });