Allows events to add no-value attributes
This commit is contained in:
parent
90c6b90279
commit
d22c23066a
|
|
@ -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
|
||||
|
|
|
|||
13
index.js
13
index.js
|
|
@ -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 + '>' : '');
|
||||
|
|
|
|||
|
|
@ -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": [
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
|
|||
Loading…
Reference in New Issue