Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid have undefined type for script tags #1809

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 34 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
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