From c3f23ffa2cc1c7ff65a97d6b5a1202af43b00151 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Tue, 20 Jun 2023 03:35:27 +0300 Subject: [PATCH 1/2] fix: avoid have undefined `type` for script tags --- index.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 27015d8a..0234a1e7 100644 --- a/index.js +++ b/index.js @@ -786,16 +786,22 @@ class HtmlWebpackPlugin { * @returns {Array} */ generatedScriptTags (jsAssets) { - return jsAssets.map(scriptAsset => ({ - tagName: 'script', - voidTag: false, - meta: { plugin: 'html-webpack-plugin' }, - attributes: { - defer: this.options.scriptLoading === 'defer', - type: this.options.scriptLoading === 'module' ? 'module' : undefined, - src: scriptAsset + return jsAssets.map(scriptAsset => { + const attributes = { src: scriptAsset }; + + if (this.options.scriptLoading === 'module') { + attributes.type = 'module'; + } else if (this.options.scriptLoading === 'defer') { + attributes.defer = true; } - })); + + return { + tagName: 'script', + voidTag: false, + meta: { plugin: 'html-webpack-plugin' }, + attributes + }; + }); } /** From 46454408a00605dfcf721f53c57b2195f9053c4f Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Tue, 20 Jun 2023 04:02:29 +0300 Subject: [PATCH 2/2] fix: avoid have undefined `type` for script tags --- index.js | 53 ++++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index 0234a1e7..23d7fa39 100644 --- a/index.js +++ b/index.js @@ -786,15 +786,18 @@ class HtmlWebpackPlugin { * @returns {Array} */ generatedScriptTags (jsAssets) { - return jsAssets.map(scriptAsset => { - const attributes = { src: scriptAsset }; + // @ts-ignore + return jsAssets.map(src => { + const attributes = {}; - if (this.options.scriptLoading === 'module') { - attributes.type = 'module'; - } else if (this.options.scriptLoading === 'defer') { + if (this.options.scriptLoading === 'defer') { attributes.defer = true; + } else if (this.options.scriptLoading === 'module') { + attributes.type = 'module'; } + attributes.src = src; + return { tagName: 'script', voidTag: false, @@ -826,23 +829,19 @@ class HtmlWebpackPlugin { /** * Generate an optional base tag * - * @param {false | string | {[attributeName: string]: string}} baseOption + * @param {string | {[attributeName: string]: string}} base * @returns {Array} */ - generateBaseTag (baseOption) { - if (baseOption === false) { - return []; - } else { - return [{ - tagName: 'base', - voidTag: true, - meta: { plugin: 'html-webpack-plugin' }, - // attributes e.g. { href:"http://example.com/page.html" target:"_blank" } - attributes: (typeof baseOption === 'string') ? { - href: baseOption - } : baseOption - }]; - } + generateBaseTag (base) { + return [{ + tagName: 'base', + voidTag: true, + meta: { plugin: 'html-webpack-plugin' }, + // attributes e.g. { href:"http://example.com/page.html" target:"_blank" } + attributes: typeof base === 'string' ? { + href: base + } : base + }]; } /** @@ -889,21 +888,17 @@ class HtmlWebpackPlugin { * Generate a favicon tag for the given file path * * @private - * @param {string| undefined} faviconPath + * @param {string} favicon * @returns {Array} */ - generateFaviconTag (faviconPath) { - if (!faviconPath) { - return []; - } - + generateFaviconTag (favicon) { return [{ tagName: 'link', voidTag: true, meta: { plugin: 'html-webpack-plugin' }, attributes: { rel: 'icon', - href: faviconPath + href: favicon } }]; } @@ -1058,9 +1053,9 @@ class HtmlWebpackPlugin { scripts: this.generatedScriptTags(assets.js), styles: this.generateStyleTags(assets.css), meta: [ - ...this.generateBaseTag(this.options.base), + ...(this.options.base !== false ? this.generateBaseTag(this.options.base) : []), ...this.generatedMetaTags(this.options.meta), - ...this.generateFaviconTag(assets.favicon) + ...(assets.favicon ? this.generateFaviconTag(assets.favicon) : []) ] }, outputName,