diff --git a/README.md b/README.md index ee2a7c4e..6bc8e347 100644 --- a/README.md +++ b/README.md @@ -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| diff --git a/index.js b/index.js index ae6026e5..d7844eaf 100644 --- a/index.js +++ b/index.js @@ -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, @@ -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); @@ -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; diff --git a/spec/BasicSpec.js b/spec/BasicSpec.js index b70f2ea3..ca0d5158 100644 --- a/spec/BasicSpec.js +++ b/spec/BasicSpec.js @@ -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) { diff --git a/spec/fixtures/templateParam.js b/spec/fixtures/templateParam.js index 55741869..6580c457 100644 --- a/spec/fixtures/templateParam.js +++ b/spec/fixtures/templateParam.js @@ -1,3 +1,3 @@ module.exports = function (templateParams) { - return 'templateParams.compilation exists: ' + !!templateParams.compilation; + return 'templateParams keys: "' + Object.keys(templateParams).join(',') + '"'; };