Skip to content

Commit d22c230

Browse files
numicaljantimon
authored andcommitted
Allows events to add no-value attributes
1 parent 90c6b90 commit d22c230

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Change History
22
==============
33

4+
v2.26.0
5+
---
6+
* Allow plugins to add attributes without values to the `<script>` and `<link>` tags
7+
48
v2.25.0
59
---
610
* Clearer loader output

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,16 @@ HtmlWebpackPlugin.prototype.appendHash = function (url, hash) {
581581
* Turn a tag definition into a html string
582582
*/
583583
HtmlWebpackPlugin.prototype.createHtmlTag = function (tagDefinition) {
584-
var attributes = Object.keys(tagDefinition.attributes || {}).map(function (attributeName) {
585-
return attributeName + '="' + tagDefinition.attributes[attributeName] + '"';
586-
});
584+
var attributes = Object.keys(tagDefinition.attributes || {})
585+
.filter(function (attributeName) {
586+
return tagDefinition.attributes[attributeName] !== false;
587+
})
588+
.map(function (attributeName) {
589+
if (tagDefinition.attributes[attributeName] === true) {
590+
return attributeName;
591+
}
592+
return attributeName + '="' + tagDefinition.attributes[attributeName] + '"';
593+
});
587594
return '<' + [tagDefinition.tagName].concat(attributes).join(' ') + (tagDefinition.selfClosingTag ? '/' : '') + '>' +
588595
(tagDefinition.innerHTML || '') +
589596
(tagDefinition.closeTag ? '</' + tagDefinition.tagName + '>' : '');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-webpack-plugin",
3-
"version": "2.25.0",
3+
"version": "2.26.0",
44
"description": "Simplifies creation of HTML files to serve your webpack bundles",
55
"main": "index.js",
66
"files": [

spec/BasicSpec.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,72 @@ describe('HtmlWebpackPlugin', function () {
750750
}, false, true);
751751
});
752752

753+
it('allows events to add a no-value attribute', function (done) {
754+
var examplePlugin = {
755+
apply: function (compiler) {
756+
compiler.plugin('compilation', function (compilation) {
757+
compilation.plugin('html-webpack-plugin-alter-asset-tags', function (pluginArgs, callback) {
758+
pluginArgs.body = pluginArgs.body.map(tag => {
759+
if (tag.tagName === 'script') {
760+
tag.attributes.async = true;
761+
}
762+
return tag;
763+
});
764+
callback(null, pluginArgs);
765+
});
766+
});
767+
}
768+
};
769+
testHtmlPlugin({
770+
entry: {
771+
app: path.join(__dirname, 'fixtures/index.js')
772+
},
773+
output: {
774+
path: OUTPUT_DIR,
775+
filename: '[name]_bundle.js'
776+
},
777+
plugins: [
778+
new HtmlWebpackPlugin(),
779+
examplePlugin
780+
]
781+
},
782+
[/<body>[\s]*<script type="text\/javascript" src="app_bundle.js" async><\/script>[\s]*<\/body>/],
783+
null, done, false, false);
784+
});
785+
786+
it('allows events to remove an attribute by setting it to false', function (done) {
787+
var examplePlugin = {
788+
apply: function (compiler) {
789+
compiler.plugin('compilation', function (compilation) {
790+
compilation.plugin('html-webpack-plugin-alter-asset-tags', function (pluginArgs, callback) {
791+
pluginArgs.body = pluginArgs.body.map(tag => {
792+
if (tag.tagName === 'script') {
793+
tag.attributes.async = false;
794+
}
795+
return tag;
796+
});
797+
callback(null, pluginArgs);
798+
});
799+
});
800+
}
801+
};
802+
testHtmlPlugin({
803+
entry: {
804+
app: path.join(__dirname, 'fixtures/index.js')
805+
},
806+
output: {
807+
path: OUTPUT_DIR,
808+
filename: '[name]_bundle.js'
809+
},
810+
plugins: [
811+
new HtmlWebpackPlugin(),
812+
examplePlugin
813+
]
814+
},
815+
[/<body>[\s]*<script type="text\/javascript" src="app_bundle.js"><\/script>[\s]*<\/body>/],
816+
null, done, false, false);
817+
});
818+
753819
it('fires the html-webpack-plugin-before-html-processing event', function (done) {
754820
var eventFired = false;
755821
var examplePlugin = {

0 commit comments

Comments
 (0)