From 9959484f5337872f5af2a2f738228f5348a93901 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Tue, 20 Jun 2023 04:06:34 +0300 Subject: [PATCH] fix: avoid have undefined `type` for script tags (#1809) --- index.js | 67 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index 27015d8a..23d7fa39 100644 --- a/index.js +++ b/index.js @@ -786,16 +786,25 @@ 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 + // @ts-ignore + return jsAssets.map(src => { + const attributes = {}; + + 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, + meta: { plugin: 'html-webpack-plugin' }, + attributes + }; + }); } /** @@ -820,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 + }]; } /** @@ -883,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 } }]; } @@ -1052,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,