Skip to content

Commit 99f9362

Browse files
committedSep 11, 2018
feat: Allow to return async template parameters
1 parent 74774eb commit 99f9362

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed
 

‎index.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -336,17 +336,20 @@ class HtmlWebpackPlugin {
336336
headTags: HtmlTagObject[],
337337
bodyTags: HtmlTagObject[]
338338
}} assetTags
339-
* @returns {{[key: any]: any}}
339+
* @returns {Promise<{[key: any]: any}>}
340340
*/
341341
getTemplateParameters (compilation, assets, assetTags) {
342-
if (this.options.templateParameters === false) {
343-
return {};
342+
const templateParameters = this.options.templateParameters;
343+
if (templateParameters === false) {
344+
return Promise.resolve({});
344345
}
345-
if (typeof this.options.templateParameters === 'function') {
346-
return this.options.templateParameters(compilation, assets, assetTags, this.options);
346+
if (typeof templateParameters === 'function') {
347+
return Promise
348+
.resolve()
349+
.then(() => templateParameters(compilation, assets, assetTags, this.options));
347350
}
348-
if (typeof this.options.templateParameters === 'object') {
349-
return this.options.templateParameters;
351+
if (typeof templateParameters === 'object') {
352+
return Promise.resolve(templateParameters);
350353
}
351354
throw new Error('templateParameters has to be either a function or an object');
352355
}
@@ -372,18 +375,17 @@ class HtmlWebpackPlugin {
372375
*/
373376
executeTemplate (templateFunction, assets, assetTags, compilation) {
374377
// Template processing
375-
const templateParams = this.getTemplateParameters(compilation, assets, assetTags);
376-
/** @type {string|Promise<string>} */
377-
let html = '';
378-
try {
379-
html = templateFunction(templateParams);
380-
} catch (e) {
381-
compilation.errors.push(new Error('Template execution failed: ' + e));
382-
return Promise.reject(e);
383-
}
384-
// If html is a promise return the promise
385-
// If html is a string turn it into a promise
386-
return Promise.resolve().then(() => html);
378+
const templateParamsPromise = this.getTemplateParameters(compilation, assets, assetTags);
379+
return templateParamsPromise.then((templateParams) => {
380+
try {
381+
// If html is a promise return the promise
382+
// If html is a string turn it into a promise
383+
return templateFunction(templateParams);
384+
} catch (e) {
385+
compilation.errors.push(new Error('Template execution failed: ' + e));
386+
return Promise.reject(e);
387+
}
388+
});
387389
}
388390

389391
/**

‎spec/basic.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,26 @@ describe('HtmlWebpackPlugin', () => {
17431743
}, ['templateParams keys: "foo"'], null, done);
17441744
});
17451745

1746+
it('should allow to set specific template parameters using a async function', done => {
1747+
testHtmlPlugin({
1748+
mode: 'production',
1749+
entry: path.join(__dirname, 'fixtures/index.js'),
1750+
output: {
1751+
path: OUTPUT_DIR,
1752+
filename: 'index_bundle.js'
1753+
},
1754+
plugins: [
1755+
new HtmlWebpackPlugin({
1756+
template: path.join(__dirname, 'fixtures/templateParam.js'),
1757+
inject: false,
1758+
templateParameters: function () {
1759+
return Promise.resolve({ 'foo': 'bar' });
1760+
}
1761+
})
1762+
]
1763+
}, ['templateParams keys: "foo"'], null, done);
1764+
});
1765+
17461766
it('should not treat templateContent set to an empty string as missing', done => {
17471767
testHtmlPlugin({
17481768
mode: 'production',

‎typings.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ interface HtmlWebpackPluginOptions {
2424
*/
2525
templateParameters:
2626
false // Pass an empty object to the template function
27-
| ((compilation: any, assets, assetTags: { headTags: Array<HtmlTagObject>, bodyTags: Array<HtmlTagObject> }, options: HtmlWebpackPluginOptions) => {})
27+
| ((compilation: any, assets, assetTags: { headTags: Array<HtmlTagObject>, bodyTags: Array<HtmlTagObject> }, options: HtmlWebpackPluginOptions) => {[option: string]: any})
28+
| ((compilation: any, assets, assetTags: { headTags: Array<HtmlTagObject>, bodyTags: Array<HtmlTagObject> }, options: HtmlWebpackPluginOptions) => Promise<{[option: string]: any}>)
2829
| {[option: string]: any}
2930
/**
3031
* The file to write the HTML to.

0 commit comments

Comments
 (0)
Please sign in to comment.