Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow to overwrite the templateParameter #904

Merged
merged 1 commit into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(',') + '"';
};