Skip to content

Commit

Permalink
feat: Allow to overwrite the templateParameter #830
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Mar 22, 2018
1 parent 2763474 commit c5e32d3
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Allowed values are as follows
|**[`title`](#)**|`{String}`|``|The title to use for the generated HTML document|
|**[`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`)|
|**[`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|
|**[`templateParameters`](#)**|`{Boolean\|Object\|Function}`|``| Allows to overwrite the parameters used in the template |
|**[`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|
|**[`favicon`](#)**|`{String}`|``|Adds the given favicon path to the output HTML|
|**[`minify`](#)**|`{Boolean\|Object}`|`true`|Pass [html-minifier](https://github.com/kangax/html-minifier#options-quick-reference)'s options as object to minify the output|
Expand Down
41 changes: 31 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class HtmlWebpackPlugin {
// Default options
this.options = _.extend({
template: path.join(__dirname, 'default_index.ejs'),
templateParameters: templateParametersGenerator,
filename: 'index.html',
hash: false,
inject: true,
Expand Down Expand Up @@ -253,25 +254,29 @@ class HtmlWebpackPlugin {
: Promise.reject('The loader "' + this.options.template + '" didn\'t return html.');
}

/**
* Generate the template parameters for the template function
*/
getTemplateParameters (compilation, assets) {
if (typeof this.options.templateParameters === 'function') {
return this.options.templateParameters(compilation, assets, this.options);
}
if (typeof this.options.templateParameters === 'object') {
return this.options.templateParameters;
}
return {};
}

/**
* Html post processing
*
* Returns a promise
*/
executeTemplate (templateFunction, chunks, assets, compilation) {
const self = this;
return Promise.resolve()
// Template processing
.then(() => {
const templateParams = {
compilation: compilation,
webpack: compilation.getStats().toJson(),
webpackConfig: compilation.options,
htmlWebpackPlugin: {
files: assets,
options: self.options
}
};
const templateParams = this.getTemplateParameters(compilation, assets);
let html = '';
try {
html = templateFunction(templateParams);
Expand Down Expand Up @@ -684,4 +689,20 @@ function trainCaseToCamelCase (word) {
return word.replace(/-([\w])/g, (match, p1) => p1.toUpperCase());
}

/**
* The default for options.templateParameter
* Generate the template parameters
*/
function templateParametersGenerator (compilation, assets, options) {
return {
compilation: compilation,
webpack: compilation.getStats().toJson(),
webpackConfig: compilation.options,
htmlWebpackPlugin: {
files: assets,
options: options
}
};
}

module.exports = HtmlWebpackPlugin;
55 changes: 54 additions & 1 deletion spec/BasicSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,60 @@ describe('HtmlWebpackPlugin', function () {
inject: false
})
]
}, ['templateParams.compilation exists: true'], null, done);
}, ['templateParams keys: "compilation,webpack,webpackConfig,htmlWebpackPlugin"'], null, done);
});

it('should allow to disable template parameters', function (done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures/index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, 'fixtures/templateParam.js'),
inject: false,
templateParameters: false
})
]
}, ['templateParams keys: ""'], null, done);
});

it('should allow to set specific template parameters', function (done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures/index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, 'fixtures/templateParam.js'),
inject: false,
templateParameters: { foo: 'bar' }
})
]
}, ['templateParams keys: "foo"'], null, done);
});

it('should allow to set specific template parameters using a function', function (done) {
testHtmlPlugin({
entry: path.join(__dirname, 'fixtures/index.js'),
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, 'fixtures/templateParam.js'),
inject: false,
templateParameters: function () {
return { 'foo': 'bar' };
}
})
]
}, ['templateParams keys: "foo"'], null, done);
});

it('should not treat templateContent set to an empty string as missing', function (done) {
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/templateParam.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = function (templateParams) {
return 'templateParams.compilation exists: ' + !!templateParams.compilation;
return 'templateParams keys: "' + Object.keys(templateParams).join(',') + '"';
};

0 comments on commit c5e32d3

Please sign in to comment.