Skip to content

Commit

Permalink
feat: omit html tag attribute with null/undefined/false value
Browse files Browse the repository at this point in the history
Refs #1598
  • Loading branch information
themgoncalves committed Feb 12, 2021
1 parent 33d69f4 commit aa6e78d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/html-tags.js
Expand Up @@ -25,12 +25,12 @@ const voidTags = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'k
* A tag element according to the htmlWebpackPlugin object notation
*
* @param xhtml {boolean}
* Wether the generated html should add closing slashes to be xhtml compliant
* Whether the generated html should add closing slashes to be xhtml compliant
*/
function htmlTagObjectToString (tagDefinition, xhtml) {
const attributes = Object.keys(tagDefinition.attributes || {})
.filter(function (attributeName) {
return tagDefinition.attributes[attributeName] !== false;
return tagDefinition.attributes[attributeName] === '' || tagDefinition.attributes[attributeName];
})
.map(function (attributeName) {
if (tagDefinition.attributes[attributeName] === true) {
Expand All @@ -49,13 +49,13 @@ function htmlTagObjectToString (tagDefinition, xhtml) {
* @param {string} tagName
* the name of the tag e.g. 'div'
*
* @param {{[attributeName: string]: string|boolean}} [attributes]
* @param {{[attributeName: string]: string|boolean|null|undefined}} [attributes]
* tag attributes e.g. `{ 'class': 'example', disabled: true }`
*
* @param {string} [innerHTML]
*
* @param {{[attributeName: string]: string|boolean}} [meta]
* meta information about the tag e.g. `{ 'pluhin': 'html-webpack-plugin' }`
* @param {{[attributeName: string]: string|boolean|null|undefined}} [meta]
* meta information about the tag e.g. `{ 'plugin': 'html-webpack-plugin' }`
*
* @returns {HtmlTagObject}
*/
Expand Down
68 changes: 68 additions & 0 deletions spec/basic.spec.js
Expand Up @@ -1232,6 +1232,74 @@ describe('HtmlWebpackPlugin', () => {
null, done, false, false);
});

it('allows events to remove an attribute by setting it to null', done => {
const examplePlugin = {
apply: function (compiler) {
compiler.hooks.compilation.tap('HtmlWebpackPlugin', compilation => {
HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync('HtmlWebpackPluginTest', (pluginArgs, callback) => {
pluginArgs.assetTags.scripts = pluginArgs.assetTags.scripts.map(tag => {
if (tag.tagName === 'script') {
tag.attributes.async = null;
}
return tag;
});
callback(null, pluginArgs);
});
});
}
};
testHtmlPlugin({
mode: 'production',
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [
new HtmlWebpackPlugin(),
examplePlugin
]
},
[/<script defer="defer" src="app_bundle.js"><\/script>[\s]*<\/head>/],
null, done, false, false);
});

it('allows events to remove an attribute by setting it to undefined', done => {
const examplePlugin = {
apply: function (compiler) {
compiler.hooks.compilation.tap('HtmlWebpackPlugin', compilation => {
HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync('HtmlWebpackPluginTest', (pluginArgs, callback) => {
pluginArgs.assetTags.scripts = pluginArgs.assetTags.scripts.map(tag => {
if (tag.tagName === 'script') {
tag.attributes.async = undefined;
}
return tag;
});
callback(null, pluginArgs);
});
});
}
};
testHtmlPlugin({
mode: 'production',
entry: {
app: path.join(__dirname, 'fixtures/index.js')
},
output: {
path: OUTPUT_DIR,
filename: '[name]_bundle.js'
},
plugins: [
new HtmlWebpackPlugin(),
examplePlugin
]
},
[/<script defer="defer" src="app_bundle.js"><\/script>[\s]*<\/head>/],
null, done, false, false);
});

it('provides the options to the afterEmit event', done => {
let eventArgs;
const examplePlugin = {
Expand Down
2 changes: 1 addition & 1 deletion typings.d.ts
Expand Up @@ -263,7 +263,7 @@ declare namespace HtmlWebpackPlugin {
* E.g. `{'disabled': true, 'value': 'demo'}`
*/
attributes: {
[attributeName: string]: string | boolean;
[attributeName: string]: string | boolean | null | undefined;
};
/**
* The tag name e.g. `'div'`
Expand Down

0 comments on commit aa6e78d

Please sign in to comment.