Skip to content

Commit c5e32d3

Browse files
committed
feat: Allow to overwrite the templateParameter #830
1 parent 2763474 commit c5e32d3

File tree

4 files changed

+87
-12
lines changed

4 files changed

+87
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Allowed values are as follows
110110
|**[`title`](#)**|`{String}`|``|The title to use for the generated HTML document|
111111
|**[`filename`](#)**|`{String}`|`'index.html'`|The file to write the HTML to. Defaults to `index.html`. You can specify a subdirectory here too (eg: `assets/admin.html`)|
112112
|**[`template`](#)**|`{String}`|``|`webpack` require path to the template. Please see the [docs](https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md) for details|
113+
|**[`templateParameters`](#)**|`{Boolean\|Object\|Function}`|``| Allows to overwrite the parameters used in the template |
113114
|**[`inject`](#)**|`{Boolean\|String}`|`true`|`true \|\| 'head' \|\| 'body' \|\| false` Inject all assets into the given `template` or `templateContent`. When passing `true` or `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element|
114115
|**[`favicon`](#)**|`{String}`|``|Adds the given favicon path to the output HTML|
115116
|**[`minify`](#)**|`{Boolean\|Object}`|`true`|Pass [html-minifier](https://github.com/kangax/html-minifier#options-quick-reference)'s options as object to minify the output|

index.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class HtmlWebpackPlugin {
1919
// Default options
2020
this.options = _.extend({
2121
template: path.join(__dirname, 'default_index.ejs'),
22+
templateParameters: templateParametersGenerator,
2223
filename: 'index.html',
2324
hash: false,
2425
inject: true,
@@ -253,25 +254,29 @@ class HtmlWebpackPlugin {
253254
: Promise.reject('The loader "' + this.options.template + '" didn\'t return html.');
254255
}
255256

257+
/**
258+
* Generate the template parameters for the template function
259+
*/
260+
getTemplateParameters (compilation, assets) {
261+
if (typeof this.options.templateParameters === 'function') {
262+
return this.options.templateParameters(compilation, assets, this.options);
263+
}
264+
if (typeof this.options.templateParameters === 'object') {
265+
return this.options.templateParameters;
266+
}
267+
return {};
268+
}
269+
256270
/**
257271
* Html post processing
258272
*
259273
* Returns a promise
260274
*/
261275
executeTemplate (templateFunction, chunks, assets, compilation) {
262-
const self = this;
263276
return Promise.resolve()
264277
// Template processing
265278
.then(() => {
266-
const templateParams = {
267-
compilation: compilation,
268-
webpack: compilation.getStats().toJson(),
269-
webpackConfig: compilation.options,
270-
htmlWebpackPlugin: {
271-
files: assets,
272-
options: self.options
273-
}
274-
};
279+
const templateParams = this.getTemplateParameters(compilation, assets);
275280
let html = '';
276281
try {
277282
html = templateFunction(templateParams);
@@ -684,4 +689,20 @@ function trainCaseToCamelCase (word) {
684689
return word.replace(/-([\w])/g, (match, p1) => p1.toUpperCase());
685690
}
686691

692+
/**
693+
* The default for options.templateParameter
694+
* Generate the template parameters
695+
*/
696+
function templateParametersGenerator (compilation, assets, options) {
697+
return {
698+
compilation: compilation,
699+
webpack: compilation.getStats().toJson(),
700+
webpackConfig: compilation.options,
701+
htmlWebpackPlugin: {
702+
files: assets,
703+
options: options
704+
}
705+
};
706+
}
707+
687708
module.exports = HtmlWebpackPlugin;

spec/BasicSpec.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,60 @@ describe('HtmlWebpackPlugin', function () {
16141614
inject: false
16151615
})
16161616
]
1617-
}, ['templateParams.compilation exists: true'], null, done);
1617+
}, ['templateParams keys: "compilation,webpack,webpackConfig,htmlWebpackPlugin"'], null, done);
1618+
});
1619+
1620+
it('should allow to disable template parameters', function (done) {
1621+
testHtmlPlugin({
1622+
entry: path.join(__dirname, 'fixtures/index.js'),
1623+
output: {
1624+
path: OUTPUT_DIR,
1625+
filename: 'index_bundle.js'
1626+
},
1627+
plugins: [
1628+
new HtmlWebpackPlugin({
1629+
template: path.join(__dirname, 'fixtures/templateParam.js'),
1630+
inject: false,
1631+
templateParameters: false
1632+
})
1633+
]
1634+
}, ['templateParams keys: ""'], null, done);
1635+
});
1636+
1637+
it('should allow to set specific template parameters', function (done) {
1638+
testHtmlPlugin({
1639+
entry: path.join(__dirname, 'fixtures/index.js'),
1640+
output: {
1641+
path: OUTPUT_DIR,
1642+
filename: 'index_bundle.js'
1643+
},
1644+
plugins: [
1645+
new HtmlWebpackPlugin({
1646+
template: path.join(__dirname, 'fixtures/templateParam.js'),
1647+
inject: false,
1648+
templateParameters: { foo: 'bar' }
1649+
})
1650+
]
1651+
}, ['templateParams keys: "foo"'], null, done);
1652+
});
1653+
1654+
it('should allow to set specific template parameters using a function', function (done) {
1655+
testHtmlPlugin({
1656+
entry: path.join(__dirname, 'fixtures/index.js'),
1657+
output: {
1658+
path: OUTPUT_DIR,
1659+
filename: 'index_bundle.js'
1660+
},
1661+
plugins: [
1662+
new HtmlWebpackPlugin({
1663+
template: path.join(__dirname, 'fixtures/templateParam.js'),
1664+
inject: false,
1665+
templateParameters: function () {
1666+
return { 'foo': 'bar' };
1667+
}
1668+
})
1669+
]
1670+
}, ['templateParams keys: "foo"'], null, done);
16181671
});
16191672

16201673
it('should not treat templateContent set to an empty string as missing', function (done) {

spec/fixtures/templateParam.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = function (templateParams) {
2-
return 'templateParams.compilation exists: ' + !!templateParams.compilation;
2+
return 'templateParams keys: "' + Object.keys(templateParams).join(',') + '"';
33
};

0 commit comments

Comments
 (0)