Allows events to add no-value attributes

This commit is contained in:
Mike Evans 2016-12-05 19:34:57 +00:00 committed by Jan Nicklas
parent 90c6b90279
commit d22c23066a
4 changed files with 81 additions and 4 deletions

View File

@ -1,6 +1,10 @@
Change History
==============
v2.26.0
---
* Allow plugins to add attributes without values to the `<script>` and `<link>` tags
v2.25.0
---
* Clearer loader output

View File

@ -581,9 +581,16 @@ HtmlWebpackPlugin.prototype.appendHash = function (url, hash) {
* Turn a tag definition into a html string
*/
HtmlWebpackPlugin.prototype.createHtmlTag = function (tagDefinition) {
var attributes = Object.keys(tagDefinition.attributes || {}).map(function (attributeName) {
return attributeName + '="' + tagDefinition.attributes[attributeName] + '"';
});
var attributes = Object.keys(tagDefinition.attributes || {})
.filter(function (attributeName) {
return tagDefinition.attributes[attributeName] !== false;
})
.map(function (attributeName) {
if (tagDefinition.attributes[attributeName] === true) {
return attributeName;
}
return attributeName + '="' + tagDefinition.attributes[attributeName] + '"';
});
return '<' + [tagDefinition.tagName].concat(attributes).join(' ') + (tagDefinition.selfClosingTag ? '/' : '') + '>' +
(tagDefinition.innerHTML || '') +
(tagDefinition.closeTag ? '</' + tagDefinition.tagName + '>' : '');

View File

@ -1,6 +1,6 @@
{
"name": "html-webpack-plugin",
"version": "2.25.0",
"version": "2.26.0",
"description": "Simplifies creation of HTML files to serve your webpack bundles",
"main": "index.js",
"files": [

View File

@ -750,6 +750,72 @@ describe('HtmlWebpackPlugin', function () {
}, false, true);
});
it('allows events to add a no-value attribute', function (done) {
var examplePlugin = {
apply: function (compiler) {
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-alter-asset-tags', function (pluginArgs, callback) {
pluginArgs.body = pluginArgs.body.map(tag => {
if (tag.tagName === 'script') {
tag.attributes.async = true;
}
return tag;
});
callback(null, pluginArgs);
});
});
}
};
testHtmlPlugin({
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [
new HtmlWebpackPlugin(),
examplePlugin
]
},
[/<body>[\s]*<script type="text\/javascript" src="app_bundle.js" async><\/script>[\s]*<\/body>/],
null, done, false, false);
});
it('allows events to remove an attribute by setting it to false', function (done) {
var examplePlugin = {
apply: function (compiler) {
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-alter-asset-tags', function (pluginArgs, callback) {
pluginArgs.body = pluginArgs.body.map(tag => {
if (tag.tagName === 'script') {
tag.attributes.async = false;
}
return tag;
});
callback(null, pluginArgs);
});
});
}
};
testHtmlPlugin({
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [
new HtmlWebpackPlugin(),
examplePlugin
]
},
[/<body>[\s]*<script type="text\/javascript" src="app_bundle.js"><\/script>[\s]*<\/body>/],
null, done, false, false);
});
it('fires the html-webpack-plugin-before-html-processing event', function (done) {
var eventFired = false;
var examplePlugin = {