diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c621483..b3b7a006 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [5.1.0](https://github.com/jantimon/html-webpack-plugin/compare/v5.0.0...v5.1.0) (2021-02-12) + + +### Features + +* omit html tag attribute with null/undefined/false value ([aa6e78d](https://github.com/jantimon/html-webpack-plugin/commit/aa6e78d1430c17d9cf05550b2c9f73a9a0c0166c)), closes [#1598](https://github.com/jantimon/html-webpack-plugin/issues/1598) + ## [5.0.0](https://github.com/jantimon/html-webpack-plugin/compare/v4.5.1...v5.0.0) (2021-02-03) diff --git a/README.md b/README.md index b4819b43..9f5b7d7a 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ Allowed values are as follows: |**`templateParameters`**|`{Boolean\|Object\|Function}`| `false`| Allows to overwrite the parameters used in the template - see [example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/template-parameters) | |**`inject`**|`{Boolean\|String}`|`true`|`true \|\| 'head' \|\| 'body' \|\| false` Inject all assets into the given `template` or `templateContent`. When passing `'body'` all javascript resources will be placed at the bottom of the body element. `'head'` will place the scripts in the head element. Passing `true` will add it to the head/body depending on the `scriptLoading` option. Passing `false` will disable automatic injections. - see the [inject:false example](https://github.com/jantimon/html-webpack-plugin/tree/master/examples/custom-insertion-position)| |**`publicPath`**|`{String\|'auto'}`|`'auto'`|The publicPath used for script and link tags| -|**`scriptLoading`**|`{'blocking'\|'defer'}`|`'blocking'`| Modern browsers support non blocking javascript loading (`'defer'`) to improve the page startup performance. | +|**`scriptLoading`**|`{'blocking'\|'defer'}`|`'defer'`| Modern browsers support non blocking javascript loading (`'defer'`) to improve the page startup performance. | |**`favicon`**|`{String}`|``|Adds the given favicon path to the output HTML| |**`meta`**|`{Object}`|`{}`|Allows to inject `meta`-tags. E.g. `meta: {viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no'}`| |**`base`**|`{Object\|String\|false}`|`false`|Inject a [`base`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) tag. E.g. `base: "https://example.com/path/page.html`| 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/package.json b/package.json index 7d48a21a..718bca71 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "html-webpack-plugin", - "version": "5.0.0", + "version": "5.1.0", "license": "MIT", "description": "Simplifies creation of HTML files to serve your webpack bundles", "author": "Jan Nicklas (https://github.com/jantimon)", 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 + ] + }, + [/