Skip to content

Commit

Permalink
feat: merge templateParameters with default template parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Mar 17, 2020
1 parent dfb98e7 commit 1d66e53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
29 changes: 17 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion spec/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 1d66e53

Please sign in to comment.