From 1d66e5333bc2aeb8caadf96e572af756d3708d19 Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Thu, 2 Jan 2020 11:59:26 +0100 Subject: [PATCH] feat: merge templateParameters with default template parameters --- index.js | 29 +++++++++++++++++------------ spec/basic.spec.js | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 2442e373..c8698518 100644 --- a/index.js +++ b/index.js @@ -371,19 +371,24 @@ class HtmlWebpackPlugin { if (templateParameters === false) { return Promise.resolve({}); } - if (typeof templateParameters === 'function') { - const preparedAssetTags = { - headTags: this.prepareAssetTagGroupForRendering(assetTags.headTags), - bodyTags: this.prepareAssetTagGroupForRendering(assetTags.bodyTags) - }; - return Promise - .resolve() - .then(() => templateParameters(compilation, assets, preparedAssetTags, this.options)); - } - if (typeof templateParameters === 'object') { - return Promise.resolve(templateParameters); + if (typeof templateParameters !== 'function' && typeof templateParameters !== 'object') { + throw new Error('templateParameters has to be either a function or an object'); } - throw new Error('templateParameters has to be either a function or an object'); + const templateParameterFunction = typeof templateParameters === 'function' + // A custom function can overwrite the entire template parameter preparation + ? templateParameters + // If the template parameters is an object merge it with the default values + : (compilation, assets, assetTags, options) => Object.assign({}, + templateParametersGenerator(compilation, assets, assetTags, options), + templateParameters + ); + const preparedAssetTags = { + headTags: this.prepareAssetTagGroupForRendering(assetTags.headTags), + bodyTags: this.prepareAssetTagGroupForRendering(assetTags.bodyTags) + }; + return Promise + .resolve() + .then(() => templateParameterFunction(compilation, assets, preparedAssetTags, this.options)); } /** diff --git a/spec/basic.spec.js b/spec/basic.spec.js index 2092c5dc..69dba5b2 100644 --- a/spec/basic.spec.js +++ b/spec/basic.spec.js @@ -2077,7 +2077,7 @@ describe('HtmlWebpackPlugin', () => { templateParameters: { foo: 'bar' } }) ] - }, ['templateParams keys: "foo"'], null, done); + }, ['templateParams keys: "compilation,webpackConfig,htmlWebpackPlugin,foo"'], null, done); }); it('should allow to set specific template parameters using a function', done => {