perf(ui-templates): don't emit message data for pure strings

This commit is contained in:
Daniel Roe 2024-05-08 13:52:19 +01:00
parent f687e211a8
commit 9b1bfe69ed
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B

View File

@ -80,6 +80,7 @@ export const RenderPlugin = () => {
// Serialize into a js function
const chunks = html.split(/\{{2,3}\s*[^{}]+\s*\}{2,3}/g).map(chunk => JSON.stringify(chunk))
let hasMessages = chunks.length > 1
let templateString = chunks.shift()
for (const expression of html.matchAll(/\{{2,3}(\s*[^{}]+\s*)\}{2,3}/g)) {
templateString += ` + (${expression[1].trim()}) + ${chunks.shift()}`
@ -88,10 +89,10 @@ export const RenderPlugin = () => {
templateString += ' + ' + chunks.join(' + ')
}
const functionalCode = [
`export type DefaultMessages = Record<${Object.keys({ ...genericMessages, ...messages }).map(a => `"${a}"`).join(' | ') || 'string'}, string | boolean | number >`,
`const _messages = ${JSON.stringify({ ...genericMessages, ...messages })}`,
'export const template = (messages: Partial<DefaultMessages>) => {',
' messages = { ..._messages, ...messages }',
hasMessages ? `export type DefaultMessages = Record<${Object.keys({ ...genericMessages, ...messages }).map(a => `"${a}"`).join(' | ') || 'string'}, string | boolean | number >` : '',
hasMessages ? `const _messages = ${JSON.stringify({ ...genericMessages, ...messages })}` : '',
`export const template = (${hasMessages ? 'messages: Partial<DefaultMessages>' : ''}) => {`,
hasMessages ? ' messages = { ..._messages, ...messages }' : '',
` return ${templateString}`,
'}',
].join('\n')