optimize head merging and preserve order

pull/4660/head
Divyansh Singh 5 months ago
parent 53369abdc2
commit d5b15634e7

@ -161,29 +161,33 @@ function createTitleTemplate(
return ` | ${template}` return ` | ${template}`
} }
function hasTag(head: HeadConfig[], tag: HeadConfig) { export function mergeHead(...headArrays: HeadConfig[][]): HeadConfig[] {
const [tagType, tagAttrs] = tag const merged: HeadConfig[] = []
if (tagType !== 'meta') return false const metaKeyMap = new Map<string, number>()
const keyAttr = Object.entries(tagAttrs)[0] // First key
if (keyAttr == null) return false for (const current of headArrays) {
return head.some( for (const tag of current) {
([type, attrs]) => type === tagType && attrs[keyAttr[0]] === keyAttr[1] const [type, attrs] = tag
) const keyAttr = Object.entries(attrs)[0]
}
if (type !== 'meta' || !keyAttr) {
merged.push(tag)
continue
}
/** const key = `${keyAttr[0]}=${keyAttr[1]}`
* Merge head tags ascending precedence const existingIndex = metaKeyMap.get(key)
* Prior duplicates are skipped in favor of later ones
*/ if (existingIndex != null) {
export function mergeHead(...heads: HeadConfig[][]) { merged[existingIndex] = tag // replace existing tag
return heads } else {
.filter(Array.isArray) metaKeyMap.set(key, merged.length)
.flat(1) merged.push(tag)
.reverse() }
.reduce((merged, tag) => { }
if (!hasTag(merged, tag)) merged.push(tag) }
return merged
}, []) as HeadConfig[] return merged
} }
// https://github.com/rollup/rollup/blob/fec513270c6ac350072425cc045db367656c623b/src/utils/sanitizeFileName.ts // https://github.com/rollup/rollup/blob/fec513270c6ac350072425cc045db367656c623b/src/utils/sanitizeFileName.ts

Loading…
Cancel
Save