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 => {