@@ -161,29 +161,33 @@ function createTitleTemplate(
161
161
return ` | ${ template } `
162
162
}
163
163
164
- function hasTag ( head : HeadConfig [ ] , tag : HeadConfig ) {
165
- const [ tagType , tagAttrs ] = tag
166
- if ( tagType !== 'meta' ) return false
167
- const keyAttr = Object . entries ( tagAttrs ) [ 0 ] // First key
168
- if ( keyAttr == null ) return false
169
- return head . some (
170
- ( [ type , attrs ] ) => type === tagType && attrs [ keyAttr [ 0 ] ] === keyAttr [ 1 ]
171
- )
172
- }
164
+ export function mergeHead ( ...headArrays : HeadConfig [ ] [ ] ) : HeadConfig [ ] {
165
+ const merged : HeadConfig [ ] = [ ]
166
+ const metaKeyMap = new Map < string , number > ( )
167
+
168
+ for ( const current of headArrays ) {
169
+ for ( const tag of current ) {
170
+ const [ type , attrs ] = tag
171
+ const keyAttr = Object . entries ( attrs ) [ 0 ]
172
+
173
+ if ( type !== 'meta' || ! keyAttr ) {
174
+ merged . push ( tag )
175
+ continue
176
+ }
173
177
174
- /**
175
- * Merge head tags ascending precedence
176
- * Prior duplicates are skipped in favor of later ones
177
- */
178
- export function mergeHead ( ... heads : HeadConfig [ ] [ ] ) {
179
- return heads
180
- . filter ( Array . isArray )
181
- . flat ( 1 )
182
- . reverse ( )
183
- . reduce ( ( merged , tag ) => {
184
- if ( ! hasTag ( merged , tag ) ) merged . push ( tag )
185
- return merged
186
- } , [ ] ) as HeadConfig [ ]
178
+ const key = ` ${ keyAttr [ 0 ] } = ${ keyAttr [ 1 ] } `
179
+ const existingIndex = metaKeyMap . get ( key )
180
+
181
+ if ( existingIndex != null ) {
182
+ merged [ existingIndex ] = tag // replace existing tag
183
+ } else {
184
+ metaKeyMap . set ( key , merged . length )
185
+ merged . push ( tag )
186
+ }
187
+ }
188
+ }
189
+
190
+ return merged
187
191
}
188
192
189
193
// https://github.com/rollup/rollup/blob/fec513270c6ac350072425cc045db367656c623b/src/utils/sanitizeFileName.ts
0 commit comments