Skip to content

Commit

Permalink
fix: avoid have undefined type for script tags (#1809)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 20, 2023
1 parent 74e728a commit 9959484
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions index.js
Expand Up @@ -786,16 +786,25 @@ class HtmlWebpackPlugin {
* @returns {Array<HtmlTagObject>}
*/
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
};
});
}

/**
Expand All @@ -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<HtmlTagObject>}
*/
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
}];
}

/**
Expand Down Expand Up @@ -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<HtmlTagObject>}
*/
generateFaviconTag (faviconPath) {
if (!faviconPath) {
return [];
}

generateFaviconTag (favicon) {
return [{
tagName: 'link',
voidTag: true,
meta: { plugin: 'html-webpack-plugin' },
attributes: {
rel: 'icon',
href: faviconPath
href: favicon
}
}];
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9959484

Please sign in to comment.