From aa6e78d1430c17d9cf05550b2c9f73a9a0c0166c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Gon=C3=A7alves?= Date: Fri, 12 Feb 2021 12:51:06 +0100 Subject: [PATCH] feat: omit html tag attribute with null/undefined/false value Refs #1598 --- lib/html-tags.js | 10 +++---- spec/basic.spec.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++ typings.d.ts | 2 +- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/lib/html-tags.js b/lib/html-tags.js index 5e727a92..3dd3df92 100644 --- a/lib/html-tags.js +++ b/lib/html-tags.js @@ -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) { @@ -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} */ diff --git a/spec/basic.spec.js b/spec/basic.spec.js index 13fd6a55..3cb646d5 100644 --- a/spec/basic.spec.js +++ b/spec/basic.spec.js @@ -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 + ] + }, + [/