Skip to content

Commit 1d66e53

Browse files
committedMar 17, 2020
feat: merge templateParameters with default template parameters
1 parent dfb98e7 commit 1d66e53

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed
 

‎index.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -371,19 +371,24 @@ class HtmlWebpackPlugin {
371371
if (templateParameters === false) {
372372
return Promise.resolve({});
373373
}
374-
if (typeof templateParameters === 'function') {
375-
const preparedAssetTags = {
376-
headTags: this.prepareAssetTagGroupForRendering(assetTags.headTags),
377-
bodyTags: this.prepareAssetTagGroupForRendering(assetTags.bodyTags)
378-
};
379-
return Promise
380-
.resolve()
381-
.then(() => templateParameters(compilation, assets, preparedAssetTags, this.options));
382-
}
383-
if (typeof templateParameters === 'object') {
384-
return Promise.resolve(templateParameters);
374+
if (typeof templateParameters !== 'function' && typeof templateParameters !== 'object') {
375+
throw new Error('templateParameters has to be either a function or an object');
385376
}
386-
throw new Error('templateParameters has to be either a function or an object');
377+
const templateParameterFunction = typeof templateParameters === 'function'
378+
// A custom function can overwrite the entire template parameter preparation
379+
? templateParameters
380+
// If the template parameters is an object merge it with the default values
381+
: (compilation, assets, assetTags, options) => Object.assign({},
382+
templateParametersGenerator(compilation, assets, assetTags, options),
383+
templateParameters
384+
);
385+
const preparedAssetTags = {
386+
headTags: this.prepareAssetTagGroupForRendering(assetTags.headTags),
387+
bodyTags: this.prepareAssetTagGroupForRendering(assetTags.bodyTags)
388+
};
389+
return Promise
390+
.resolve()
391+
.then(() => templateParameterFunction(compilation, assets, preparedAssetTags, this.options));
387392
}
388393

389394
/**

‎spec/basic.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2077,7 +2077,7 @@ describe('HtmlWebpackPlugin', () => {
20772077
templateParameters: { foo: 'bar' }
20782078
})
20792079
]
2080-
}, ['templateParams keys: "foo"'], null, done);
2080+
}, ['templateParams keys: "compilation,webpackConfig,htmlWebpackPlugin,foo"'], null, done);
20812081
});
20822082

20832083
it('should allow to set specific template parameters using a function', done => {

0 commit comments

Comments
 (0)
Please sign in to comment.